-1

so here is my code:

#include <stdio.h>



double CalculateFinalScore(int assignment[], int midterm[], int finalExam[], double scoreSum[], char gradeAchieved[]) 
{
    int i = 0;

    for (i = 0; i < NUM_SCORES; ++i) 
    {
        scoreSum[i] = (assignment[i] * 0.2) + (midterm[i] * 0.3) + (finalExam[i] * 0.5);
    }
}

char grade(int assignment[], int midterm[], int finalExam[], double scoreSum[], char gradeAchieved[]){
    char A = 'A';
    char B = 'B';
    char C = 'C';
    char D = 'D';
    char E = 'E';

    int j = 0;

    for (j = 0; j < NUM_SCORES; ++j){

    if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 85 ) {
        gradeAchieved[j] = A;
    }
    else if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 75) && (CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) < 85)  ){
        gradeAchieved[j] = B;
    }
    else if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 60) && (CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) < 75)  ){
        gradeAchieved[j] = C;
    }
    else if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 45) && (CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) < 60)  ){
        gradeAchieved[j] = D;
    } 
    else if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 0) && (CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) < 45)  ){
        gradeAchieved[j] = E;
    }
    } 
}

int main(void) 
{
    int NUM_SCORES;
    char finalGrade[NUM_SCORES];
    double testScores[NUM_SCORES]; 
    int i = 0;
    int x[NUM_SCORES];
    int y[NUM_SCORES];
    int z[NUM_SCORES];

    // Prompt user to enter test scores
    printf("Input number of students: ");
    scanf("%d", &NUM_SCORES);


    for (i = 0; i < NUM_SCORES; ++i) 
    {
        printf("Input student %d assignment score: ", (i+1));
        scanf("%d", &(x[i]));
        printf("Input student %d midterm score: ", (i+1));
        scanf("%d", &(y[i]));
        printf("Input student %d final exam score: ", (i+1));
        scanf("%d", &(z[i]));
        printf("\n");

    }

    printf("\n");
    // Call function to calculate final score
    CalculateFinalScore(x, y, z, testScores, finalGrade);

    for (i = 0; i < NUM_SCORES; ++i) 
    {
        printf("Final student %d test score: ", (i+1));
        printf("%lf\n", testScores[i]);

        printf("Final student %d test score: ", (i+1));
        printf("%lf\n", finalGrade[i]);
    }

    return 0;
}

}

So bassicly, unlike in my code where NUM_SCORES is alerady determined with 4. instead I want to input the NUM_SCORES value so I can determined its value easily, but everytime I do that, there is always an error that my NUM_SCORES is undeclared. oh and I also got these error

main.c: In function ‘CalculateFinalScore’:
main.c:9:21: error: ‘NUM_SCORES’ undeclared (first use in this function)
     for (i = 0; i < NUM_SCORES; ++i)
                     ^~~~~~~~~~
main.c:9:21: note: each undeclared identifier is reported only once for each function it appears in
main.c: In function ‘grade’:
main.c:24:21: error: ‘NUM_SCORES’ undeclared (first use in this function)
     for (j = 0; j < NUM_SCORES; ++j){
                     ^~~~~~~~~~
main.c:26:41: error: expected expression before ‘]’ token
     if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 85 ) {
                                         ^
main.c:26:105: error: expected ‘)’ before ‘{’ token
     if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 85 ) {
                                                                                                         ^
main.c:41:5: error: expected expression before ‘}’ token
     }
     ^
main.c: In function ‘main’:
main.c:81:19: warning: format ‘%lf’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
         printf("%lf\n", finalGrade[i]);
                   ^

Can somebody help me and fix it?

Smarkite
  • 109
  • 6
  • "there is always an error that my NUM_SCORES is undeclared". That's not what the error messages shown are saying. So are you asking about the NUM_SCORES error (which doesn't seem to be shown) or the errors given in the question? – kaylum Dec 20 '19 at 02:40
  • The errors given in the question occur because arguments in the function calls have invalid syntax. `[]` is never valid in function calls (they are valid in the function definition/declaration). That is, should be `CalculateFinalScore(assignment, midterm, finalExam, scoreSum, gradeAchieved)` – kaylum Dec 20 '19 at 02:41
  • if I delete the define num scores and just put the num scores in main, then the num scores will be undeclared at other function, i want to fix it – Smarkite Dec 20 '19 at 02:42
  • Well then why are you showing error messages and code that is different to what you are asking about? Show the actual code that has the errors you want to fix. – kaylum Dec 20 '19 at 02:43
  • i just edit it, sorry – Smarkite Dec 20 '19 at 02:53
  • `NUM_SCORES` is only declared in the scope of the `main` function. The other functions can't see it. You need to pass that value as an argument into the functions. Also, `NUM_SCORES` is never set so everywhere it is currently being used (.e.g `char finalGrade[NUM_SCORES]`) results in undefined behaviour. You need to set it to some value before using it. – kaylum Dec 20 '19 at 02:57
  • There's nothing wrong with being a beginner. But it is advisable to learn the language by doing some basic tutorials or reading some C books before jumping into writing the code. [Here are some useful references](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – kaylum Dec 20 '19 at 02:59

1 Answers1

0

You cannot declare arrays using variable dimension (like in your's main). So, the easiest way is to assumpt constant MAX_SCORES, which will be always greather than your NUM_SCORES, and declare arrays using this.

To ensure that your NUM_SCORES is visible in all functions, use global static variable.

Additionally, you should pass arrays to functions by its name only, as pointers, without [] (like in c# ;) ).

#include <stdio.h>

// define below to maximum value, what you expected
#define MAX_SCORES 255

// declare global variable
static int NUM_SCORES = 0; // this is good practice to initialize global variables

double CalculateFinalScore(int *assignment, int *midterm, int *finalExam, double *scoreSum, char *gradeAchieved) 
{
    // your function body
}

char grade(int *assignment, int *midterm, int *finalExam, double *scoreSum, char *gradeAchieved)
{
    // your function body
}

int main(void) 
{
    // int NUM_SCORES; <- remove this!
    char finalGrade[MAX_SCORES];
    double testScores[MAX_SCORES]; 
    int i = 0;
    int x[MAX_SCORES];
    int y[MAX_SCORES];
    int z[MAX_SCORES];

    // Prompt user to enter test scores
    printf("Input number of students: ");
    scanf("%d", &NUM_SCORES);

    // check NUM_SCORES is less than MAX_SCORES
    if (NUM_SCORES >= MAX_SCORES) {
        printf("\nNumber too big! Should be less than: %d\n", MAX_SCORES);
        return 0;
    }

    // the rest of main() body
}

Finally, this is not a good practice to name variables using upper case letters only - such naming is "reserved" for constants, defined using #define directive. If you want to signalize that it is global variable, simply add underscore ('_') at the beginning of the name, for example _numScores.

VillageTech
  • 1,968
  • 8
  • 18