Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple array question

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hey guys, I'm in a C programming class in school, and my teacher doesn't teach too well for those of you who hate kids who ask for homework help. In any case, I have to make an array of test scores in which the user enters the size of the array (number of scores), and the scores themselves. What I'm confused about is how to prompt the user for the scores and store them in each individual term of the array. All we know how to use really is printf, scanf, do, while, for loops, etc. Any help would be greatly appreciated.
 
Well well well,

What I can understand from your question is you are in need of a varying array! and the things that you know (as mentioned by you) dosent give you enough power to do that as in C you cant have varying lenght arrays. They are static, fixed lenght.

With what you know, I can give you a suggestion (no code). And that would be to take a big enough array with respect to the data you intend to enter say 10! Take an array of 12 and proceed with the loop where you fill the array using scanf("%d", &ar[idx]); kinda stuff. That's all...

I hope you can start with that ;-)

have fun coding...

Roy.
user.gif
 
If all you have gotten so far is how to print out data and loop on it, i would define a MAX_SIZE constant variable for the array and dont bother with malloc and free. To loop on the input, i would use a for loop with the input value for number of entrys used to control the for loop in the second arguement. Increment the variable in the for loop in its 3rd argument and just keep prompting the user for a grade.

That is the overview of it. Good luck :)

<Matt
 
add as many defines as you expect to have input.
Handle the final array as needed to display
or print to screen or file.

PLEASE!!!!!!!!!!!!!
I kNow this is cumbersome. The guy is just
beginning. This is simple, maybe not so elegant
as the experts but It gets the job done.

finally you have to study more than &quot;for, while,
printf&quot; a bad teacher is no excuse for a bad
student. I have taught myself and I have no
teacher at all.

#define ONE 1
#define TWO 2

void main()
{
char word[maxarray];
printf(&quot;\Input character array --- &quot;);
gets(word);
int x = atoi (word);
int y = 0;

if (x == ONE)
{
char word1[20][ONE];
while (y < x)
{
gets(word1 [y]);
y++;
}
}
if (x == TWO)
{
char word1[20][TWO];
while (y < x)
{
gets(word1 [y]);
y++;
}
}
}
 
And of course there should be error
checking. Right now just remember
that the input for the array size
must be translatable to an integer.

&quot;This string does not translate&quot; error.

&quot;2&quot; The preceding string does translate. no error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top