Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ write the code to read in ASCII input data into an appropriate vector and

ID: 3593913 • Letter: I

Question

In C++ write the code to read in ASCII input data into an appropriate vector and then dump the data using an appropriate function. Use cin and cout. Similiar to:

int main(int argc, char *argv[]) {

vector<double> myvec;

// read the 20 numbers from stdin and push back into a 'vector'
for (int i = 0; i < 20; ++i) {
double x;
cin >> x;
myvec.push_back(x);
}

// dump the 20 numbers from the 'vector' to stdout
for (int i = 0; i < 20; ++i) {
cout << myvec.at(i) << endl;
}

// cout << "MAX VALUE IS " << max_distance << endl;
// cout << "MIN VALUE IS " << min_distance << endl;
return 0;
}

But obviously needs to be able to take in ASCII not integers.

Explanation / Answer

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
vector<char> myvec;
// read the 20 numbers from stdin and push back into a 'vector'
for (int i = 0; i < 20; ++i) {
char x;
cin >> x;
myvec.push_back(x);
}
// dump the 20 numbers from the 'vector' to stdout
for (int i = 0; i < 20; ++i) {
cout << myvec.at(i) << endl;
}
// cout << "MAX VALUE IS " << max_distance << endl;
// cout << "MIN VALUE IS " << min_distance << endl;
return 0;
}