How to count the entries of array while taking them as an input from the user?

Hi!
I have understand arrays a little bit. But the problem, I am dealing with requires to count the entries when they are entered by the user , which then defines the size of array. The problem then demands to find the sum and average.
I have made the code as per my knowledge.But it's not working.
Help!

#include<iostream>
using namespace std;
int main()
{
int i, count;
double grade[count] ,average,sum;
for(i=0;grade[i]>=0;i++)
{ if(grade[i]>=0)
{
cout<<"Grade "<<i+1<<": ";
cout<<grade[i];
count++;
}
else
break;
}
for(i=0,sum=0;i<count;i++)
{
sum=sum+grade[i];
}
average=sum/count;

cout<<"Average"<<average;
}
Last edited on
double grade[count]

Here, I see you are making an array. Tell me, what is the size of this array?
The size of Array is making trouble. The problem states:"... reads a list of double-precision
grades from the keyboard into an array named grade. The grades are to be counted as they’re
read, and entry is to be terminated when a negative value has been entered."
So here what I understand is The size of arrays will be determined by the number of entries.
Yes you're trying to create the array BEFORE you know how big it needs to be. Do you see a problem there? Do you understand that you need to know how big to make an array before you make it?
So what is the solution. How I am suppose to take a size of array when it is not mentioned?
Make a big array. If it turns out not to be big enough, make a bigger array, copy the first one into the second, and then keep going.

This is something you could have worked out for yourself. Programming is thinking.
Yeah programming is Thinking. But it usually fails when you got exam next day . Thanks anyway.
Topic archived. No new replies allowed.