Question

Utilizing Vectors, create 4 vectors: 2 each Integers 2 each Characters Populate each of the vectors...

Utilizing Vectors, create 4 vectors:

  • 2 each Integers

  • 2 each Characters

  1. Populate each of the vectors with appropriate values (integers or

    characters).
    a. Promptuserforappropriatevaluesandvalidatethatentriesare

    correct
    b. Displayvectorcontents

  2. Sort each vector and display the sorted contents

  3. Merge the character vectors and display the result

  4. Merge the integer vectors and display the result

Display the source code and the results of running the compiled program.

Note:
Save the source code to a file and place that into a document.
Ensure that the results of the displays are legible and include them in the document submitted on BB.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<vector>
#include<bits/stdc++.h>
#include<sstream>
using namespace std;

int validateInt(){
int value;
string check_string;
while(true){
cout<<"Enter a integer value:";
getline(cin,check_string);
stringstream convert(check_string);
if(convert >> value && !(convert >> check_string)) //Checking that it can be converted to int and not to string
return value;
cin.clear(); //Clearing the input buffer
       cout<< "Input must be an integer\n";
}
}

char validateChar(){
char value;
string check_string;
while(true){
cout<<"Enter a character value:";
getline(cin,check_string);
stringstream convert(check_string);
if(convert >> value && !(convert >> check_string)) //Checking that it can be converted to char and not to string
return value;
cin.clear(); //Clearing the input buffer
       cout<< "Input must be a character\n";
}
}
int main(){
vector <int> int1;
vector <int>int2;
vector <char> char1;
vector <char>char2;
int size,i,value;
char value2;
cout<<"Enter the size of first integer vector\n";
cin>>size;
cout<<"Enter the values for first integer vector\n";
for(i=0;i<size;i++){
value = validateInt();
int1.push_back(value);
}
cout<<"Integer vector 1:\n";
for(i=0;i<int1.size();i++)
cout<<int1[i]<<" ";
cout<<"\n";
cout<<"Enter the size of second integer vector\n";
cin>>size;
cout<<"Enter the values for second integer vector\n";
for(i=0;i<size;i++){
value = validateInt();
int2.push_back(value);
}
cout<<"Integer vector 2:\n";
for(i=0;i<int2.size();i++)
cout<<int2[i]<<" ";
cout<<"\n";
cout<<"Enter the size of first character vector\n";
cin>>size;
cout<<"Enter the values for first character vector\n";
for(i=0;i<size;i++){
value2 = validateChar();
char1.push_back(value2);
}
cout<<"Character vector 1:\n";
for(i=0;i<char1.size();i++)
cout<<char1[i]<<" ";
cout<<"\n";
cout<<"Enter the size of second character vector\n";
cin>>size;
cout<<"Enter the values for second character vector\n";
for(i=0;i<size;i++){
value2 = validateChar();
char2.push_back(value2);
}
cout<<"Character vector 2:\n";
for(i=0;i<char2.size();i++)
cout<<char2[i]<<" ";
cout<<"\n";
sort(int1.begin(),int1.end());
cout<<"Sorted Integer vector 1:\n";
for(i=0;i<int1.size();i++)
cout<<int1[i]<<" ";
cout<<"\n";
sort(int2.begin(),int2.end());
cout<<"Sorted Integer vector 2:\n";
for(i=0;i<int2.size();i++)
cout<<int2[i]<<" ";
cout<<"\n";
sort(char1.begin(),char1.end());
cout<<"Sorted Character vector 1:\n";
for(i=0;i<char1.size();i++)
cout<<char1[i]<<" ";
cout<<"\n";
sort(char2.begin(),char2.end());
cout<<"Sorted Character vector 2:\n";
for(i=0;i<char2.size();i++)
cout<<char2[i]<<" ";
cout<<"\n";
//Merging the integer vectors
for(i=0;i<int2.size();i++)
int1.push_back(int2[i]);
cout<<"Merged Integer vectors:\n";
for(i=0;i<int1.size();i++)
cout<<int1[i]<<" ";
cout<<"\n";
//Merging the character vectors
for(i=0;i<char2.size();i++)
char1.push_back(char2[i]);
cout<<"Merged charcter vectors:\n";
for(i=0;i<char1.size();i++)
cout<<char1[i]<<" ";
cout<<"\n";
return 0;
}

Output:

Enter the size of first integer vector 3 Enter the values for first integer vector Enter a integer value: Input must be an inEnter a character value:d Enter a character value:2 Enter a character value:^ Character vector l: d 2^ Enter the size of seco

Add a comment
Know the answer?
Add Answer to:
Utilizing Vectors, create 4 vectors: 2 each Integers 2 each Characters Populate each of the vectors...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 2 Sorting, CPU Time, and Big O Analysis Note: you will be using Microsoft Visual...

    Project 2 Sorting, CPU Time, and Big O Analysis Note: you will be using Microsoft Visual Studios 2019 as the compiler. This is an individual assignment. You will have lab time to work on this assignment as well as homework time. Each person will analyze two sorts. The first sort will be either bubble sort or insertion sort. The second sort will be either quick sort or merge sort. Create two projects, one for each sort in the given choices....

  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • // ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; //...

    // ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; // ref to array a // number of data items public ArrayIns(int max) // constructor a = new long[max]; nElems - © // create the array // no items yet --- public void insert(long value) // put element into array a[nElems] = value; nElems++; // insert it // increment size public void display() // displays array contents for(int j=0; j<ntlems; 1++) 1/ for each element,...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • 1. Write a C++ program called Password that handles encrypting a password. 2. The program must...

    1. Write a C++ program called Password that handles encrypting a password. 2. The program must perform encryption as follows: a. main method i. Ask the user for a password. ii. Sends the password to a boolean function called isValidPassword to check validity. 1. Returns true if password is at least 8 characters long 2. Returns false if it is not at least 8 characters long iii. If isValidPassword functions returns false 1. Print the following error message “The password...

  • I need help with certain questions. Please. 18. 4 points For each Rental, list the Rental...

    I need help with certain questions. Please. 18. 4 points For each Rental, list the Rental ID, Rental date, customer ID, customer first name, customer last name, and count of disks rented; sort by Rental ID. Show the Rental date formatted as ‘mm-dd-yyyy.’ Hint: use a GROUP BY clause. 19. 4 points List the disk ID, title name, rating, format description, and fee amount for all copies rented in Rental 3; sort by disk ID. Show the fee amount formatted...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT