Hi...

I want to insert a variable finalScore into an array competitorsArray[competitorCounter] . I can't find any instructions anywhere on how to do this, so I'm wondering if it's even possible.

Also, how do I output an error message if the user enters a number that I then store into an array if the number is not within certain parameters, ie the number should be between 0.0 and 6.0 and only be in increments of 0.1, only one decimal place?

I've included my original code with comments as to what I would like to do and what I've already tried.
Thanks in advance for any help. btw I'm a newbie at C++ :)

``````/*##########################################################################
### The problem should prompt the operator to enter the
number of competitors, followed by the scores awarded to each
by the 5 judges. Scores can range from 0.0 to 6.0 in increments of 0.1.
Invalid scores entered by the operator should result in an error message
being displayed. To calculate the final score to be awarded to each
Diving Competitor, the highest and lowest scores are ignored and
the remaining scores are averaged to give the final score,
which is then displayed. The problem should continue
until specifically terminated.
##########################################################################*/

#include <iomanip>
#include <iostream>
using namespace std;

int main (){

char  closeProgramOption = 'y'; // Declare variable y that will be used in close/restart program option

while(closeProgramOption=='y'){

int numberOfCompetitors;     // Number of competitors
float judgesScore;           // Score awarded by each individual judge
float finalScore = 0.0;      // Diver's final score, initialised to 0.0
int competitorCounter = 1;   // Counts competitors, initialised to 1
int judgeCounter = 1;        // Counts judges, initialised to 1
float lowestScore = 6.0;     // Lowest score awarded to diver, initialised to 6.0
float highestScore = 0.0;    // Highest score awarded to diver, initialised to 0.0
int i;						  // control variable for loop
float insert; // temporary variable to hold element to insert

/*---------------------
Display welcome message
----------------------*/

cout << "\n ******************************************";
cout << "\n *                                        *";
cout << "\n * Welcome to the Diving Score for the    *\n";
cout << " *       London, 2012 Olympic Games       *";
cout << "\n *                                        *";
cout << "\n ******************************************\n\n";

/*------------------------------------------
Asks user to input the number of competitors
-------------------------------------------*/

cout << "Please enter the number of competitors: ";
cin >> numberOfCompetitors; // Store number of competitors
cout << endl;

/*-------------------------------------------------------------------------------
Processing phase:
-----------------
This phase takes the input from each of 5 judges for each individual competitor,
calculates the competitor's final score by disregarding the highest and lowest
score awarded and obtaining the average of the 3 remaining scores.
--------------------------------------------------------------------------------*/

float competitorsArray[numberOfCompetitors]; // Declare array to hold the scores
//from all the competitors

while (competitorCounter <= numberOfCompetitors){

cout << fixed << setprecision ( 1 ); // sets output of scores to one decimal place
cout << "Scores for Competitor: " << competitorCounter << endl;

while (judgeCounter <= 5){

cout << "Enter the score from judge no. " << judgeCounter << ": ";     // Prompts user to enter judges'score
cin >> judgesScore;

// Determine if judge's score awarded is within acceptable range, i.e. 0.0 to 6.0:
if (judgesScore >= 0.0 && judgesScore <= 6.0){

// Add score to competitor's final score
finalScore += judgesScore;

// Determines lowest score awarded to competitor
if (judgesScore < lowestScore){
lowestScore = judgesScore;
}

// Determines highest score awarded to competitor
if (judgesScore > highestScore){
highestScore = judgesScore;
}

// Increment judgeCounter by 1
judgeCounter += 1;
}
else{

//  Display error message if incorrect value for judge's score is entered
cout << "You have entered an incorrect value.\nPlease enter a value between 0.0 and 6.0...\n\n";
}
} // end while

// Display Highest and Lowest Score
cout << "highestScore = " << highestScore << "\nlowestScore = " << lowestScore << endl;

// Calculate competitor's final score
finalScore = (finalScore - highestScore - lowestScore) / 3;

// Display competitor's final score
cout << endl << "The final score for Competitor " << competitorCounter << " is: " << finalScore << endl << endl;

/*This is the code I also have using an array but I'm unable to display an error message if the user enters an incorrect value for judges score

cout << "Scores for Competitor: " << competitorCounter << endl;

//input scores from judges
for ( i=0; i<5; i++){
cout << "Score from Judge No. " << i+1 << ": ";
cin >> setprecision ( 1 ) >> judgeScore[i];
}

//output scores
for (i = 0; i<5; i++){
cout << fixed << setprecision ( 1 );
cout << "Score from Judge: " <<i+1 << ": " << judgeScore[i] << "\n";
}

// sort judges score into ascending order
// insertion sort
// loop over the elements of the array

for (int next=1; next<arraySize; next++){
insert = judgeScore[next];
int moveItem = next;

while ((moveItem >0) && (judgeScore[moveItem -1] > insert)){
judgeScore[moveItem] = judgeScore[moveItem - 1];
moveItem--;
} // end while

judgeScore[moveItem] = insert;
} // end for

cout << endl;

// Display Highest and Lowest Score
cout << "highestScore = " << judgeScore[4] << "\nlowestScore = " << judgeScore[0] << endl;

//Calculate final score
finalScore = (judgeScore[1] + judgeScore[2] + judgeScore[3]) / 3;
// Display competitor's final score:
cout << endl << "The final score for Competitor " << competitorCounter << " is: " << finalScore << endl << endl;
cout << endl << endl; */

/* here I want to enter final score into an array that will diplay all the final scores from all competitors at the end of the program, which I can sort and output in ascending or descending order*/

// Increment competitorCounter by 1
competitorCounter += 1;

// Reset counters to re-enter processing loop
judgeCounter = 1;
lowestScore = 6.0;
highestScore = 0.0;
finalScore = 0.0;
} //end while

// End program or restart
cout <<"\n Do you want to continue:? \n Press (Y) to restart OR any other key to Exit ";
cin >> closeProgramOption;
} // end while

return 0;

} // end main``````

