Yiannis
Yiannis

Reputation: 163

float arrays using functions and for loop

I have a mere grasp of functions in C so bear with me. My first two functions are correct and probably the third one. The goal is to be able to output up to 100 assignments' grades when entering the points awarded and available in.

We were given a standard format of char GradeFromTotalRawMarks(float marksAwarded[], float marksAvailable[], int maxAssignments); . I keep getting the following error about pointers (which I have no knowledge of yet) and I don't know how to use max assignments, which should indicate the total number of assignments being passed to the function.

argument of type "float" is incompatible with parameter of type "float *" 'function':
cannot convert from 'float' to 'float *

Here's the code:

#include <stdio.h>

char GradeFromPercentage(float percentage);
char GradeFromRawMarks(float marksAwarded, float marksAvailable);
char GradeFromTotalRawMarks(float marksAwarded[], float marksAvailable[], int maxAssignments);

int main()
{
    int maxAssignments;
    float MarksAwarded[100];
    float MarksAvailable[100];
    GradeFromTotalRawMarks(MarksAwarded[100], MarksAvailable[100], maxAssignments);

    return 0;
}
char GradeFromPercentage(float percentage)
{
    char grade;
    if (percentage >= 90)
    {
        grade = 'A';
    }
    else if (percentage >= 70)
    {
        grade = 'B';
    }
    else if (percentage >= 50)
    {
        grade = 'C';
    }
    else if (percentage >= 30)
    {
        grade = 'D';
    }
    else
    {
        grade = 'F';
    }
    return grade;
}

char GradeFromRawMarks(float marksAwarded, float marksAvailable)
{
    float percentage = (marksAwarded / marksAvailable) * 100;
    GradeFromPercentage(percentage);

    return GradeFromPercentage(percentage);
}
char GradeFromTotalRawMarks(float marksAwarded[], float marksAvailable[], int maxAssignments)
{
    char finish = 'n';
    int count1;
    while (finish == 'n')
    {
        for (count1 = 0; count1 < 100 && finish == 'n'; count1++)
        {       
            printf("Enter the marks awarded:");
            scanf("%f", &marksAwarded[count1]);
            printf("Enter the marks available: \n", &marksAvailable[count1]);
            scanf("%f", &marksAvailable[count1]);

            GradeFromRawMarks(marksAwarded[count1], marksAvailable[count1]);

            printf("If you have finished, type n.");
            scanf("%c", &finish);
        }
    }

    return GradeFromRawMarks(marksAwarded[count1], marksAvailable[count1]);
}

Upvotes: 0

Views: 83

Answers (1)

Ankit Arora
Ankit Arora

Reputation: 140

In your main function, check this line:

GradeFromTotalRawMarks(MarksAwarded[100], MarksAvailable[100], maxAssignments);

The first parameters are wrong. You are supposed to pass the whole array. Change that line to:

GradeFromTotalRawMarks(MarksAwarded, MarksAvailable, maxAssignments);

I compiled your code after fixing the above problem and it compiles without any error.

Upvotes: 1

Related Questions