HOMEWORK HELP, AGAIN!! 
Author Message
 HOMEWORK HELP, AGAIN!!

Thankyou for responding, Grego. the code you see is exactly what we are being
taught!

 The program runs now, but it still does not produce the correct output. I get
all zeroes in the DisplayResults function.
Any ideas?
I believe that, for some reason, the array ( grades) contains all zeroes?
This is my last program for the semester!!!!
Thanks,
Erik

****************************************************************************
//Header files
#include <stdio.h>
#include <conio.h>

//Variable Declarations
int row, num, passing, aboveAv;
float av, totGrades;
float grades[10];

//Function Prototypes
void Initialize();
void GetGrades();
void FindAverage();
void CountHighGrades();
void DisplayResults();

void main()
{
        clrscr();

        Initialize();
        GetGrades();
        FindAverage();
        CountHighGrades();
        DisplayResults();

        printf("\n\nPress any key to continue....");
        getch();
        return;

Quote:
}

//FunctionName: Initialize()
//Description:  Initializes program variables

void Initialize()
{
//initialize variables
        passing = 0;    
        num = 100;
        totGrades = 0.0;
        av = 0;
        aboveAv = 0;

// Prompt for and accept #of grades to be processed
// If # of grades exceeds 10, display error message and reprompt

    printf("Please enter the number of grades to be averaged [maximum 10]:");
                while (num > 10)
                {
                scanf("%i", &num);
                if (num > 10)
                printf("\nPlease enter a number less than 11:");
                }
return;

Quote:
}

//Function Name:        GetGrades
//Description:          Accept grade inputs

void GetGrades()
{
        row = 0;
        while(row < num)
        {
                printf("\nEnter grade #%i:  ", row+1);
                scanf("%i", &grades[row]);
                row = row + 1;
        }
        return;

Quote:
}

//Function name:        FindAverage
//Description:          Calculate the average grade

void FindAverage()
{
        row = 0;
        while (row < num)
        {
                totGrades = totGrades + grades[row];
                row = row + 1;
        }
        av = totGrades / num;
return;

Quote:
}

//Function name:        CountHighGrades
//Description:          Count the number of grades higher than the
//                      average and the number of passing grades.

void CountHighGrades()
{
        row = 0;
        while(row < num)
        {
                if(grades[row] > av)
                aboveAv = aboveAv + 1;

                if(grades[row] > 60.0)
                passing = passing + 1;
                row = row + 1;
        }
        return;

Quote:
}

//Function name:        DisplayResults
//Description:          Display the grades, average, # of passing grades
//                      and the # of grades higher than the average

void DisplayResults()
{
        printf("\n\nThe grades you entered were:  ");
        row = 0;
        while(row < num)
        {
                printf("%f     ", grades[row]);
                row = row + 1;
        }
        printf("\n\nThe average grade is %.2f", av);
        printf("\n\nThere are %i grades above the average.", aboveAv);
        printf("\n\nThere are %i passing grades.", passing);
return;

Quote:
}



Sun, 12 May 2002 03:00:00 GMT  
 HOMEWORK HELP, AGAIN!!

Quote:

>Thankyou for responding, Grego. the code you see is exactly what we are being
>taught!

> The program runs now, but it still does not produce the correct output. I get
>all zeroes in the DisplayResults function.
>Any ideas?
>I believe that, for some reason, the array ( grades) contains all zeroes?
>This is my last program for the semester!!!!
>Thanks,
>Erik

Erik
go back and re-read Gergo's post. There are several things  you have
NOT done which may prevent your program working.
Quote:

>****************************************************************************
>//Header files
>#include <stdio.h>
>#include <conio.h>

>//Variable Declarations
>int row, num, passing, aboveAv;
>float av, totGrades;
>float grades[10];

>//Function Prototypes
>void Initialize();
>void GetGrades();
>void FindAverage();
>void CountHighGrades();
>void DisplayResults();

>void main()
THIS IS INVALID C
>{
>        clrscr();

>        Initialize();
>        GetGrades();
>        FindAverage();
>        CountHighGrades();
>        DisplayResults();

>        printf("\n\nPress any key to continue....");

unless you put the \n at the END of the string instead of the start,
you may never see your message. DONT put them at the start.

- Show quoted text -

Quote:
>        getch();
>        return;
>}

>//FunctionName: Initialize()
>//Description:  Initializes program variables

