Fuctions

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.


heres mu code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <stdlib.h>
using namespace std;

// Function prototypes
void Number(); // Number Stack Different # 4-4
void RightTriangle(); // Right Triangle  # 4-6
void AboveAverage(); // Above Average # 5-2
void TwoDieSimulation(); // Two Die Simulation # 5-6 
void AccessGranted();  // Access Granted # 5-7
void OlympicJudging(); // Olympic Judging # 5-8
void Func7();

int main()
{
	int Choice;
	do {
		cout << endl <<"\t\tTeam Shuriken" << endl <<endl;
		cout << " 1. Number Stack Diiferent " << endl;
		cout << " 2. Right Triangle " << endl;
		cout << " 3. Above Average " << endl;  
		cout << " 4. Two Die Simulation " << endl;
		cout << " 5. Access Granted " << endl;
		cout << " 6. Olympic Judging " << endl;
		cout << " 7. Func7 " << endl;
		cout << " 8. Quit the Program" << endl << endl;
		
		cout << "Enter number 1 - 8 for your Choice: ";
		cin >> Choice;
		
		if(Choice==1)Number();
		else if(Choice==2)RightTriangle();
		else if(Choice==3)AboveAverage();
		else if(Choice==4)TwoDieSimulation();
		else if(Choice==5)AccessGranted();
		else if(Choice==6)OlympicJudging();
		else if(Choice==7)Func7();
	}while (Choice != 8);
	system("cls");
	cout << " Thank You " << endl;
	system("PAUSE");
	return 0;
}
void Number() // Number Stack Different
{
	int SMB;
	for (int num = 1; num <= 9; num++){
		for (int num1 = 1; num1 <= num; num1++){
			SMB = num1;
			cout << SMB;
		}
		cout <<" " << endl;
	}
}
void RightTriangle() // Right Triangle
{
	for(int LineCount = 1; LineCount <= 10; LineCount++){
		for(int SpaceCount = 10 - LineCount; SpaceCount > 0; SpaceCount--){
			cout <<" ";
		}
		for(int CircleCount = 1; CircleCount < LineCount; CircleCount++){
			cout << "o";
		}
		cout << endl;
	}
}
void AboveAverage() // Above Average
{
	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];
	}
	average = numbers[i];
	average = average / 5;
	cout <<"The average is: " << average << endl;
	for(i = 0 ; i < 6; i++){
		if(numbers[i] > average)
			count++;
		cout << count <<"The following numbers are greater than the average: \n" << endl;
		cout << numbers[i];
	
	}
}
void TwoDieSimulation() //Two Die Simulation
{
	int counters [12];
	double diePercentage;
	int rolltomake, dieValue;
	cout <<"Please type a value for ROLLTOMAKE: ";
	cin >> rolltomake;
	for(int i = 1; i <= rolltomake; i++){
		dieValue = rand() % 12 + 1;
		counters[dieValue] = counters[dieValue] + 1;
	}
	for(int k = 1; k <= 12; k++){
		cout <<"The value " << k << "was rolled " << counters[k] << " times ";
		diePercentage = (counters[k] * 100) / rolltomake;
		cout << diePercentage << endl;
	}
	cout <<"Below are the results in histogram form: \n";
	for(int x = 1; x <= 12; x++){
		cout << x << " : ";
		for (int n = 1; n <= counters[n]; n++){
			cout <<"o";
		}
		cout << endl;
	}
}
void AccessGranted()  // Access Granted
{
	string inputUser, inputPass;
	int foundFlag, k;
	string userName [10];
	string passWord [10];
	
	for (k = 1; k <= 2; k++){
		cout <<"\nEnter the Username: ";
		cin >> userName[k];
		cout <<"Enter the Password: ";
		cin >> passWord[k];}
	cout <<"Input User: ";
	cin >> inputUser;
	foundFlag = false;
	for (k = 1; k <= 2; k++){
		if (inputUser == userName[k])
			foundFlag = true;
		cout <<"Input Pass: ";
		cin >> inputPass;
		if (inputPass == passWord[k])
			cout <<"Access granted.";
		else
			cout <<"Username and password do not match.";
	}
	if (foundFlag = false)
		cout <<"Username not Found";

}
void OlympicJudging() // Olympic Judging
{
	double numbers [6], average;
	int temp;
	for(int i = 0; i < 6; i++){
		cout << "Please type a value for scores: ";
		cin >> numbers[i];
	}
	for(int i = 0; i < 6; i++){
		for(int j = 1; j < 6 - i; j++){
			if(numbers[i] > numbers[j])
			{
				temp = numbers[i];
				numbers[i] = numbers[j];
				numbers[j] = temp;
			}
		}
	}
	cout << "\nHighest Score: " << numbers[9] << endl;
	cout << "Lowest Score: " << numbers[0] << endl;
}
void Func7()
{
	cout << "Yet to be completes -- Member was Absent"<< endl;
}
Last edited on
void AboveAverage()

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.

