Thread: Array question

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    18

    Question Array question

    Hello there-

    I'm drawing a blank here with this problem: create an array of grades based on user input, add up the scores and return a letter grade. Anyhoo...what I can't seem to figure out is what the best way to create this array. I would like the user to be able to add each score one at a time ("87", "78", "100", "97"...) until the Enter key is pressed.

    Any help is extremely appreciated!!!

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, here is simple algorithm
    Code:
    integer scorearray;
    
    do
    { 
          prompt user to enter score
          get val
          scoresarray <- val
    }while val not equal to '\n';
    
    find the sum of all the scoresarray
    
    grade <-- findgrade(send the sum);
    
    output grade
    u will have to think about the findgrade function

    hope this help you

    ssharish2005

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Presumably it's an assignment. Does the assignment mention a maximum number? In which case, create an array of N grades, then have it not allow the user enter more than N values.

    If it truly has to be of arbitrary length, you could use a pointer and realloc, or ask the user how many grades they will enter, then malloc to that amount.

    Finally, you could use a linked list or something, but that's somewhat overkill for this.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    Thank you both.
    This is what I had in the meantime - I think I'm on the right track.

    Code:
    do
    	{
    	printf	("\nPlease enter assignments scores, one at a time. Press Enter key when done:");
    	scanf	(  "%i", aScore);
    	}	while ('\n'!=getchar());

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    int grades[100]={0}, i=0;
    
    while(scanf("%d",&grades[i]) == 1 && i++<100);
    could be one solution...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM