tags:

views:

117

answers:

4

First of all this is homework. I have been trying to get rid of the errors and usually screw the code up farther and end up coming back to the original code. If you could please give me some direction (in terms I can understand) I would really appreciate it. I do not think our instructor actually expects us to get it, but I still have to try. I am posting now, but will be away from the computer for a while, so I can not comment back right away, Thank you.

#include <stdio.h> 

#define MAX 10

main () {
  int i; 
  int num_students; 
  float total;
  long id[MAX];                // Student ID
  short midterm[MAX];          // Midterm Grade
  short final[MAX];            // Final Grade
  float average[MAX];          // Average Grade
  float avg_average;           // Average of the averages
  float diff;                    // Student's average minus class average

  // The average grade is 60% final and 40% midterm
  // Compute the course grades, the average course grade,
  // and for each student, print out their id, midterm and final grades,
  // the average grade, and their difference

  i = 0;
  // The syntax of this while loop is correct
  while (scanf("%i %i %i",&id[i], &midterm[i], &final[i]) != EOF) {
    average[i] = midterm[i]*.4 + final[i]*.6;
    i++;
  }

  num_students = i;

  for (i = 0; i < num_students; i++) {
    total = total + average[i];
  }
  avg_average = total/num_students;

  printf("ID\tMidterm\tFinal\tAvg\tAvgAvg\tDiff\n");
  printf("---\t--------\t------\t----\t-------\t-----\n");
  for (i = 0; i < num_students; i++) { 
    diff = avg_average - average[i];
    printf("%d\t", id[i]);
    printf("%i\t", midterm[i]);
    printf("%i\t", final[i]);
    printf("%f2.2\t", average[i]);
    printf("%f2.2\t",avg_average);
    printf("%f2.2\t",diff);

    printf("\n");
  }

This is the data file I am passing in:

0 94 93 
1 88 84
2 99 79
3 78 64
4 66 83
5 78 90
6 90 87
7 23 18
8 55 76
9 48 26
10 87 67
11 97 89
12 100 89
13 88 78
14 67 78
15 87 76
16 77 65
17 83 87
18 89 87

I just do not get the right output in any of the categories.

+2  A: 

One of the issues is that you're truncating and doing integer division:

average[i] is a float, but you're indirectly assigning it to total, which is an int, which causes truncating. This also means you're later doing integer division.

Change total to be a float.

You're also not initializing it, so do:

float total = 0;
Matthew Flaschen
Why did you choose do multiply by `1.0` instead of casting `total` to `float`? Is there an advantage here that I am missing?
Jack Kelly
@Jack, a cast is fine too; there's no special advantage. Anyway, I've since changed it, since `total` itself should be a float.
Matthew Flaschen
ok that makes sense
pisfire
I made that change.
pisfire
@pisfire, you're not initializing `total` to 0 in the above code. There are compiler options to warn for this (what compiler are you using?).
Matthew Flaschen
+3  A: 

Here's another issue.

scanf returns the number of successful conversions from the standard input, or EOF. But what happens when you input a blank line, or invalid input?

e.g. entering the following

"50 80 60" - this line OK
"garbage 3 7"  - scanf returns 2. 1st conversion fails. id[i] could contain anything.

What you want to do is modify your program to check that scanf returns 3 or EOF and not just EOF. If it didn't return either of those then you need to print out an error ask for input again.

Since it's homework I'm going to let you work that bit out.

Matt H
I am sorry I do not understand, but I will think about it for now.
pisfire
A: 

Ok, the biggest problem was #define max was not equal to 10, this screwed up most of the answers.

pisfire
A: 

My attempts at trying to fix your program:

  1. http://ideone.com/b8LT1 Your code with a final } added
    compilation errors don't post code that doesn't compile!
  2. http://ideone.com/c8TNa added return type int to main and changed variables to int Runtime error (SIGSEGV) -- Probably the program tries to read too many lines
  3. http://ideone.com/iD4Lk changed MAX macro definition
    It runs. It outputs stuff! But the formats are all messed up
  4. http://ideone.com/ZrZMZ looks good now :-)

You still need to see if the output is correct and fix any logic errors if it isn't.

pmg