C - Grade boundary program, potential run time error?

233 Views Asked by At

The aim of this program is to determine the minimum marks needed in the exam to get a certain grade. Eg. the user will enter TEST 5 10 10 PROJECT 9 10 15, meaning that the test they got 5/10 and it's worth 10% of their mark, and the project 9/10 worth 15% of their mark.

I wrote this program and hard coded in the information but now that I'm asking the user to input the info I'm not managing to get past the printing inside the for loop. Can anyone help? I think it's taking too long to run, when I left the program running for about half an hour I managed to get it to print out 'Total' but nothing more.

EDIT: here is the expected output. And here is what I'm getting at the moment.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define P 50
#define H3 65
#define H2b 70
#define H2a 75
#define H1 80
#define MAX 10

typedef char name_t[MAX];
typedef struct {
    name_t name;
    double mark;
    double outof;
    double weight;
    double total;
} assesment_t;


int main(int argc, char **argv){
    assesment_t Subject[MAX];
    /*---------------------------------------------------------------------------*/    
    printf("\n\nEnter information in this order separated by a space\n");
    printf("When all info entered, press enter\n");
    printf("(Include exam but enter the Mark bit as 0):\n");
    printf("Name    Mark    OutOf   Weight\n\n");
    /*---------------------------------------------------------------------------*/    
    int i, n;
    double total = 0;
    /*---------------------------------------------------------------------------*/    
    for(i = 0;(scanf("%s %lf %lf %lf", &Subject[i].name,&Subject[i].mark,&Subject[i].outof,&Subject[i].weight)) == 4;i++) {

        if(i == 0) {
            printf("Name\tMark\tOut of\tWeight\tTotal");   
        }

        Subject[i].total = ( (Subject[i].mark/Subject[i].outof) * Subject[i].weight );

        printf("\n%s\t%2.1lf\t%2.1lf\t%2.2lf\t%2.2lf", Subject[i].name, Subject[i].mark,
            Subject[i].outof, Subject[i].weight, Subject[i].total);
    }
    /*---------------------------------------------------------------------------*/       
    n = i;
    for( i = 0; i <= n; i++) {
        total += Subject[i].total;
    }
    printf("\nTotal: %3.2lf\n\n", total);
    /*---------------------------------------------------------------------------*/        
    double grade, result;
    for( grade = 23; grade <= 60; grade++) {
        result = (grade / Subject[i-1].outof) * Subject[i-1].weight;
        if(total + result > P && (total + result - 1) < P)
            printf("minimum of %2.0lf is needed for FAIL\n", grade);
        else if(total + result > H3 && (total + result - 1) < H3)
            printf("minimum of %2.0lf is needed for P\n", grade);
        else if(total + result > H2b && (total + result - 1) < H2b)
            printf("minimum of %2.0lf is needed for H3\n", grade);
        else if(total + result > H2a && (total + result - 1) < H2a)
            printf("minimum of %2.0lf is needed for H2b\n", grade);
        else if(total + result > H1 && (total + result - 1) < H1)
            printf("minimum of %2.0lf is needed for H2a\n", grade);
        else if(total + result > H1 && (total + result - 1) < H1)
            printf("minimum of %2.0lf is needed for H1\n", grade);
        else
            ;
    }
    /*---------------------------------------------------------------------------*/
    return 0;
}
1

There are 1 best solutions below

4
On

Please take a look at the below changes and I see it is working fine for me: After calculating total you should set i=1 and there should be a break condition within the for loop like if(i>10) break; I have made few other changes please run the code and see.I see int with double arithmetic being done which might not field expected result.Check the below link: Can I compare and add a floating-point number to an integer in C?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define P 50
#define H3 65
#define H2b 70
#define H2a 75
#define H1 80
#define MAX 10 

typedef char name_t[MAX];
typedef struct {
    name_t name;
    double mark;
    double outof;
    double weight;
    double total;
} assesment_t;


int main(int argc, char **argv){
    assesment_t Subject[MAX];
       double grade=0, result=0;
    /*---------------------------------------------------------------------------*/    
    printf("\n\nEnter information in this order separated by a space\n");
    printf("When all info entered, press enter\n");
    printf("(Include exam but enter the Mark bit as 0):\n");
    printf("Name    Mark    OutOf   Weight\n\n");
    /*---------------------------------------------------------------------------*/    
    int i, n;
    double total = 0;
    /*---------------------------------------------------------------------------*/    
    for(i = 0;i<10;i++) {
scanf("%s %lf %lf %lf", &Subject[i].name,&Subject[i].mark,&Subject[i].outof,&Subject[i].weight);
        if(i == 0) {
            printf("Name\tMark\tOut of\tWeight\tTotal");   
        }

        Subject[i].total = ( (Subject[i].mark/Subject[i].outof) * Subject[i].weight );

        printf("\n%s\t%2.1lf\t%2.1lf\t%2.2lf\t%2.2lf", Subject[i].name, Subject[i].mark,
            Subject[i].outof, Subject[i].weight, Subject[i].total);
    }
    /*---------------------------------------------------------------------------*/       
    n = i;
    for( i = 0; i < n; i++) {
        total += Subject[i].total;
    }

    printf("\nTotal: %3.2lf\n\n", total);
    /*---------------------------------------------------------------------------*/        
 i= 1;
         while(i<=10){
for( grade = 23; grade <= 60; grade++) {
    result = (grade / Subject[i-1].outof) * Subject[i-1].weight;
    printf("%lf\n",result);
    if((total + result) > P && (total + result - 1) < P)
        printf("minimum of %2.0lf is needed for FAIL\n", grade);
    else if((total + result) > H3 && (total + result - 1) < H3)
        printf("minimum of %2.0lf is needed for P\n", grade);
    else if((total + result) > H2b && (total + result - 1) < H2b)
        printf("minimum of %2.0lf is needed for H3\n", grade);
    else if((total + result) > H2a && (total + result - 1) < H2a)
        printf("minimum of %2.0lf is needed for H2b\n", grade);
    else if((total + result) > H1 && (total + result - 1) < H1)
        printf("minimum of %2.0lf is needed for H2a\n", grade);
    else if((total + result) > H1 && (total + result - 1) < H1)
        printf("minimum of %2.0lf is needed for H1\n", grade);
        else
        printf("None of the above cases pass\n");

}
i++;
 }
    printf("Out of Loop\n");
    /*---------------------------------------------------------------------------*/
    return 0;
}