Solved

How to write the HIGHEST part of the code? A.S.A.P

Posted on 2001-06-07
8
243 Views
Last Modified: 2012-05-04
I have a problem with my code and I need some help to figure out the code. The purpose of the code is to enter the grades and display the overall average.  Also to display the highest grade entered.  the output of the code should show the overall average of the grades entered and the highest grade entered... my biggest problem is I can't figure out the code to display the hightest grade ... Please give suggestions or please fix the code/
thank you
 
 
 
#include <stdio.h>
#define HIGHEST 90
int highest[HIGHEST];
int  main()
{
   float average;              /* new data type */
   int counter, grade, total, highest;
   /* initialization phase */
   total= 0;
   counter= 0;
   highest=80;
   /* processing phase */
   printf( "Enter grade, -1 to end: " );
   scanf( "%d", &grade );
   printf("High grade");
   scanf("%d", &highest);

   while ( grade != -1 ) {
      total = total + grade;
      counter = counter + 1;
      printf( "Enter grade, -1 to end: " );
      scanf("%d", &grade);
  }
  while(grade > highest) {
   grade=highest;
   printf("High");
   scanf("%d", &highest);
  }

   average = ( float ) total / counter;
      printf( "Class average is %.2f", average );
   return(highest);
         }
0
Comment
Question by:Nancy
8 Comments
 
LVL 2

Accepted Solution

by:
agriggs earned 50 total points
ID: 6166534
I will only give pseudocode.  But first, why do you initialize highest to 80?  I would suggest initializing it to zero.

I am also going to make an assumption, that you don't really want the user to enter the highest grade in the class and the program just to spit that back out.  You probably want the user to enter all the grades and the program to figure out which is highest and spit that back out.  If so, then every time you do a scanf("%d", &highest) needs to be removed.

Within your while loop which is allowing user to enter the grades, and accumulating total, you also need to place an if statement to check and see if the grade just entered is greater than your current highest value.  If it is, then replace the value of the highest with the value of the last grade entered.

You have coded a second while statement, which it appears was intended to do what I just suggested.  However, you need an if statement, not a while statement.  A while statement starts a loop going.  And like I said that if statement needs to be within the confines of your other while loop.

I hope I have helped.  Please continue to ask questions if you need any additional assistance.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6168744
1) In your global scope you have a definition: "int highest[HIGHEST]; ". It seems you originally wanted to save something (the grades?).
2) agriggs is right, highest should be initilized with the lowest possible grade: 0, because it is growing over time!
3) The "while ( grade != -1 ) " is the place you can do all work:
- Read the next grade.
- Check if the new grade is a new "highest grade"
- Add up all grades (think about overflows, what is the highest int in your system?)
- Is the last entered grade handled right? (Tip: do it by hand for 2 or three values)
4) Your second loop is not needed anymore.
5) print result(s).
6) Good luck with your homework. Sorry, no code!

======
Werner

0
 
LVL 1

Expert Comment

by:jdrescher
ID: 6332370
#include <stdio.h>
int  main()
{
     float average;              /* new data type */
     int counter, grade, total, highest;
     /* initialization phase */
     total= 0;
     counter= 0;
     highest=0;
     /* processing phase */
     printf( "Enter grade, -1 to end: " );
     scanf( "%d", &grade );
     
     while ( grade != -1 ) {
          total = total + grade;
          counter = counter + 1;
          printf( "\nEnter grade, -1 to end: " );
          scanf("%d", &grade);
          if(grade > highest) {
               highest=grade;
               printf("High\n");

          }
     }
         
     average = ( float ) total / counter;
     printf( "Class average is %.2f \n", average );
     return(highest);
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:jdrescher
ID: 6332373
#include <stdio.h>
int  main()
{
     float average;              /* new data type */
     int counter, grade, total, highest;
     /* initialization phase */
     total= 0;
     counter= 0;
     highest=0;
     /* processing phase */
     printf( "Enter grade, -1 to end: " );
     scanf( "%d", &grade );
     
     while ( grade != -1 ) {
          total = total + grade;
          counter = counter + 1;
          printf( "\nEnter grade, -1 to end: " );
          scanf("%d", &grade);
          if(grade > highest) {
               highest=grade;
               printf("High\n");

          }
     }
     
       if ( counter > 0 ) {
       average = ( float ) total / counter;
        printf( "Class average is %.2f \n", average );
       }
       else
         printf("ERROR: You did not enter any grades.\n");


     return(highest);
}
0
 
LVL 11

Expert Comment

by:griessh
ID: 6332846
jdrescher

The solution was needed ASAP, so I gues you are late anyway. So this is just a reminder to your membership agreement. We are not allowed to do the homework for people (and that was clearly a homework). We tried to give some hints here, but the work has to be done by the student. Please be careful with your help.

======
Werner
0
 
LVL 1

Expert Comment

by:jdrescher
ID: 6334949
Sorry. I have not used EE for a long time (> 1 year) and don't remember any of the rules. Anyways it was obvious that this was homework, but since it was over a month late I thought It could possibly help her learn something to see a simple solution but probably not get many points for turning in homework this late.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6811721
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days.
Unless there is objection or further activity,  I will suggest to accept "agriggs" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please do not accept this comment as an answer!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6828371
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question