Thread: frustrating professor

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Unhappy frustrating professor

    i've got a massive problem that i've been trying to independently learn how to figure out, as my prof explains so little information that's actually pertinent to the assigned homework!

    here's the problem, it seems like a dusey at first:

    The purpose of this homework is to reinforce ideas of decisions and loops, if else and while, and sequence.

    the professor keeps his grades in a file similar to the following

    0000 10 10 10 10 20 20 20 100 100 100
    1121 10 9 10 7 20 20 19 100 95 100
    3321 9 8 9 18 19 19 19 88 75 92
    etc,

    The first number is the student number. The next 10 numbers are scores the student has earned. The first line of the file is a line telling the total number of points possible for each of the exercises. You can tell this line since it has a student number of 0000. We know that Dr. Eggen has exactly 8 students in his class. Your task is to read the information from the file using unix redirection of stdin, and calculate an average score for each of the students, points earned divided by points possible. You then suggest a grade for that student, using the scale
    90-100 A
    80-89 B
    70-79 C
    60-69 D
    below 60 F

    You must make up a file of data containing the 8 students, and must print a suitably formatted output.

    i've already made the text file with made-up grades, but i can't for the life of me figure out how to incorporate that into the code of the c programme! here is what i've got so far:

    Code:
    #include <stdio.h>
    
    int main () 
    {
            int student;  //this is what i figured the 4 digit student id number would be in C
            int num1;    /*these int nums are what i thought would be the individual assignment grades for each student, each student being represented by the four digit number in the text file */
            int num2;
            int num3;
            int num4;
            int num5;
            int num6;
            int num7;
            int num8;
            int num9;
            int num10;
            int sum;
            sum=num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
            float avg;
            avg=(sum) / 400;
            float score;
            score=avg * 100;
    
            scanf("%d", &student);
            scanf("%d", &num1);
            scanf("%d", &num2);
            scanf("%d", &num3);
            scanf("%d", &num4);
            scanf("%d", &num5);
            scanf("%d", &num6);
            scanf("%d", &num7);
            scanf("%d", &num8);
            scanf("%d", &num9);
            scanf("%d", &num10);
    
            printf("Average is: %f\n", &avg);
            printf("Score is %f\n", &score);
            if (score >= 90) 
            {
                    printf("A");
            }
            else {
                    if (score >=80)
                            printf("B");
            }
            else {
                    if (score >=70)
                            printf("C");
            }
            else {
                    if (score >=60) 
                            printf("D");
            }       else {
                            printf("F");
            }
    
       /* everything ok return without error */
       return 0;
    }
    that's a direct copy-paste.
    he wants me to use the a.out < txtfile command in unix, but i need to know how to make c parse the text file correctly! please somebody help me. i've been slaving over this for six straight hours. i'm tearing my hair out over my teacher's lack of eponymous duty!!!

    ANY help is greaaatly appreciated, thank you
    Last edited by vwy; 02-07-2006 at 03:10 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > sum=num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8;
    You need to do this AFTER you have input the values, not before.
    Likewise for your calulation of average

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Quote Originally Posted by Salem
    > sum=num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8;
    You need to do this AFTER you have input the values, not before.
    Likewise for your calulation of average
    thanks for your reply.
    the values are in the separate text file with the ten grades for each student, or so i thought.
    i'm sorry if i misunderstood, but there is an extremely high probabliliyt that i will. it's 313 here and i'm a complete newbie to this. please

  4. #4
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by vwy
    thanks for your reply.
    the values are in the separate text file with the ten grades for each student, or so i thought.
    i'm sorry if i misunderstood, but there is an extremely high probabliliyt that i will. it's 313 here and i'm a complete newbie to this. please

    What Salem is saying is that you have created your sum before ANY of your num variables have had any data put into them. If this compiled and ran, you'd have whatever random data was in the variables being added up into sum.

    You need to put that sum statement after the scanf statements.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Yeah I'm going to second what everyone else said. What you've done is calculated the sum and average and the score of the numbers before you've actually determined what those numbers are. What you need to do is take the sum= line and the avg= line and the score= line and put then all between the scanf's and the part of the code where you print the results. After you've done that you can continue with whatever other questions you have.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Quote Originally Posted by CrazedBrit
    Yeah I'm going to second what everyone else said. What you've done is calculated the sum and average and the score of the numbers before you've actually determined what those numbers are. What you need to do is take the sum= line and the avg= line and the score= line and put then all between the scanf's and the part of the code where you print the results. After you've done that you can continue with whatever other questions you have.
    ok, i've followed directions and i get a syntax error when i try to compile it:
    [vyanta@Xena01 CStuff]$ gcc Homework3.c
    Homework3.c: In function ‘main’:
    Homework3.c:45: error: syntax error before ‘else’
    [vyanta@Xena01 CStuff]$

    here is my newly edited code that gives this error:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main () 
    {
            int student;
            int num1;
            int num2;
            int num3;
            int num4;
            int num5;
            int num6;
            int num7;
            int num8;
            int num9;
            int num10;
            int sum;
            float avg;
            float score;
    
            scanf("%d", &student);
            scanf("%d", &num1);
            scanf("%d", &num2);
            scanf("%d", &num3);
            scanf("%d", &num4);
            scanf("%d", &num5);
            scanf("%d", &num6);
            scanf("%d", &num7);
            scanf("%d", &num8);
            scanf("%d", &num9);
            scanf("%d", &num10);
            avg=(sum) / 400;
            sum=num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
            score=avg * 100;
            printf("Average is: %f\n", &avg);
            printf("Score is %f\n", &score);
             if (score >= 90)
            {
                    printf("A");
            }
            else {
                    if (score >=80)
                            printf("B");
            }
            else { /*this is line 45 which gives the syntax error */
                    if (score >=70)
                            printf("C");
            }
            else {
                    if (score >=60)
                            printf("D");
            }       else {
                            printf("F");
            }
       /* everything ok return without error */
       return 0;
    }
    i have spent well over 10 hours on this problem and have all but given up, so whatever you can advise is well appreciated.
    thank you.
    Last edited by vwy; 02-07-2006 at 06:30 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    An if/else chain works like so:
    Code:
    if( this )
    {
    }
    else if( that )
    {
    }
    ...repeat...
    You have:
    Code:
    if( this )
    {
    }
    else
    {
        if( that )
        {
        }
    }
    else /* <--- See? You have if-else-else. */
    You want:
    Code:
    if ...
    {
    }
    else if ...
    {
    }
    else if ...
    {
    }
    else /* one final else, with no if */
    {
    }
    Also, you have your sum lines backwards. You aren't assigning a formulae to a variable. That is to say, if you apply some maths to a variable, any time you do anything in the future with that variable, it doesn't take that same maths and compute it with new numbers. See:
    Code:
    avg=(sum) / 400;
            sum=num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
            score=avg * 100;
    The top line is dividing whatever random value is in sum by 400 and sticking that in avg. You haven't however actually used sum first. You use it next, which has no effect on the previous line.

    You have to think in a linear manner for most everything here. What comes first happens first.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    In addition to quzah's comments you also need to do some typecasting. If you don't understand this properly it's best to try and find an online tutorial (google) or get a book about the c language. In the line where you calculate your average, you need to typecast the sum as a float, otherwise your average will always be some number with a decimal of .00000000
    Code:
    avg=(float)sum / 400;
    this is what I mean by that.
    Good Luck
    -Crazed

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    thank you, that cleared up a lot.
    i was able to compile the programme, but now whenever i try to combine the text file with the a.out, i get this:

    [@Xena01 CStuff]$ a.out < file4hw2
    Average is: 0.000000
    Score is 0.000000

    the content of the text file with arbitrary "grades" is as follows:

    0000 10 10 10 10 20 20 20 100 100 100
    1121 10 9 10 7 20 20 19 100 95 100
    3321 9 8 9 8 19 19 19 88 75 92
    2121 7 8 6 7 17 18 12 85 94 78
    2211 9 9 9 8 19 17 19 98 95 100
    3221 8 8 7 3 12 18 17 91 74 98
    1179 6 9 6 8 17 19 16 94 96 91
    2541 9 10 10 9 20 19 19 98 99 100
    1511 5 4 4 6 3 12 11 16 71 68 79

    the four digit number on the left is the student ID, the top line is the maximum total a student can get, so 0000 is not a student. the rest are students, 8 of them.
    i feel like i've asked too much of you, but half a day's worth of research has yielded little!
    thanks again

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So let's see your latest code. What you have doesn't do anything with a file.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Did you include the typecasting I mentioned in my above post? Because if you didn't the program is probably getting a decimal, such as 0.899302 or whatever for the average and then rounding it down to 0.000000. This will then be multiplied by 100 to get a score of 0.000000. Like Quzah said, post your code and we can take a look.
    -Crazed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Frustrating Error Message!!! (for a function)
    By Holtzy in forum C Programming
    Replies: 3
    Last Post: 03-13-2008, 05:03 PM
  2. Frustrating Error
    By ZWarren69 in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2005, 02:39 AM
  3. Ask Professor Giraffenstein! (Contains some NSFW material)
    By Glirk Dient in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 08-20-2005, 10:23 PM
  4. Highly Frustrating Problem
    By Eminence in forum C++ Programming
    Replies: 7
    Last Post: 09-09-2002, 12:32 PM
  5. Basic Shell Script-Very Frustrating
    By kwigibo in forum Linux Programming
    Replies: 8
    Last Post: 05-19-2002, 02:43 AM