Answer to Question #297574 in C++ for Kelly

Question #297574

How to execute this using vector?




#include <iomanip>


#include <iostream>



using namespace std;



int main() {



const int MAXCNT = 10; // Constant


float arr[MAXCNT], x; // Array, temp. variable


float array2[MAXCNT];



int i, cnt; // Index, quantity



cout << "Enter up to 10 numbers \n"


<< "(Quit with a letter):" << endl;



for (i = 0; i < MAXCNT && cin >> x; ++i)


arr[i] = x;



cnt = i;



// copying elements of an array to another


// array2 = arr


for (int index = 0; index < cnt; index++)


array2[index] = arr[index];



cout << "The given numbers:\n" << endl;



for (i = 0; i < cnt; ++i)


cout << setw(10) << arr[i];



cout << endl;



return 0;


}



1
Expert's answer
2022-02-14T12:04:43-0500
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;


int main() {


	const int MAXCNT = 10; // Constant
	vector<float> arr;
	vector<float> array2;
	float x;
	int i, cnt; // Index, quantity
	cout << "Enter up to 10 numbers \n"<< "(Quit with a letter):" << endl;
	for (i = 0; i < MAXCNT && cin >> x; ++i)
		arr.push_back(x);
	cnt = i;


	// copying elements of an array to another
	// array2 = arr
	for (int index = 0; index < cnt; index++)
		array2.push_back(arr[index]);
	cout << "The given numbers:\n" << endl;
	for (i = 0; i < cnt; ++i)
		cout << setw(10) << arr[i];


	cout << endl;


	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog