473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

"for" loop to find the highest score in an array.

I am working on this program. The array has 10 scores. (there is more to this program.) Does the last "for" section make sense?


/*Defines a global constant called N throughout the file.
Note that N = 10.
*/

#define N 10
#include <iostream>
using namespace std;

/*Define a StudentScores class with methods called

(1) getHighestScore – find the highest score in the array
(2) getLowestScore – find the lowest score in the array
(3) getAverageScore – compute the average score

*/
class StudentScores
{
private:
int _scores[N];
public:
StudentScores(int scores[])
{
for(int i = 0; i < N; i = i + 1)
_scores[i] = scores[i];
}

int getHighestScore()
{
int highest;
highest = _scores[0];
for (int i = 0; i < N; i++)
if (a [i]> scores [0]
highest = a [i]
return highest;
Nov 22 '06 #1
3 16069
sivadhas2006
142 100+
I am working on this program. The array has 10 scores. (there is more to this program.) Does the last "for" section make sense?


/*Defines a global constant called N throughout the file.
Note that N = 10.
*/

#define N 10
#include <iostream>
using namespace std;

/*Define a StudentScores class with methods called

(1) getHighestScore – find the highest score in the array
(2) getLowestScore – find the lowest score in the array
(3) getAverageScore – compute the average score

*/
class StudentScores
{
private:
int _scores[N];
public:
StudentScores(int scores[])
{
for(int i = 0; i < N; i = i + 1)
_scores[i] = scores[i];
}

int getHighestScore()
{
int highest;
highest = _scores[0];
for (int i = 0; i < N; i++)
if (a [i]> scores [0]
highest = a [i]
return highest;
Hi,

The code is incomplete.

Expand|Select|Wrap|Line Numbers
  1. /*Defines a global constant called N throughout the file. 
  2. Note that N = 10.
  3. */
  4.  
  5. #define N 10 
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. /*Define a StudentScores class with methods called
  10.  
  11. (1) getHighestScore – find the nHighest score in the array
  12. (2) getLowestScore – find the lowest score in the array
  13. (3) getAverageScore – compute the average score
  14.  
  15. */
  16. class StudentScores
  17. {
  18.    private:
  19.       int m_nScores[N];
  20.  
  21.    public:
  22.       StudentScores(int a_nScores[])
  23.       {
  24.          for(int i = 0; i < N; i = i + 1)
  25.          {
  26.             m_nScores[i] = a_nScores[i];
  27.          }
  28.       }
  29.  
  30.       int GetHighestScore()
  31.       {
  32.          int 
  33.             nHighest = 0;
  34.  
  35.          nHighest = m_nScores[0];
  36.          for (int i = 0; i < N; i++)
  37.          {
  38.             if (a [i] > a_nScores [0])
  39.             {
  40.                nHighest = a [i]; 
  41.             }
  42.          }
  43.          return nHighest;
  44.       }
  45. };
  46.  
Can you post your full code?

Regards,
M.Sivadhas.
Nov 22 '06 #2
Here is the full code:

/*Defines a global constant called N throughout the file.
Note that N = 10.
*/

#define N 10
#include <iostream>
using namespace std;

/*Define a StudentScores class with methods called

(1) getHighestScore – find the highest score in the array
(2) getLowestScore – find the lowest score in the array
(3) getAverageScore – compute the average score

*/
class StudentScores
{
private:
int _scores[N];
public:
StudentScores(int scores[])
{
for(int i = 0; i < N; i = i + 1)
_scores[i] = scores[i];
}

int getHighestScore()

int nHighest = 0;
nHighest = m_nScores [0];
for (int i = 0; i < N; i++)
if (a [i] > a_nScores [0];
return nHighest;
{
int getLowestScore()
{
int lowest;
lowest = _scores[0];
I am supposed to here WRITE A 'while' LOOP THAT DETERMINES THE LOWEST SCORE
return lowest;
}

double getAverageScore()
{
double ave;
I am supposed to here WRITE A 'for' LOOP THAT COMPUTES THE AVERAGE GRADE
return ave;
}
};

int main(){

//Create a scores array and assign 10 scores
int scores[] = {1,29,31,45,5,61,99,87,12,100};

//Create a student scores object
StudentScores studentOne(scores);

//Use the getHighestScore method to display the highest score
cout << "The highest score is " <<
studentOne.getHighestScore() << endl;
cout << "The lowest score is " <<
studentOne.getLowestScore() << endl;
cout << "The average score is " <<
studentOne.getAverageScore() << endl;
}
Nov 24 '06 #3
horace1
1,510 Expert 1GB
Here is the full code:
int getHighestScore()

int nHighest = 0;
nHighest = m_nScores [0];
for (int i = 0; i < N; i++)
if (a [i] > a_nScores [0];
return nHighest;
{
there are a few errors in the above function, the following should work
Expand|Select|Wrap|Line Numbers
  1. int getHighestScore()
  2. {                           // ** added {
  3. int nHighest = 0;
  4. nHighest = _scores [0];     // ** fixed name
  5. for (int i = 0; i < N; i++)
  6. if (_scores  [i] > nHighest) nHighest=_scores  [i];  // ** fixed test
  7. return nHighest;
  8. }                             // ** changed { to }
  9.  
make sure you are consistent with variable names

now try and complete the rest of the program
Nov 24 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
5
by: Fabian Vilers | last post by:
Hi again... I'm wondering what could be better in terms of performance between: var my_array = new Array(); // populate array for (index in my_array) { // do something with my_array
3
by: Patrick Sullivan | last post by:
In this for loop, IE skips over the animation function until the end of the loop and only aninmates the last phrase. Firefox does it right. Loop is right below, entire script is below that. This...
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
9
by: PengYu.UT | last post by:
Hi, I have the code below this email. I want to replace the last 4 lines with a Metaprogramming loop to get something like the following (I don't know the syntax). Is it possible? for type in...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
9
by: Alexnb | last post by:
Okay, so lets say you have a list: funList = and you do: for x in funList: print x this will print 1-5
3
by: bhavyagupt | last post by:
i 'm not able to use the accordion menu code in for loop . can anyone solve ma problem. code....... slider.js------> java script var slider=function(){ var array=; var speed=10; var...
3
by: npm | last post by:
Hi, I've been using a "for" loop in a javascript to load multiple nodes from an xml file, but now I need to do the same thing in flash. Here's the javascript code: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.