Solved

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

Posted on 2001-06-07
8
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

626 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