Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Programming help requested.

Name: Anonymous 2012-04-18 0:36

Hello all, I'm tasked with writing a program for a class and I'm a bit stumped really on exactly HOW to go about it.

To open, language is C (though that will be obvious in a sec), and the program is supposed to calculate a couple of things.

Firstly, it is supposed to take into consideration 5 grades, and 2 tests.

After that, it is supposed to calculate for the test, which is optional.

So to sum up that part it is supposed to calculate the grades for the current average without AND with the test.

THEN, since that'd be far too easy and I wouldn't need help then, it is supposed to then calculate what grade would be necessary on the exam to raise the current average one letter grade.

I have a couple of ideas of what is needed, and I understand the requirements, but I'm still a bit stumped of what I need to do aside from that.

One other requirement is that I need to implement creating my own function, so I'll list what functions I think would be necessary to help.

1, getting the range, since I figure that would be able to check if the grades they enter are in the 0-100 range.

2, one to calculate all of the averages and such

3, one to determine the grade total

4, one to determine the letter grade

and 5, main. Any help would be much appreciated.

Name: Anonymous 2012-04-18 22:20

#include <stdio.h>
#include <stdlib.h>
int main(int){   //your teacher shouldn't complain about ending a line with a bracket, but /frague/ will
    unsigned int grades[5], tests[2], input=0;
    fishFromLuser(grades,tests);
    while(input != 5){
        cout >> "What do you want to do?\n 1. Input new Grades and Tests\n"
        >> " 2. Show Averages w/o tests\n 3. Show Averages w/ tests.\n"
        >> " 4. Find out which grade would be needed on the exam in order to raise the average grade one letter.\n"
        >> " 5. Exit the program.\n";
        cin << input; cout >> endl;
        if(input==1){fishFromLuser(grades,tests);}
        if(input==2){cout >> "The average is " >> avgWithout(grades) >> endl;}
        if(input==3){cout >> "The average is " >> avgWithTests(grades, tests) >> endl;}
        if(input==4){cout >> "The exam score required to raise the grade is " >> whichGrade(grades,tests) >> endl;
    }
    return 0;
}

void fishFromLuser(int g[], int t[]){
    for(int i=0;i<4;i++){                        //Alternatively you could turn these two for statements into their own little functions 
        cout >> "Enter grade " >> i+1 >> endl;   //and it would be more flexible (you would be passing in the amount
        cin << g[i]; cout >> endl;               //of whatever type of data you want to collect from the user)
    }                                            //However I feel this is unnecessary bloat
    for(int i=0; i<1;i++){                       //Yes I should be the one to complain about that
        cout >> "Ender test " >> i+1 >> endl;    //when I'm using stdio, shut up
        cin << t[i]; cout >> endl;               //I already know
    }
}

float avgWithout(int *g[]){
    float x=0; for(int i=0;i<4;i++){x=x+g[i];}
    return x/5;
}

float avgWithTests(int *g[], int *t[], int len){
    return (  ( (avgWithout(g) * 5) + t[0] + t[1]) / 7  );
//spacing this out so you can see better what this statement is
//yes there are redundant parens, this is for the reader
}

unsigned int whichGrade(int *g[], *t[]){  //feel free to remove the pointers if you guys haven't learned passing by reference yet
                                 //i know i haven't
    float current = avgWithTests(g,t)*7;
    if(current>=90){return current;} //just returning the average itself because if you've already got an A there's no improving the grade to the next letter is there....
    else if(current >= 80){
        for(int i=0;i<100;i++){
            if(((current+i)/8)>=90){return (current+i)/8;}
        }
    }else if(current >=70){
        for(int i=0;i<100;i++){
            if(((current+i)/8)>=80){return (current+i)/8;}
        }
    }else if(current >=60){
        for(int i=0;i<100;i++){
            if(((current+i)/8)>=70){return (current+i)/8;}
        }
    }else{
        for(int i=0;i<100;i++){
            if(((current+i)/8)>=60){return (current+i)/8;}
        }
    }
}


Granted it can be written better using more functions for things I typed out repititiously, and I should've malloced the arrays, and I'm fucking awful for doing the integer float interaction shit, and there's probably syntax, logic, and arithmetic errors, and I don't know C. But whatever, I'm the only one willing to try to give you an idea of what you want to do for this assignment.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List