Thread: Trouble Understanding the assignment given.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    Trouble Understanding the assignment given.

    I am having a hard time understanding exactly what is being asked. These are the instructions. Below the instructions is my previous program. How do I use the *FILE?

    For this program, you are going to modify your previous program (program 3) so that it will now have a menu to see if the user wants to read the input from a file or interactively. If the user wants to read the input from a file, then the output will also go into a different file. If the user wants to read the input interactively, then the output will go to the screen.

    Your initial menu will look like the following:
    Menu
    <1> Read data from a file
    <2> Read data interactively
    Enter choice:

    Here is a sample input file:

    21 25
    30 30
    10 12
    55 60
    -1 /* this is the flag to end inputting the assignment data */
    21 25
    30 30
    10 12
    21 25
    30 30

    Here is another sample input file:

    21 25
    30 30
    10 12
    55 60
    -1 /* this is the flag to end inputting the assignment data */
    21 25
    -1 /* this is the flag to end inputting the midterm exam data */
    21 25
    30 30

    Code:
    #include <stdio.h>
    float Homework() //Function for Homework grades 
    {    
        int HWTotal=0; //The total amount of points that have been earned in homework
        int HWAssignment=0; //The amount of points the assignment is worth
        int HWAssignmentTotal=0;//The net total of assignment points
        int HWInput=0;//User inputs the grade for homework
        float Final_HWGrade; //Calculated Homework average
        int n=1; //Count for assignment
    
        printf("Type -1 when you are done entering grades \n");
        //Loop for Homework assignments enter grades until -1 is inputed
        while  (HWInput != -1)
        { 
            printf("\nWhat is the grade for Assignment %d ?  ", n);
            scanf("%d", &HWInput);
    
            //If a -1 is entered and no grades have been entered total homework score is 0
            if (HWInput == -1 && HWAssignmentTotal==0)
            
            {
                printf("No Homework grades were entered for Homework \n");
                return 0.0;
            
            }
            
            //If there is a negative number other than -1 that is entered error
            else if (HWInput != -1 && HWInput < 0)
            {
                printf("Error: No Negative Grades!\n Please close and try again.");
                return (0);
            
            }
            
            //If grades are still being inputed this asks for assignment points and adds all points possible
            //until the user inputs -1 which will stop the loop
            else if (HWInput != -1)
            {    
                printf("What is the total amount of points in the Assignment %d?  ",n);
                scanf("%d", &HWAssignment);
    
            
                //If inputed grade is higher than assignment networth then ERROR
                if (HWInput > HWAssignment)
                {
                    printf("Error: Grade is Higher than Total Possible Points! \n No extra credit available! Close and try again \n");
                    return 0;    
                
                }
    
    
                HWAssignmentTotal+=HWAssignment;//Calculates Homework points earned
                HWTotal+=HWInput;//Calculates maximum of points available
    
            }
        
            n++; //incriment for Homework count
        }
        //Calculates the Homework Average
        Final_HWGrade=50.00*HWTotal/HWAssignmentTotal;
        
        return Final_HWGrade; //Returns Homework average
    }
    
    float Midterm() //Funtcion for midterm grades
    {
        int MidtermGrade=0; //Variable for user to input the Midterm grade
        int MidtermMaxGrade=0; //Maximum of total points that can be earned in the Midterm Assignment
        int Midterm_Total=0; //Total Points earned in Midterm
        int Midterm_Points=0;//Maximum number of points from all assignments
        int MidtermNumber=1; //Count for midterm
        float MidtermOverall_Grade;//Final Midterm Average
        
        //Loop for Midterm Inputs no more than 3 can be entered
        while (MidtermNumber <= 3)
        { 
            printf("\nWhat is the grade for Midterm %d? ", MidtermNumber);
            scanf("%d", &MidtermGrade);
            
            /*If a -1 is inputed and the Midterm_Total is 0 no grades have been entered and 
                0 points are earned in Midterm Average*/
            if (MidtermGrade == -1 && Midterm_Total == 0)
            {
                printf("No Midterm grades were entered\n");
                return 0.0;
            
            }
    
            //If user inputs a number smaller than 0 other than -1 ERROR
            else if(MidtermGrade != -1 && MidtermGrade < 0)
            {
            
                printf("Error: No Negative Grades!\n Please close and try again.");
                return (0);
            
            }
    
            /* If there you want to input 1 or 2 grades and input -1 this lets the program know 
                that only a certain number of grades are to be calculated*/
                
            else if (MidtermGrade == -1)
            {
            MidtermOverall_Grade=20.00*Midterm_Points/Midterm_Total;
            return MidtermOverall_Grade;
            }
    
            else
            {
            printf("What is the total amount of points in Midterm %d? ", MidtermNumber);
            scanf("%d", &MidtermMaxGrade);
    
                if (MidtermGrade > MidtermMaxGrade)
                {
                    printf("Error: Grade is Higher than Total Possible Points! \n No extra credit available! Close and try again \n");
                    return 0;    
                
                }
    
    
    
            Midterm_Points+=MidtermGrade; //Calculates total points earned
            Midterm_Total+=MidtermMaxGrade; //Calculates max number of points available
    
            MidtermNumber++; //Incriment for Midterm Count
            }
        }
    
        MidtermOverall_Grade=20.0*Midterm_Points/Midterm_Total; //Calculates Midterm average
    
        return MidtermOverall_Grade; //Returns Midterm Average
    }
    
    float FinalExam() //Function for Final Exam
    {
        int FinalInput;//Input for Final grade
        int FinalTotalPoints=0; //Maximum of total points available for assignments
        int FinalPoints=0; //Maximum points available for Assignment
        int FinalPointsEarned=0;//Total points earned in assignments
        float FinalTestAvg;//Average for Final Exam
        int FinalCount=1;//Count for Final Exam
        
        //Loop for Final Exam. Loops out when it reaches 2 grades
        while (FinalCount <=2)
        {
        //Asks user for Final Grade
        printf("\nWhat is the grade for Final Part %d?  ", FinalCount);
        scanf("%d", &FinalInput);
        
        //If grade inputed is a negative Error occurs
        if (FinalInput < 0)
        {
            printf("Error: Grade cannot be Negative! \n Please close and try again \n");
            return 0;
        
        }
        //Ask user for maximum points available
        printf("What is the total amount of for this part of the Final?  ");
        scanf("%d", &FinalPoints);
        
        FinalPointsEarned+=FinalInput; //Calculates total of points that have been earned
        FinalTotalPoints+=FinalPoints; //Calculates total of points available
        
        //If Points earned is greater than maximum points in an assignment ERROR.
        if (FinalPointsEarned > FinalTotalPoints)
        {
            printf("Error: Grade is Higher than Total Possible Points! \n No extra credit available! Close and try again \n");
            return 0;    
        }
    
        FinalCount++; //Count for Final Exam
        }
        
        //Calculates Final Exam Average
        FinalTestAvg=30.00*FinalPointsEarned/FinalTotalPoints;
        
        return FinalTestAvg; //Returns the Final Exam Average
    }
    
    char FinalGrade( float FinalExamAvg, float MidtermAvg, float HWAverage)
    /* Once all grades have been calculated this function takes in values from previous functions
        and calculates the final overall score and then determines what letter grade is earned
    */
    {    
        char LetterGrade; //Variable to recieve what letter grade will be earned
        float FinalOverall_Avg;//Variable to recieve all grades added together
        
        FinalOverall_Avg= HWAverage + MidtermAvg + FinalExamAvg; //Calculates the overall grade
    
        /* This system of if statements determines what letter grade will be earned based off of the number FinalOverall_Avg variable 
        recieves. 
        If the Final Grade is Higher than or equal to a 90 then LetterGrade is given the character 'A'.
        If the Final Grade is between a 89 and 80 then LetterGrade is given the character 'B'.
        If the Final Grade is between a 79 and 70 then LetterGrade is given the character 'C'.
        If the Final Grade is between a 69 and 60 then LetterGrade is given the character 'D'.
        If the Final Grade is less than 60 then LetterGrade is given the character 'F'.
        
        */
        if (FinalOverall_Avg >=90)
        {
            LetterGrade='A';
            return LetterGrade;
        }
    
        else if (FinalOverall_Avg <=89 && FinalOverall_Avg >=80)
        {
            LetterGrade='B';
            return LetterGrade;
        }
        
        else if (FinalOverall_Avg <=79 && FinalOverall_Avg >=70)
        {
            LetterGrade='C';
            return LetterGrade;
        }
    
        else if (FinalOverall_Avg <=69 && FinalOverall_Avg >=60)
        {
            LetterGrade='D';
            return LetterGrade;
        }
    
        else
        {    
            LetterGrade='F';
            return LetterGrade;
        }
    
        
    }
    
    void Outputs(float HWFinal, float MidtermFinal, float FinalTestFinal, char Grade_Letter)
    //This function takes all return values for previous functions and is then outputted with their respective Assignments
    {
        float AverageGrade;
        AverageGrade=HWFinal+MidtermFinal+FinalTestFinal;
        printf("\nHomework Assignments Averge: %.2f\n",HWFinal);
        printf("Midterm Averge: %.2f\n", MidtermFinal);
        printf("Final Test Averge: %.2f\n", FinalTestFinal);
        printf("--------------------\n");
        printf("Final Grade: %.2f\n", AverageGrade);
        printf("You made a %c in the class.\n", Grade_Letter);
    
        return ;
    }
    int main()
    {
            printf("Menu\n");
            printf("<1> ");
            printf("");
            printf("");
    
    
            float HomeworkAverage= Homework();
            float MidtermAverage= Midterm();
            float FinalAverage= FinalExam();
            char OverallLetter_Grade= FinalGrade(HomeworkAverage, MidtermAverage, FinalAverage);
            Outputs(HomeworkAverage, MidtermAverage, FinalAverage, OverallLetter_Grade);
            
            
            return 0;
    }
    -Thank You for your time

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Google "C File I/O".
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The top half of this is all you need for text files:
    C File I/O Tutorial - Cprogramming.com

    Your output function will need to be passed a variable to indicate whether the output should go to a file, or to the screen. That would be a parameter you pass to the output function, and then inside the output function, you'd have an if statement:

    With pseudo code logic like:
    Code:
    if(variable printToFile) 
        fprintf(etc.... to the file)
    else
        printf(etc.... to the screen)
    There are more elegant ways to do this, but this is fine for a beginner.
    Last edited by Adak; 03-29-2012 at 03:11 AM.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by AlbyRang View Post
    I am having a hard time understanding exactly what is being asked.
    Did you check with your professor or TA?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble Understanding Structure Algorithm
    By JJohnson in forum C Programming
    Replies: 14
    Last Post: 10-19-2010, 01:27 AM
  2. Replies: 11
    Last Post: 09-25-2010, 02:00 PM
  3. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 12:44 PM
  4. a little trouble understanding
    By zema in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2003, 05:49 PM
  5. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 09:00 AM