Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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

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
Nancy
Asked:
Nancy
1 Solution
 
agriggsCommented:
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
 
griesshCommented:
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
 
jdrescherCommented:
#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!

 
jdrescherCommented:
#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
 
griesshCommented:
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
 
jdrescherCommented:
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
 
griesshCommented:
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
 
NetminderCommented:
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now