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

****************************************************************************
#include <stdio.h>
#include <conio.h>

//Variable Declarations
int row, num, passing, aboveAv;

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

void main()
{
clrscr();

Initialize();
FindAverage();
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;
av = 0;
aboveAv = 0;

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

while (num > 10)
{
scanf("%i", &num);
if (num > 10)
printf("\nPlease enter a number less than 11:");
}
return;

Quote:
}

{
row = 0;
while(row < num)
{
row = row + 1;
}
return;

Quote:
}

//Function name:        FindAverage

void FindAverage()
{
row = 0;
while (row < num)
{
row = row + 1;
}
return;

Quote:
}

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

{
row = 0;
while(row < num)
{
aboveAv = aboveAv + 1;

passing = passing + 1;
row = row + 1;
}
return;

Quote:
}

//Function name:        DisplayResults
//                      and the # of grades higher than the average

void DisplayResults()
{
printf("\n\nThe grades you entered were:  ");
row = 0;
while(row < num)
{
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:

>****************************************************************************
>#include <stdio.h>
>#include <conio.h>

>//Variable Declarations
>int row, num, passing, aboveAv;

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

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

>        Initialize();
>        FindAverage();
>        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;
>        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;
>}

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

>//Function name:        FindAverage

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

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

>{
>        row = 0;
>        while(row < num)
>        {
>                aboveAv = aboveAv + 1;

>                passing = passing + 1;
>                row = row + 1;
>        }
>        return;
>}

>//Function name:        DisplayResults
>//                      and the # of grades higher than the average

>void DisplayResults()
>{
>        printf("\n\nThe grades you entered were:  ");
>        row = 0;
>        while(row < num)
>        {
>                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:
> {
>         row = 0;
>         while(row < num)
>         {
>                 printf("\nEnter grade #%i:  ", row+1);

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

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

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

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

To make the output more readable, confine the print of the grade to one

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

 Page 1 of 1 [ 3 post ]

Relevant Pages