Thread: sum+= not working???

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    8

    sum+= not working???

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[]) 
    {
     int students;
     int *score;
     int i;
     int avg = 0, sum=0;
     
      printf ("enter student amount:\n");
      scanf ("%d", &students);
     score = (int*)malloc(sizeof(int)*students);
     
     for (i=0; i<students; i++){
      //scanf ("%d", &score[i])
      scanf ("%d", score+i);
     }
     
     for (i=0; i<students; i++){
      printf ("student %d: %d\n", i+1, score[i]);
     }
     
     sum += score[i];
     printf ("sum: %d\n", sum);
     avg = sum/ students;
     printf ("Avg = %d", avg);
     
     free(score);
     
     system("pause"); 
     return 0;
    }
    this is my code, can anyone help me? I'm using the sum+= score [i], it should work since i can add the array part right?

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    You don't cast the malloc call, and you didn't increment the sum in a loop.

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    int main(int argc, char *argv[])
    {
     int students;
     int *score;
     int i;
     int sum=0;
     float avg = 0;
    
    
      printf ("enter student amount:\n");
      scanf ("%d", &students);
     score = malloc(sizeof(*score)*students);
    
    
     printf ("enter scores:\n");
     for (i=0; i<students; i++){
      //scanf ("%d", &score[i])
      scanf ("%d", score+i);
     }
    
    
     for (i=0; i<students; i++){
      printf ("student %d: %d\n", i+1, score[i]);
      sum += score[i];
     }
    
    
     printf ("sum: %d\n", sum);
     avg = (float)sum / students;
     printf ("Avg = %f\n", avg);
    
    
     free(score);
    
    
     system("pause");
     return 0;
    }

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    8
    could you please explain what you meant by the malloc part? and why would I increment the sum in the loop, I want the sum to be calculated after each student has entered their score. Thx!

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    You were accessing a part of the array you didn't allocate earlier was the first problem. Your variable i was equal to students and your max index was students - 1.

    If you want to do a sum you have to walk through each index of the array which is why you want to increment the sum in the loop.

  6. #6
    Registered User
    Join Date
    Jul 2016
    Posts
    8
    Thanks guys, the problem is solved, sum is now in the loop, works perfectly now. Thx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-25-2016, 08:49 AM
  2. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  3. eof isn't working
    By geek@02 in forum Windows Programming
    Replies: 4
    Last Post: 08-16-2008, 01:01 PM
  4. .exe not working?
    By deng17 in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2007, 09:16 AM
  5. an rpg i'm working on
    By wesdgreat in forum C++ Programming
    Replies: 0
    Last Post: 01-29-2003, 06:37 PM

Tags for this Thread