Hi, This program is supposed to ask the user how many numbers they want to sum up and then take all the numbers one by one to give the final result. Its working but perhaps you all professional programmers will point out some flaws in it.

#include <iostream>
using namespace std;
int main()
{
	int x,sum=0,count;
	int* ptr;
	cout<<endl<<"How many numbers?"<<endl;
	cin>>x;
	ptr=new int[x];
	for(count=1;count<=x;count++)
	{
		cout<<endl<<"Enter "<<count<<"th number:";
		cin>>*ptr;
		sum+=*ptr;
	}
	cout<<endl<<sum;
	return 0;
}

Thanks for your time.

From your logic you didnt need an array of integers at all. you are writing the value to the same pointer every loop. if you want to use the array do something like this.

cin>>ptr[count]

and then later sum up the array. or else simply use an int var and do something like this

#include <iostream>
using namespace std;
int main()
{
	int x,sum=0,count;
	int ptr;
	cout<<endl<<"How many numbers?"<<endl;
	cin>>x;
	//ptr=new int[x]; not needed
	for(count=1;count<=x;count++)
	{
		cout<<endl<<"Enter "<<count<<"th number:";
                                ptr = 0;
		cin>>ptr;
		sum+=ptr;
	}
	cout<<endl<<sum;
	return 0;
}

Also if you decide to use 'new' to allocate memory in your program, dont forget to delete it when its done. For arrays:

delete [] ptr;

Also if you decide to use 'new' to allocate memory in your program, dont forget to delete it when its done. For arrays:

delete [] ptr;

Oh yes, thanks...you found a bug.

The program looks better now. If you want some minor performance tweaks:
change for(count=1;count<=x;count++) to for(int count=1;count<=x;count++) and remove the previous declaration of count.
This has a few advantages:
- the memory declared for count, is cleared after the loop is done
- your program will look better, less declaration at the beginning for vars that are used somewhere in the rest of the program. With a program this small, this isn't a problem, but if you have a big function and you declare a var that you use 100 lines later, it will klose readability
(- the name 'count' will come available for another use)

You could also change: using namespace std; to :

using std::cout;
using std::endl;
using std::cin;

Because you're not using anything else from the namespace.

But if you wish to ignore my advice, that's fine. Your program is good enough for a good grade. ;)

And there's a better way to do it if you have to use the dynamic array, you can increment the ptr everytime instead of using the index and then you can sum it also in the same place.

N I think i found quite a few bugs !!!

This question has already been answered. Start a new discussion instead.