Solved

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

Posted on 2001-06-07
8
241 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
std::atomic with custom class 6 186
how to understand recursion 12 218
PDF library for Delphi 2 103
Should CArray be used for a list of pointers in C++? 19 97
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

932 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

10 Experts available now in Live!

Get 1:1 Help Now