-
Array Problem
Hey Guys, I'm having trouble with this one project i'm doing..I'm new to Arrays, and I'm trying to figure out where I'm going wrong, but to no avail.
My task is to ask how many scores will be input, Then using a loop, enter each test score (Must be at least 6), average and find the largest. I'm having trouble with the logistics i guess.. any advice?
#include <iostream.h>
#include <stdlib.h>
main()
{int Num, Test[What do i put here?], C, Sum=0; float Avg;
cout<<"\nHow Many Test Scores do you want to enter?- You Must enter at least 6: ";cin>>Num;
for(C=0;C<=Num;C++)
{cout<<"\n Enter Score "<<C<<": "; cin>>Test[C];
Sum=Sum + Test[C];
}
Avg=Sum/Num;
system("PAUSE");
return 0;
}
-
I'm proud to say: I think I know the answer to the first question: 5. One less than the number of elements, which is 6, you say?
-
Get your user input for the number of scores first, then declare the array.
-
======================
Declare :
int *TestScores;
int num;
======================
Usage :
cout<<"enter # of scores";
cin>>num;
TestScores=new int[num];
// then TestScores[3]=99;
======================
-
ginoitalo is right, you'll have to use memory
allocation but at the end of the program you'll have to delete it.
======================
Declare :
int *TestScores;
int num;
======================
Usage :
cout<<"enter # of scores";
cin>>num; //if user types in 10
TestScores = (int*)malloc(sizeof(int) * num; //<--
//TestScores will become an array of 10
//at the end of program
free(TestScores);
======================
-
well you could do declare an array of say onethousand and limit size of user input so you don't need to use dynamic memory if you have never used that before. Otherwise, if you have no control over size of user input, then use of dynamic memory is the ticket.
int array[1000];
int index = 20000;
while(index < 6 || index > 1000)
{
cout << "You may enter from 6 - 1000 scores. enter number of scores now. " << endl;
cin >> index;
}
-
Thanks Guys, I appreciate your help.