0

This is a question that I have found on this site but the responses were not directly related to what I believe my problem is. I am a little embarrassed because I'm still very much an amateur. I am asking the user for how many GPAs they would like to enter. Then I am asking for input for each index. The problem is each index is returning 0. So I'm pretty sure I am messing up either the variable type or possibly the way I am incrementing the index. This is a homework problem and I am looking more for guidance than a complete give away.

#include<stdio.h>

main()
{
      char Rspns; 
      int i, toCount, Total;
      float GPA[toCount];

      printf("\n\tYou can average up to 30 GPAs.");
      printf("\n\tPlease choose how many GPAs you would like to average:");
      scanf(" %d" , &toCount); 

      //assign how many indexes array should have
      for(i = 0; i<toCount; i++)
            GPA[i] = i++;


            do
                {

                system("cls");
                printf("\n\nEnter a GPA:");
                scanf(" %f" , &GPA); 
                printf(" %f" , GPA[i]);
               Total += GPA[i];

               getch();
               system("cls");
               printf("\n\n\tWould you like to average the current amount of GPA's?");
               printf("\n\nY/N: ");
               scanf(" %c" , &Rspns);

                       if(Rspns == 'y' || Rspns == 'Y')
                               {

                               Total = Total / toCount;
                               printf("The average of those GPAs is %.1f" , Total);
                               getch();

                               }// end response

                }while(i<=toCount);//end forCount
                Total = Total / toCount;


 } 
Ev00
  • 19
  • 6
  • `float GPA[toCount];` - It hasn't been initialized. – chris Oct 18 '12 at 22:18
  • `GPA[i] = i++;` Since you're incrementing `i` in the loop control, I think you meant `GPA[i] = i;` there. Also, what you have is undefined behaviour. – Daniel Fischer Oct 18 '12 at 22:23
  • This is helpful. I am now seeing that when i go to average the scores I input that it seems like only the last value I entered is being stored. I hate arrays. – Ev00 Oct 19 '12 at 01:46

4 Answers4

0
int i, toCount, Total;
float GPA[toCount];

toCount is not initialized at this point.

Total += GPA[i];

same here, Total starts with an indeterminate value.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Yes. My professor confused me greatly by telling me not to intitalize "toCount" because setting it 0 would mean I have 0 indexes... So even though I allow the user to give it a value I should still intialize it – Ev00 Oct 18 '12 at 22:21
  • @Ev00 declare the array after you have obtained the size from user input. – Daniel Fischer Oct 18 '12 at 22:24
  • If *You can average up to 30 GPAs* then use `30` as the size of your array and check `scanf` read value is lower than `30`. – ouah Oct 18 '12 at 22:24
0

This

scanf(" %f" , &GPA); 

is absolutely not what you want to do: remember, arrays and pointers are closely related in C - see this SO answer.

scanf(" %f" , &(GPA[i]) ); 

will scan the value into the i'th element of the array

Community
  • 1
  • 1
fvu
  • 32,488
  • 6
  • 61
  • 79
  • Thanks so much! thats funny because the previous question on this exact problem recommended the way I had it. I am now getting the values I want.. however now it is skipping additional iterations. – Ev00 Oct 18 '12 at 22:31
  • I cannot comment on answers I haven't seen but there is absolutely no way &GPA is going to work properly / as intended **if GPA is an array**. Are you sure that's what they recommended? With an array variable? – fvu Oct 18 '12 at 22:35
0

Either initialise your GPA array to be a fixed size and validate toCount to be between 1 and the maximum, or use the memory allocation functions malloc or calloc to dynamically allocate your GPA array once you know the size (and free when finished with it).

I can't work out what you are trying to do where you say "assign how many indexes array should have", but there's no need to enter values into the array that you are going to subsequently overwrite (you can use calloc as described above to initialise them all to zero). Note that you should scan into the ith element of the GPA array (&GPA[i]), not into &GPA which is always the first element.

Also, be careful about the i++ because you are incremementing i twice each loop. Although that part of your code is unnecessary, it's a trap to watch out for in general.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
kallikak
  • 844
  • 6
  • 13
0

I think you should be scanning into the specific float as opposed to scanning into the whole array

scanf("%f", &GPA[i]);

also, check your types for printf, the compiler is spewing out warnings.

Igbanam
  • 5,904
  • 5
  • 44
  • 68