cout << "\nHighest Score: " << numbers[9] << endl;


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 
Last edited on
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;

// Function prototypes
void Number(); // Number Stack Different # 4-4
void RightTriangle(); // Right Triangle  # 4-6
void AboveAverage(); // Above Average # 5-2
void TwoDieSimulation(); // Two Die Simulation # 5-6 
void AccessGranted();  // Access Granted # 5-7
void OlympicJudging(); // Olympic Judging # 5-8
void Func7();

int main()
{
	int Choice;
	do {
		cout << endl <<"\t\tTeam Shuriken" << endl <<endl;
		cout << " 1. Number Stack Diiferent " << endl;
		cout << " 2. Right Triangle " << endl;
		cout << " 3. Above Average " << endl;    // not working properly !!CHECK!!
		cout << " 4. Two Die Simulation " << endl;
		cout << " 5. Access Granted " << endl;
		cout << " 6. Olympic Judging " << endl;
		cout << " 7. Func7 " << endl;
		cout << " 8. Quit the Program" << endl << endl;
		
		cout << "Enter number 1 - 8 for your Choice: ";
		cin >> Choice;
		
		if(Choice==1)Number();
		else if(Choice==2)RightTriangle();
		else if(Choice==3)AboveAverage();
		else if(Choice==4)TwoDieSimulation();
		else if(Choice==5)AccessGranted();
		else if(Choice==6)OlympicJudging();
		else if(Choice==7)Func7();
	}while (Choice != 8);
	system("cls");
	cout << " Thank You " << endl;
	system("PAUSE");
	return 0;
}
void Number() // Number Stack Different
{
	int SMB;
	for (int num = 1; num <= 9; num++){
		for (int num1 = 1; num1 <= num; num1++){
			SMB = num1;
			cout << SMB;
		}
		cout <<" " << endl;
	}
}
void RightTriangle() // Right Triangle
{
	for(int LineCount = 1; LineCount <= 10; LineCount++){
		for(int SpaceCount = 10 - LineCount; SpaceCount > 0; SpaceCount--){
			cout <<" ";
		}
		for(int CircleCount = 1; CircleCount < LineCount; CircleCount++){
			cout << "o";
		}
		cout << endl;
	}
}
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] / 5;
	cout <<"The average is: " << average << endl;
	for(i = 0 ; i < 6; i++){
		if(numbers[i] > average)
			count++;
		cout << count <<"The following numbers are greater than the average: \n" << endl;
		cout << numbers[i];
	
	}
}
void TwoDieSimulation() //Two Die Simulation
{
	int counters [12];
	double diePercentage;
	int rolltomake;
	unsigned dieValue = 0;
	srand(time(0));
	dieValue = rand() % 12 + 1;
	while (dieValue){
		cout <<"Please type a value for ROLLTOMAKE: ";
		cin >> rolltomake;
		for(int i = 0; i <= rolltomake; i++){
			dieValue = rand() % 12 + 1;
			counters[dieValue] = counters[dieValue] + 1;
		}
		cout << endl;
		for(int k = 1; k < 13; k++){
			cout <<"The value " << k << " was rolled " << dieValue << " times ";
			diePercentage = (k * 100) / rolltomake;
			cout << diePercentage << endl;
		}
		cout <<"\nBelow are the results in histogram form: \n";
		for(int x = 1; x < 13; x++){
			cout << x << " : ";
			for (int n = 1; n <= dieValue; n++){
				cout <<"o";
			}
			cout << endl;
		}
	}
}
void AccessGranted()  // Access Granted
{
	string inputUser, inputPass;
	int foundFlag, k;
	string userName [10];
	string passWord [10];
	
	for (k = 1; k <= 2; k++){
		cout <<"\nEnter the Username: ";
		cin >> userName[k];
		cout <<"Enter the Password: ";
		cin >> passWord[k];}
	cout <<"Input User: ";
	cin >> inputUser;
	foundFlag = false;
	for (k = 1; k <= 2; k++){
		if (inputUser == userName[k])
			foundFlag = true;
		cout <<"Input Pass: ";
		cin >> inputPass;
		if (inputPass == passWord[k])
			cout <<"Access granted.";
		else
			cout <<"Username and password do not match.";
	}
	if (foundFlag = false)
		cout <<"Username not Found";

}
void OlympicJudging() // Olympic Judging
{
	int numbers [6], average;
	int temp;
	for(int i = 0; i < 6; i++){
		cout << "Please type a value for scores: ";
		cin >> numbers[i];
	}
	for(int i = 0; i <= 6; i++){
		for(int j = 1; j < 6 - i; j++){
			if(numbers[i] > numbers[j])
			{
				temp = numbers[i];
				numbers[i] = numbers[j];
				numbers[j] = temp;
			}
		}
	}
	cout << "\nHighest Score: " << numbers[6] << endl;
	cout << "Lowest Score: " << numbers[0] << endl;
}
void Func7()
{
	cout << "Yet to be completes -- Member was Absent"<< endl;
}
Last edited on
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.
Can you show how it's done and also the line 80 part
1
2
3
4
5
6
7
8
9
10
const int 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
}
my option my 4, 5 and 6 doesnt work also could you show the calculation for line 80

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;

