Thread: Error Redeclaration Int sum

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    6

    Error Redeclaration Int sum

    Hi i got error redeclaration int sum for my programme.. Please assist me to fix my error at line 10 which i underline and bold.
    Code:
    *#include <stdio.h>
    
    
    int main() {
     
    int marks[4][4]; /* declare an array to store 4 values */
    
    int j; /* array index */
     
      int sum = 0;
       float average;
     
      printf("Enter the marks of 4 students :- \n");
    
      
    
     // read marks of 5 students
     
      for ( j = 0; j < 4 ; j++ ) {
     
         printf("Marks of student %d : ", j+1);
     
         scanf("%d", &marks[j]); /* input marks of student 'j' in index 'j' */
       }
    
      
    
     /*  Compute sum and average */
     
      for ( j = 0; j < 4 ; j++ ) {
     
      sum += marks[j]; /* add the value in each index of the array */
       }
     
      average = (float)sum / 4;
    
     
    
      printf("Sum : %d\n", sum);
     
      printf("Average : %f\n", average);
     
      return 0;
    } *
    Last edited by iswan82; 09-08-2016 at 09:22 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I compiled this:
    Code:
    #include <stdio.h>
    
    int main() {
        int marks[4][4]; /* declare an array to store 4 values */
        int j; /* array index */
        int sum = 0;
        float average;
    
        printf("Enter the marks of 4 students :- \n");
    
        // read marks of 5 students
        for ( j = 0; j < 4 ; j++ ) {
            printf("Marks of student %d : ", j+1);
            scanf("%d", &marks[j]); /* input marks of student 'j' in index 'j' */
        }
    
        /*  Compute sum and average */
        for ( j = 0; j < 4 ; j++ ) {
            sum += marks[j]; /* add the value in each index of the array */
        }
        average = (float)sum / 4;
    
        printf("Sum : %d\n", sum);
        printf("Average : %f\n", average);
    
        return 0;
    }
    and the compiler gave me these warnings:
    Code:
    test.c: In function ‘main’:
    test.c:14:15: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[4]’ [-Wformat=]
             scanf("%d", &marks[j]); /* input marks of student 'j' in index 'j' */
                   ^
    test.c:19:13: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
             sum += marks[j]; /* add the value in each index of the array */
                 ^
    I did not get "error redeclaration int sum", and in fact eyeballing the code shows that the int variable named sum is only declared once, so the error message you cited does not make sense.

    To fix the warnings from my compiler:
    • You need to fix the logic error where your comment says "read marks of 5 students", but your code actually just reads 4 values. You probably want a nested loop: the outer loop would loop over each of the 5 students, and the inner loop would the student's marks one by one. Oh, and you either need to fix your comment, or fix the code so that there is space to store the marks for 5 rather than 4 students.
    • Likewise, you need a nested loop to add the student marks. But perhaps you are supposed to compute the sum and average for each student? Check your requirements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function redeclaration gives an error
    By JayCee++ in forum C++ Programming
    Replies: 8
    Last Post: 06-24-2012, 09:43 AM
  2. enum redeclaration
    By mynickmynick in forum C Programming
    Replies: 4
    Last Post: 02-26-2009, 12:06 PM
  3. redeclaration of enumerator
    By dlittle in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 11:35 AM
  4. string redeclaration in function, why is required?
    By elninio in forum C++ Programming
    Replies: 6
    Last Post: 07-14-2008, 06:58 PM
  5. Redeclaration Error
    By shiju in forum C Programming
    Replies: 6
    Last Post: 09-02-2004, 07:51 AM

Tags for this Thread