>void Initialize()
>{
>//initialize variables
>        passing = 0;    
>        num = 100;
>        totGrades = 0.0;
>        av = 0;
>        aboveAv = 0;

>// Prompt for and accept #of grades to be processed
>// If # of grades exceeds 10, display error message and reprompt

>    printf("Please enter the number of grades to be averaged [maximum 10]:");
>                while (num > 10)
>                {
>                scanf("%i", &num);

What if they press 'Q' or type "ten". DONT use scanf. Its evil. Use
fgets and sscanf.
Quote:
>                if (num > 10)
>                printf("\nPlease enter a number less than 11:");

String won't be printed unless you send \n at the end...

- Show quoted text -

Quote:
>                }
>return;
>}

>//Function Name:        GetGrades
>//Description:          Accept grade inputs

>void GetGrades()
>{
>        row = 0;
>        while(row < num)
>        {
>                printf("\nEnter grade #%i:  ", row+1);
again....
>                scanf("%i", &grades[row]);
.... AGAIN
>                row = row + 1;
>        }
>        return;
>}

>//Function name:        FindAverage
>//Description:          Calculate the average grade

>void FindAverage()
>{
>        row = 0;
>        while (row < num)
>        {
>                totGrades = totGrades + grades[row];
>                row = row + 1;
>        }
>        av = totGrades / num;
>return;
>}

>//Function name:        CountHighGrades
>//Description:          Count the number of grades higher than the
>//                      average and the number of passing grades.

>void CountHighGrades()
>{
>        row = 0;
>        while(row < num)
>        {
>                if(grades[row] > av)
>                aboveAv = aboveAv + 1;

>                if(grades[row] > 60.0)
>                passing = passing + 1;
>                row = row + 1;
>        }
>        return;
>}

>//Function name:        DisplayResults
>//Description:          Display the grades, average, # of passing grades
>//                      and the # of grades higher than the average

>void DisplayResults()
>{
>        printf("\n\nThe grades you entered were:  ");
>        row = 0;
>        while(row < num)
>        {
>                printf("%f     ", grades[row]);
>                row = row + 1;
>        }
>        printf("\n\nThe average grade is %.2f", av);
>        printf("\n\nThere are %i grades above the average.", aboveAv);
>        printf("\n\nThere are %i passing grades.", passing);
>return;
>}

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Tue, 14 May 2002 03:00:00 GMT  
 HOMEWORK HELP, AGAIN!!

Quote:

> Thankyou for responding, Grego. the code you see is exactly what we are being
> taught!

The problem, the code is not C and will not compile on an ANSI C compiler.That
aside, there is one simple correction that will probably make the code run as you
expect.

;;;;;;;;;;;;;;snip;;;;;;;;;;;;;;;;;

Quote:
> void GetGrades()
> {
>         row = 0;
>         while(row < num)
>         {
>                 printf("\nEnter grade #%i:  ", row+1);
>                 scanf("%i", &grades[row]);

This scanf statement is flawed and you and others did not catch the flaw because
grades[] is a global variable. Global variables are notoriously difficult to keep
track of. That is one reason why you had the suggestion of doing away with
globals.

Anyway, the grades[] is an array of type float. The conversion character you are
using in scanf is 'i' which specifies a type int pointer. For the float pointer,
&grades[row], you need to use the conversion character 'f'. So, this statement
should read:

scanf(("%f",&grades[row]);

Quote:
>                 row = row + 1;
>         }
>         return;
> }

;;;;;;;;;;;;;snip;;;;;;;;;;;;;;;;

Quote:
> void DisplayResults()
> {
>         printf("\n\nThe grades you entered were:  ");
>         row = 0;
>         while(row < num)
>         {
>                 printf("%f     ", grades[row]);

To make the output more readable, confine the print of the grade to one
decimal.printf("%.2f   ",grades[row[);

Quote:
>                 row = row + 1;
>         }
>         printf("\n\nThe average grade is %.2f", av);
>         printf("\n\nThere are %i grades above the average.", aboveAv);
>         printf("\n\nThere are %i passing grades.", passing);
> return;
> }

This scanf correction should make the code work, as long as you input correctly.
e.g., input an int for the number of grades, and input a number not greater than
10, and input a float number for the grade input. You really need to address these
problems also.

--
Al Bowers
Tampa, FL  USA

http://www.gate.net/~abowers/



Tue, 14 May 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. I NEED HELP WITH MY HOMEWORK AGAIN!!!

2. Help with c homework (again)

3. homework help

4. help with java homework

5. Homework Help

6. HELP! Homework re:Strings

7. Help with c homework

8. homework help scanning info from disk?

9. Help woth homework due tonight.

10. I will pay for homework help !HELLLPPPP !!!HELLLPPPP !!!

11. help: process management homework

12. Help With homework please(yes i have code posted)

 

 
Powered by phpBB® Forum Software