// Function prototypes
void Number(); // Number Stack Different # 4-4
void RightTriangle(); // Right Triangle  # 4-6
void AboveAverage(); // Above Average # 5-2
void TwoDieSimulation(); // Two Die Simulation # 5-6 
void AccessGranted();  // Access Granted # 5-7
void OlympicJudging(); // Olympic Judging # 5-8
void Func7();

int main()
{
	int Choice;
	do {
		cout << endl <<"\t\tTeam Shuriken" << endl <<endl;
		cout << " 1. Number Stack Diiferent " << endl;
		cout << " 2. Right Triangle " << endl;
		cout << " 3. Above Average " << endl;    // not working properly !!CHECK!!
		cout << " 4. Two Die Simulation " << endl;
		cout << " 5. Access Granted " << endl;
		cout << " 6. Olympic Judging " << endl;
		cout << " 7. Func7 " << endl;
		cout << " 8. Quit the Program" << endl << endl;
		
		cout << "Enter number 1 - 8 for your Choice: ";
		cin >> Choice;
		
		if(Choice==1)Number();
		else if(Choice==2)RightTriangle();
		else if(Choice==3)AboveAverage();
		else if(Choice==4)TwoDieSimulation();
		else if(Choice==5)AccessGranted();
		else if(Choice==6)OlympicJudging();
		else if(Choice==7)Func7();
	}while (Choice != 8);
	system("cls");
	cout << " Thank You " << endl;
	system("PAUSE");
	return 0;
}
void Number() // Number Stack Different
{
	int SMB;
	for (int num = 1; num <= 9; num++){
		for (int num1 = 1; num1 <= num; num1++){
			SMB = num1;
			cout << SMB;
		}
		cout <<" " << endl;
	}
}
void RightTriangle() // Right Triangle
{
	for(int LineCount = 1; LineCount <= 10; LineCount++){
		for(int SpaceCount = 10 - LineCount; SpaceCount > 0; SpaceCount--){
			cout <<" ";
		}
		for(int CircleCount = 1; CircleCount < LineCount; CircleCount++){
			cout << "o";
		}
		cout << endl;
	}
}
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] / 5;
	cout <<"The average is: " << average << endl;
	for(i = 0 ; i < 6; i++){
		if(numbers[i] > average)
			count++;
		cout << count <<"The following numbers are greater than the average: \n" << endl;
		cout << numbers[i];
	
	}
}
void TwoDieSimulation() //Two Die Simulation
{
	int counters [12];
	double diePercentage;
	int rolltomake;
	unsigned dieValue = 0;
	srand(time(0));
	dieValue = rand() % 12 + 1;
	while (dieValue){
		cout <<"Please type a value for ROLLTOMAKE: ";
		cin >> rolltomake;
		for(int i = 0; i <= rolltomake; i++){
			dieValue = rand() % 12 + 1;
			counters[dieValue] = counters[dieValue] + 1;
		}
		cout << endl;
		for(int k = 1; k < 13; k++){
			cout <<"The value " << k << " was rolled " << dieValue << " times ";
			diePercentage = (k * 100) / rolltomake;
			cout << diePercentage << endl;
		}
		cout <<"\nBelow are the results in histogram form: \n";
		for(int x = 1; x < 13; x++){
			cout << x << " : ";
			for (int n = 1; n <= dieValue; n++){
				cout <<"o";
			}
			cout << endl;
		}
	}
}
void AccessGranted()  // Access Granted
{
	string inputUser, inputPass;
	int foundFlag, k;
	string userName [10];
	string passWord [10];
	
	for (k = 1; k <= 2; k++){
		cout <<"\nEnter the Username: ";
		cin >> userName[k];
		cout <<"Enter the Password: ";
		cin >> passWord[k];}
	cout <<"Input User: ";
	cin >> inputUser;
	foundFlag = false;
	for (k = 1; k <= 2; k++){
		if (inputUser == userName[k])
			foundFlag = true;
		cout <<"Input Pass: ";
		cin >> inputPass;
		if (inputPass == passWord[k])
			cout <<"Access granted.";
		else
			cout <<"Username and password do not match.";
	}
	if (foundFlag = false)
		cout <<"Username not Found";

}
void OlympicJudging() // Olympic Judging
{
	const int 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 <= 6; i++){
		for(int j = 1; j < 6 - 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;
}
void Func7()
{
	cout << "Yet to be completes -- Member was Absent"<< endl;
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.
for option number 3, 4 and 6 calculations are not working properly..

option 3 always shows average of zero and it never shows the following numbers that are above average.

option 3 code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.

option 4 code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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.

option 6 code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void OlympicJudging() // Olympic Judging
{
	const int 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;
}
option 3

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
i have corrected some
option 4
the results in histogram goes in never ending loop;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void OlympicJudging() // Olympic Judging
{
	int numbers [6], average;
	int temp;
	for(int i = 0; i < 6; i++){
		cout << "Please type a value for scores: ";
		cin >> numbers[i];
	}
	for(int i = 0; i <= 6; i++){
		for(int j = 1; j < 6 + 1 ; j++){
			if(numbers[i] > numbers[j])
			{
				temp = numbers[i];
				numbers[i] = numbers[j];
				numbers[j] = temp;
			}
		}
	}
	cout << "\nHighest Score: " << numbers[6] << endl;
	cout << "Lowest Score: " << numbers[0] << endl;
}


Last edited on
my code wont work correctly

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 )

heres the code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;
// Function prototypes
void Number(); // Number Stack Different # 4-4
void RightTriangle(); // Right Triangle  # 4-6
void AboveAverage(); // Above Average # 5-2
void TwoDieSimulation(); // Two Die Simulation # 5-6 
void AccessGranted();  // Access Granted # 5-7
void OlympicJudging(); // Olympic Judging # 5-8
void Func7();

int diceRoll (){
int dice1, dice2;
int total;
// loop to simulate dice roll
dice1 = rand()%6+1;
dice2 = rand()%6+1;
total=dice1+dice2;
return total;
}


int main()
{
	int Choice;
	do {
		cout << endl <<"\t\tTeam Shuriken" << endl <<endl;
		cout << " 1. Number Stack Diiferent " << endl;
		cout << " 2. Right Triangle " << endl;
		cout << " 3. Above Average " << endl;    // not working properly !!CHECK!!
		cout << " 4. Two Die Simulation " << endl;
		cout << " 5. Access Granted " << endl;
		cout << " 6. Olympic Judging " << endl;
		cout << " 7. Func7 " << endl;
		cout << " 8. Quit the Program" << endl << endl;
		
		cout << "Enter number 1 - 8 for your Choice: ";
		cin >> Choice;
		
		if(Choice==1)Number();
		else if(Choice==2)RightTriangle();
		else if(Choice==3)AboveAverage();
		else if(Choice==4)TwoDieSimulation();
		else if(Choice==5)AccessGranted();
		else if(Choice==6)OlympicJudging();
		else if(Choice==7)Func7();
	}while (Choice != 8);
	system("cls");
	cout << " Thank You " << endl;
	system("PAUSE");
	return 0;
}
void Number() // Number Stack Different
{
	int SMB;
	for (int num = 1; num <= 9; num++){
		for (int num1 = 1; num1 <= num; num1++){
			SMB = num1;
			cout << SMB;
		}
		cout <<" " << endl;
	}
}
void RightTriangle() // Right Triangle
{
	for(int LineCount = 1; LineCount <= 10; LineCount++){
		for(int SpaceCount = 10 - LineCount; SpaceCount > 0; SpaceCount--){
			cout <<" ";
		}
		for(int CircleCount = 1; CircleCount < LineCount; CircleCount++){
			cout << "o";
		}
		cout << endl;
	}
}
void AboveAverage() // Above Average
{
	int numbers [5];
	double 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 <<"The following numbers are greater than the average: \n" << endl;
	for(i = 0 ; i <= 6; ++i){
	    if(average > numbers[i])
		    cout << numbers[i] ++;
	
	}
}
void TwoDieSimulation() //Two Die Simulation
{
	int rolls;
	int counter[12] = {0};
	int i, j;
	int value;
	float percentage[12] = {0};
	int diceValues [rolls];
	
	for (i = 0; i < rolls; i++)
	diceValues[i]= 0;

	cout <<"Please type a value for ROLL TO MAKE: ";
	cin >> rolls;
	
	j= rolls;
	
	for (i = 0; rolls >= i; ++rolls)
	{
	diceValues[i] = diceRoll();
	for (value=1 ; value<13 ; value++)
	{
	if (diceValues[i] == value)
	counter[value-1]++;
	}
	i++;
	}
	for(int k = 1; k < 13; k++){
	percentage [k] = (counter[k] / j) * 100;
	cout <<"The value " << k << " was rolled " << counter [12] << " times or " << percentage[12] << " % " << endl;

    }  

	cout <<"\nBelow are the results in histogram form: \n";
	for(int x = 1; x <= 12; x++){
		cout << x << " : ";
		for (int n = 1; n < counter[x]; --n){
			cout <<"o";
		}
		cout << endl;
	}
}	
void AccessGranted()  // Access Granted
{
	string inputUser, inputPass;
	int foundFlag, k;
	string userName [10];
	string passWord [10];
	
	for (k = 1; k <= 2; k++){
		cout <<"\nEnter the Username: ";
		cin >> userName[k];
		cout <<"Enter the Password: ";
		cin >> passWord[k];}
	cout <<"Input User: ";
	cin >> inputUser;
	foundFlag = false;
	for (k = 1; k <= 2; k++){
		if (inputUser == userName[k])
			foundFlag = true;
		cout <<"Input Pass: ";
		cin >> inputPass;
		if (inputPass == passWord[k])
			cout <<"Access granted.";
		else
			cout <<"Username and password do not match.";
	}
	if (foundFlag = false)
		cout <<"Username not Found";

}
void OlympicJudging() // Olympic Judging
{
	int numbers [6], average;
	int temp , i;
	for(i = 0; i < 6; i++){
		cout << "Please type a value for scores: ";
		cin >> numbers[i];
	}
	for(i = 0; i <= 6; i++){
		for(int j = 1; j < 6 + 1 ; j++){
			if(numbers[i] > numbers[j])
			{
				temp = numbers[i];
				numbers[i] = numbers[j];
				numbers[j] = temp;
			}
			average = ((numbers[6] / 5) + 1);
		}
	}
	cout << "\nHighest Score: " << numbers[6] << endl;
	cout << "Lowest Score: " << numbers[0] << endl;
	cout << " Average is: " << average << endl;
}
void Func7()
{
	cout << "Yet to be completes -- Member was Absent"<< endl;
}

Topic archived. No new replies allowed.