im writing a source for a choice program that does the commands in its function and the program runs fine. the only sections not working is :
Above Average # 5-2
Two Die Simulation # 5-6
Olympic Judging # 5-8
and for the Olympic Judging # 5-8 it is supposed to do the average of the remaining four score and the highest and lowest scores are not include in the average. i have written the code but not sure it is right.
You've created an array with 5 elements, which will have indices from 0 to 4. Your for loops run from 0 to 5, so you're going out of bounds on the array.
1 2 3 4 5 6 7
double numbers [5], average = 0;
int i, count = 0;
for(i = 0; i < 6; i++){
cout << "Please type a value for LIST: ";
cin >> numbers[i];
}
I don't see where you're actually adding up the numbers entered in the array to be able to calculate the average.
void TwoDieSimulation()
You have a 12 element array with index values 0 through 11. Your for loops and possible dieValue are going up to 12. You haven't initialized the counters array so there is an uninitialized value when you try to add 1 here.
1 2
counters[dieValue] = counters[dieValue] + 1;
void OlympicJudging()
Numbers is an array with 6 elements. The code tries to access a non-existent element here.
You're using an integer type variable to store the value of the double number in the array. You're losing the fractional part of the number here.
int temp;
The sorting from lowest to highest isn't working. I just put some cout statements into your code so you can see what is happening on each iteration of the outer for loop.
You input numbers :2.5 9.4 8.2 4.3 7.2 1.9
current array after iteration 0 : 1.9 9.4 8.2 4.3 7.2 2.5
current array after iteration 1 : 1.9 4.3 9.4 8.2 7.2 2.5
current array after iteration 2 : 1.9 9.4 4.3 8.2 7.2 2.5
current array after iteration 3 : 1.9 9.4 8.2 4.3 7.2 2.5
current array after iteration 4 : 1.9 9.4 8.2 4.3 7.2 2.5
current array after iteration 5 : 1.9 9.4 8.2 4.3 7.2 2.5
here is an updated code and i was trying to fix the problems in :
Above Average # 5-2
Two Die Simulation # 5-6
Olympic Judging # 5-8
but couldn't fix it.
You still have issues with arrays going out of bounds. If an array has n elements, the valid index numbers for each element are going to be between 0 and n-1.
Line 80 - you're just dividing the value of one element of the array by 5. You haven't summed up the individual values in all the five elements.
constint arraySize = 5; //or whatever size you need the array to be
int myArray[arraySize] = {}; //initialize array elements to 0
int sumArray = 0; //create variable to store sum and initialize to 0
for (int i = 0; i < arraySize; i++)
{
cout << "Please enter a value: ";
cin >> myArray[i]; // fill each array element, 0 through arraySize-1
sumArray += myArray[i] //increment the sum with the number just entered
}
The generic example I posted earlier that you copied into your Olympic Judging function shows how to sum all the elements in an array. I thought you needed the sum for your Above Average function?
An average value of all the elements in the array is the total of all the elements divided by the number of elements. You've already got the formula set up in line 80, you're just dividing the value of one element by the total of all the elements.
void AboveAverage() // Above Average
{
double numbers [5], average = 0;
int i, count = 0;
for(i = 1; i < 6; i++){
cout << "Please type a value for LIST: ";
cin >> numbers[i];
}
average += numbers[i];
average = numbers[i] / 5;
cout <<"The average is: " << average << endl;
cout << count <<"The following numbers are greater than the average: \n" << endl;
for(i = 0 ; i < 6; i++){
if(numbers[i] > average)
count++;
//cout << numbers[i];
}
}
option 4 the results in histogram which is to represent "o" by how many times the dice has rolled but instead it goes to never ending loop.
void TwoDieSimulation() //Two Die Simulation
{
int counters [6];
int roll, diepercent, die;
cout <<"Please type a value for ROLL TO MAKE: ";
cin >> roll;
srand(time(0));
for(int i = 1; i <= roll; i++){
die = random(12) + 1;
counters[die] = counters[die] + 1;
}
for(int k = 1; k < 13; k++){
cout <<"The value " << k << " was rolled " << counters[k] << " times ";
diepercent = counters[k] / roll;
cout <<" or " << diepercent << endl;
}
cout <<"\nBelow are the results in histogram form: \n";
for(int x = 1; x < 13; x++){
cout << x << " : ";
for (int n = 1; n < counters[x]; n++){
cout <<"o";
}
cout << endl;
}
}
my option my 6 doesnt work also could you show the calculation for line 80
and for option 6 it never asks for any inputs and it has to calculate average with only four numbers exculding highest and lowest number. l follwed how you showed in your example but it didnt worked on my part.
void OlympicJudging() // Olympic Judging
{
constint arraySize = 6; //or whatever size you need the array to be
int myArray[arraySize] = {}; //initialize array elements to 0
int sumArray = 0; //create variable to store sum and initialize to 0
int temp;
for (int i = 0; i < arraySize; i++){
cout <"Please type a value for scores: ";
cin >> myArray[i]; // fill each array element, 0 through arraySize-1
sumArray += myArray[i]; //increment the sum with the number just entered
}
for(int i = 0; i < arraySize; i++){
for(int j = 1; j < arraySize - i; j++){
if(myArray[i] > myArray[j])
{
temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
}
}
cout << "\nHighest Score: " << myArray[6] << endl;
cout << "Lowest Score: " << myArray[0] << endl;
}
line 6/14 - the for loop is still accessing outside of bounds of the array
line 10 - is outside the loop, needs to be inside to increment the total as each value is entered - I might use a separate variable to hold the sum of the numbers then on line 11 - divide that by the number of elements to assign to the average variable
option 4
the counters array is defined to have 6 elements. you're still going out of bounds of the array in your for loops
line 13 - the array still hasn't been initialized so counters[die] has a garbage value when you try to add 1 to it
option 6
I gave you a generic example of how arrays are initialized and the values in each element are summed. I didn't mean for you to literally cut and paste it into your code. It adds up all the elements of the array. If you only want the middle four numbers in this function you'll need to make adjustments to the example and implement it after you sort the array.
The sorting algorithm you have there isn't doing what you want.
The sorting from lowest to highest isn't working. I just put some cout statements into your code so you can see what is happening on each iteration of the outer for loop.
You input numbers :2.5 9.4 8.2 4.3 7.2 1.9
current array after iteration 0 : 1.9 9.4 8.2 4.3 7.2 2.5
current array after iteration 1 : 1.9 4.3 9.4 8.2 7.2 2.5
current array after iteration 2 : 1.9 9.4 4.3 8.2 7.2 2.5
current array after iteration 3 : 1.9 9.4 8.2 4.3 7.2 2.5
current array after iteration 4 : 1.9 9.4 8.2 4.3 7.2 2.5
current array after iteration 5 : 1.9 9.4 8.2 4.3 7.2 2.5
void TwoDieSimulation() //Two Die Simulation
{
int counters [12];
int roll, diepercent, die;
srand(time(0));
cout <<"Please type a value for ROLL TO MAKE: ";
cin >> roll;
for(int i = 1; i <= roll; i++){
die = random(12) + 1;
counters[die] = counters[die] + 1;
}
for(int k = 1; k <= 12; k++){
cout <<"The value " << k << " was rolled " << counters[k] << " times ";
diepercent = (counters[k] / roll) * 100;
cout <<" or " << diepercent << " %" << endl;
}
cout <<"\nBelow are the results in histogram form: \n";
for(int x = 1; x <= 12; x++){
cout << x << " : ";
for (int n = 1; n < counters[x]; --n){
cout <<"o";
}
cout << endl;
}
}
option 3 the average gives me zero and it doesnt give the numbers that are greater than the average
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void AboveAverage() // Above Average
{
double numbers [5], average = 0;
int i, count = 0 and what does count do ;
for(i = 0; i < 6; i++){
cout << "Please type a value for LIST: ";
cin >> numbers[i];
}
average += numbers[i];
average = numbers[i] / 5;
cout <<"The average is: " << average << endl;
cout << count <<"The following numbers are greater than the average: \n" << endl;
for(i = 0 ; i < 6; i++){
if(numbers[i] > average)
cout << numbers[i]++;
}
option 6
highest gives me only 10 and can you help me started with the getting the average of only four numbers
option 4
the results in histogram goes in never ending loop;
option 3
the average gives me zero and it doesnt give the numbers that are greater than the average
option 6
highest gives me only 10 and i need to get the average of only four numbers ( excluding lowest and highest )