-1

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;
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
LollyW
  • 53
  • 1
  • 7
  • 2
    1) `for( i = 0; i <= n; i++) {` --> `for( i = 0; i < n; i++) {` 2) `result = (grade / Subject[i-1].outof) * Subject[i-1].weight;` : `i` is wrong value. – BLUEPIXY Nov 16 '14 at 10:40
  • @BLUEPIXY Thanks! Fixed those errors, however neither of them help the runtime at all. This question has been stressing me out all day! Any idea why it's doing what it's doing or rather not doing? – LollyW Nov 16 '14 at 10:47
  • @LollyW You have you failure condition in the for loop as `scanf("%s %lf %lf %lf", &Subject[i].name,&Subject[i].mark,&Subject[i].outof,&Subject[i].weight)) == 4` why not just `i – Gopi Nov 16 '14 at 10:49
  • @Gopi I've been taught that framework so that the for loop carries on working until the user stops inputting data. The problem I'm finding is that the for loop works fine but nothing outside the for loop seems to work which is confusing me, because it all worked when I hard coded the information. That framework I've used many times without any trouble. – LollyW Nov 16 '14 at 10:57
  • @LollyW So you mean you enter MAX(10) number so of items to your structure and later after calculating total you set `i=1`? Are you doing this? – Gopi Nov 16 '14 at 11:05
  • @Gopi I'm not really sure I understand your question. The variable i is reused after the for loop as it's just a counting or 'buddy' variable. Does that answer your question? The user has to input a string then three doubles otherwise the guard/failure condition is broken. – LollyW Nov 16 '14 at 11:09
  • Compile with all warnings and debug info (`gcc -Wall -Wextra -g`). Then **use the debugger** (`gdb`) to understand how and why the program misbehave. – Basile Starynkevitch Nov 16 '14 at 11:36
  • @LollyW Check the below code there was no break condition in your loop that's what I wanted to convey – Gopi Nov 16 '14 at 11:41
  • @BasileStarynkevitch I'm already compiling with -Wall and compiling with both -Wall and -Wextra hasn't given me any new bugs. – LollyW Nov 16 '14 at 11:43
  • 1
    @LollyW a bug is a runtime error. A compiler error is not a bug. Also, please include all pertinent information *in the question itself*. Then, as Basile suggested, learn how to use a debugger. It will make your life infinitely easier. – dandan78 Nov 16 '14 at 11:48
  • @dandan78 This is my first question here, although I use the website a lot to help with my studies so wasn't really sure what to include in the question, I put all the information I thought was pertinent. I'm looking into the debugger right now. – LollyW Nov 16 '14 at 11:52
  • @user3121023 That's how I've been ending it, because otherwise the program will carry on waiting for input from the user, however the rest of the program should execute, but it doesn't! Very irritating! – LollyW Nov 16 '14 at 12:01
  • You must enter a `CTRL+Z` for the end of the input.(instead of `CTRL+C`) – BLUEPIXY Nov 16 '14 at 12:31
  • [RUN DEMO](http://ideone.com/S1HyUS) – BLUEPIXY Nov 16 '14 at 12:38
  • You need to learn how to use a debugger. Debugging cannot be fully & reliably automatized (read about the [halting problem](https://en.wikipedia.org/wiki/Halting_problem)...). So you need to think on your program with the help of the debugger. – Basile Starynkevitch Nov 16 '14 at 12:43
  • or checked `mark==0` as a termination condition for the loop. – BLUEPIXY Nov 16 '14 at 12:46

1 Answers1

0

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;
}
Community
  • 1
  • 1
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • it's still not working for me. I'm getting the exact same output. Are you getting [this](http://i1374.photobucket.com/albums/ag407/laura_worger/blah2_zps9a33bcd9.png) output? – LollyW Nov 16 '14 at 11:47
  • @LollyW I don't see any of you cases passing in the for loop but it is coming out of the loop. One more thing you are checking int with double make sure that is fixed – Gopi Nov 16 '14 at 11:56
  • The picture I linked is from the hardcoded version so that SHOULD be the output I get when I run the program and then input the data via the command prompt, but it isn't. – LollyW Nov 16 '14 at 12:12
  • @LollyW Please check the above code now. It should yield you proper results. – Gopi Nov 16 '14 at 17:52