Solved

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

Posted on 2001-06-07
8
240 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now