Thread: error in array

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    5

    error in array

    i'm trying to create a function that reads specific value in array, there's no error in compiling but the output is not like the expected output, it keeps showing some random numbers


    Code:
    #include <stdio.h>
    #include <limits.h>
    
    
    int maxi, mini, subject;
    int A[0];
    int B[0];
    int C[0];
    int D[0];
    int E[0];
    int score[60];
    
    
    void scoregrade(int score[60]);
    void average(float, float);
    float countgpa, gpa;
    void maximini(int score[60]);
    void printset(void);
    
    
    int main()
    {
            int maxi = INT_MIN;
            int mini = INT_MAX;
                for(subject=0; subject <=5; subject++){
                    printf("Please Enter The Score per Subject: ", (subject+1));
                                    scanf("%d", &score[subject]);                                }                                                                                                       scoregrade(score);
                   // maximini(score);
                    printset();
                    average(countgpa, gpa);
    }
    
    
    void maximini(int score[60])
    {
            for(subject=0; subject <=5; subject++){
            if (score[subject] >= maxi){maxi = score[subject];}
            else if (score[subject] <= mini){mini = score[subject];}
            printf("Max = %d\n", maxi);
            printf("Min = %d\n", mini);}
    }
    
    
    void printset(void)
    {
        printf("A = %d\n", A);
        printf("B = %d\n", B);
        printf("C = %d\n", C);
        printf("D = %d\n", D);
        printf("E = %d\n", E);
    }
    
    
    void scoregrade(int score[60])
    {
        for(subject = 0; subject <= 5; subject++){
            if (score[subject] >= 85)
                {A[0]++; printf("Your score is:%d => grade A\n", score[subject]);}
                    else if (score[subject] >= 70)
                        {B[0]++; printf("Your score is:%d => grade B\n", score[subject]);}
                            else if (score[subject] >= 60)
                                {C[0]++; printf("Your score is:%d => grade C\n", score[subject]);}
                                    else if (score[subject] >= 55)
                                        {D[0]++; printf("Your score is:%d => grade D\n", score[subject]);}
                                            else {E[0]++; printf("Your score is:%d => grade E\n", score[subject]);}
        }
    }
    
    
    void average(float countgpa, float gpa)
    {
    //    for (subject = 0; subject <= 5; subject++){
        countgpa=(4*A[0])+(3*B[0])+(2*C[0])+(1*D[0])+(0*E[0]);
        gpa=countgpa/6;
        printf("GPA Semester 1: %.2f\n", gpa);
    }
    sorry for asking such a noob question, thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,505
    I believe zero-length arrays as used here aren't standard C:
    Code:
    int A[0];
    int B[0];
    int C[0];
    int D[0];
    int E[0];
    If I remember correctly, they are a language extension for a specific use involving flexible structs or something like that. This certainly isn't your use case here, and it looks like you don't need them to be arrays at all, so perhaps:
    Code:
    int A;
    int B;
    int C;
    int D;
    int E;
    By the way, you should avoid global variables as they tend to make it harder to reason about (and hence debug and maintain) your program.
    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. Replies: 2
    Last Post: 03-29-2017, 01:22 AM
  2. Replies: 16
    Last Post: 08-01-2013, 04:12 AM
  3. Array error somewhere
    By cyberCLoWn in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 11:02 PM
  4. error w/array
    By LouB in forum C++ Programming
    Replies: 5
    Last Post: 10-19-2003, 07:32 PM

Tags for this Thread