UnhappyDynamic memory w/ Arrays?

Posts 1–4 of 4 · Page 1 of 1
Dynamic memory w/ Arrays?
Ok i was watching the tutorial i found on Dynamic memory and it shows you a code that ask the user how many numbers did they want, cin to >> i and which numbers , and make an array and the program only prints the numbers.

What i want to do is the sum of all the numbers he inputed, i thought of storing the values in variables instead but idk...
(If you feel this is like a... blame my imagination ^_^)
Code:
#include <iostream>

using namespace std;

int *arr;
int i,sum = 0;

int main()
{
	cout << "Count: ";
	cin >> i;
	arr = new int[i];
	cout <<"Enter " << i << " numbers.\n";
	for(int n=0;n<i;n++)
	{
		cin >> arr[n];
		sum += arr[n];
	}
	cout << "Sum: " << sum << endl;
	cin.ignore();
	cin.get();
	return 0;
}
WOW thanks, i didnt expect it to be this easy, maybe i should have read more than ask thanks! ^_^ ( It work like magic ;D )
Try vectors Also。
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us