I hope all this makes sense :)

>>I want to insert a variable finalScore into an array competitorsArray[competitorCounter]

Depends on what you mean by "insert". If you have an array that already contains numbers and you want to insert another one in the middle of the array without destroying the number that is already there then …

It's just a simple assignment

``````int competitorsArray[255] = {0};

competitorsArray[competitorCounter] = finalScore;``````

## All 5 Replies

>>I want to insert a variable finalScore into an array competitorsArray[competitorCounter]

Depends on what you mean by "insert". If you have an array that already contains numbers and you want to insert another one in the middle of the array without destroying the number that is already there then you will have to move all the numbers from the insertion point to open up an empty spot for the new number. That's just a simple loop to copy each number to its next higher index in the array. Start at the end of the array and work backwards until you reach the insertion point.

As for your second question -- c and c++ use floating points which have lots of digits after the decimal point. It's not possible using floats to restrict them to just one decimal digit. One way to solve that problem is to convert the number to an int then compare the integers. Example: `int score = (int)(judgesScore * 10);` Then compare score between 0 and 6.

>>I want to insert a variable finalScore into an array competitorsArray[competitorCounter]

Depends on what you mean by "insert".

hi..

Maybe I should have been clearer...

I want to create an array competitorsArray[competitorCounter] and then add the variable finalScore to it each time the value of finalScore changes / is recalculated.

It's just a simple assignment

``````int competitorsArray[255] = {0};

competitorsArray[competitorCounter] = finalScore;``````

It's just a simple assignment

``````int competitorsArray[255] = {0};

competitorsArray[competitorCounter] = finalScore;``````

Thanks for that. I'll try that.
I see where I was making a mistake in initialising the array.
I'm guessing the array length of 255 is just an arbitrary length that you need to declare?

>>I'm guessing the array length of 255 is just an arbitrary length that you need to declare?

Yes -- make it whatever size you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.