arrays

Pages: 12
Im trying to compile the code but it gives me certain errors and im also trying to fix certain certains parts of the source code but couldn't figure it out: here are the certain parts:
my code wont work correctly

On option 4 it must show the results of how many dice have rolled and then display in histogram form and also the and goes in never ending loop;

for example: if dice rolled 7 times it should show in histogram 1: ooooooo

On option 3 the average calculation doesn't correctly work and it should display the numbers that are greater than the average
On option 6 highest gives me in negatives and also calculate average of only four numbers ( excluding lowest and highest )

HERES THE SOURCE 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
#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 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 = diceRoll();
		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;
	}
}	
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 + 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;
}
void Func7()
{
	cout << "Yet to be completes -- Member was Absent"<< endl;
}
Last edited on
need help
Some obvious problems:

Line 87: You're indexing out of bounds. numbers is declared with a size of 5. This means the elements are 0 - 4. You're accessing elements 1 - 6.

line 89-90: What are you expecting the value of i to be here? In order to exit the for loop i must be > 6. Again an out of bounds reference. When you use a for loop index outside of the loop, you're pretty much guaranteed not to be making a valid reference.

lines 93-95: Same problem with out of bounds references.

line 103: srand() should only ever be called once at the beginning of your program. Calling it multiple times will cause the random number generator to return the same sequence of random numbers.

line 109: Again a possible out of bounds reference. diceRoll can return 12. Elements of counter are 0 - 11.

Line 111, 117: ditto.

Lines 158, 162: Again out of bounds references.
Here is some updated code:
On option 4 when the dice is rolled it gives the result of all values same
for example: if dice rolled 7 times it should show in histogram 1: ooooooo, the second value : if dice rolled 3 times it should show in histogram 1: ooo

On option 3 the average calculation doesn't correctly work and it should display the numbers that are greater than the average

On option 6 my lowest is working correctly but highest gives me in negatives and also calculate average of only four numbers ( excluding lowest and highest )


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
#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 = 0, count = 0;
	
	for(i = 0; i <= 4; 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 <= 4; i++){
	    if(average > numbers[i])
		    cout << numbers[i] ++;
	
	}
}
void TwoDieSimulation() //Two Die Simulation
{
	int myArray [12];
	int diceThrows = 0;
	srand(time(NULL));
	int dice;
	double percentage;
	cout <<"Please type a value for ROLL TO MAKE: " << endl;
	cin >> diceThrows[myArray];
	dice = ((1+ rand()%6) + (1+ rand()%6));
	for(int k = 1; k < 13; k++){
		cout <<"The value " << k << " was rolled " << dice << " times ";
		// calculate percentage
		percentage = ((double)dice/(double)diceThrows) * 100;
		cout <<" or " << percentage << " %" << endl;
	}
	cout <<"\nBelow are the results in histogram form: \n";
	for(int x = 1; x < 13; ++x){
		cout << x << " : ";
		for (int n = 1; n <= dice; 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 < 5; i++){
		cout << "Please type a value for scores: ";
		cin >> numbers[i];
	}
	for(int i = 0; i < 5; i++){
		for(int j = 0; j < 5+ 1; j++){
			if(numbers[i] < numbers[j])
			{
				temp = numbers[i];
				numbers[i] = numbers[j];
				numbers[j] = temp;
			}
		}
		// average must be calculated for for numbers only excluding the highest and lowest numbers 
		// and my highest number goes into negative.
	}
	cout << "\nHighest Score: " << numbers[6] << endl;
	cout << "Lowest Score: " << numbers[0] << endl;
}
void Func7()
{
	cout << "Yet to be completes -- Member was Absent"<< endl;
}
line 90: see my previous comment. Also, you're doing integer arthimetic here.

line 103: see my previous comment.

line 107: dicethrows is not an array and why are you indexing it by an array (pointer)?

Line 109: What's the purpose of the loop. You're executing lines 110-114 12 times, but doing the same thing each time through the loop.

Line 118: Why is dice (a random roll), the upper limit of your inner loop?




can you show some example from line 107 to line 118 form your comments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int point;  // point to make
  int myArray [13];  // We'll ignore [0] and [1] since they can't be rolled

  for (int i=0; i<13; i++)
    myArray[i] = 0;   // Zero the array
  cout <<"Please type a value for ROLL TO MAKE: " << endl;
  cin >> point;
  do   // Roll until we make the point
  { dice = diceRoll();
     myArray[dice]++;  // increment count
     diceThrows++;      // Total rolls
  } while (dice != point);
  for (int k = 2; k < 13; k++)
 {  cout <<"The value " << k << " was rolled " << myArray[k] << " times ";
    // calculate percentage
    percentage = ((double)dice/(double)diceThrows) * 100;
    cout << " or " << percentage << " %" << endl;
  }

i followed you steps but when i enter any number nothing happens and also in your example from where did you get diceThrow++ and what does that do
The code I posted does not check for a valid value of point. The user must enter a value between 2 and 12. Anything outside of that range will cause the do/while to run forever since it is impossible for diceRoll to return a value that is < 2 or > 12.

diceThrows is the int at line 102 in your code. It counts the number of times the dice have been rolled. This is needed at line 16 to calculate the percentage.

option 4
i think i confused you the int myArray [12] at line 101 in my code keeps 12 elements, i was checking nothing goes there extra from line 109 to 110 of my code but the dice roll can have its value till 400, so the user can enter 1 to 400 from the lines 106 to 107 from my code.

On option 3 the average calculation doesn't correctly work and it should display the numbers that are greater than the average

On option 6 my lowest is working correctly but highest gives me in negatives and also calculate average of only four numbers ( excluding lowest and highest )
the int myArray [12] at line 101 in my code keeps 12 elements

Yes, I understand that. It keeps track of how many times each number has been rolled. Your code fails to initialize the array. See my lines 4-5.

As I pointed out previously, your line 107 is defective.
 
cin >> diceThrows[myArray];

diceThrows is not an array and myArray is a pointer. That line will NOT work.

As I understood option #4, the object was to roll the dice until the user's point (value) was made (like craps) and then display a histogram of how many times each number was rolled. Your code does not do that.

but the dice roll can have its value till 400

Where does the 400 come from? That's not in the original problem statement. The value of a diceRoll is 2-12. Your code rolls the dice once at line 108. You then execute the following line 12 times varying k:
cout <<"The value " << k << " was rolled " << dice << " times ";
Note that the value of dice does not change and it it NOT the number of rolls of a particular number. Lets assume the value of dice from line 108 was 7:
The value 1 was rolled 7 times // This isn't even possible
The value 2 was rolled 7 times
...
The value 12 was rolled 7 times

This is why it's necessary to increment the array indexed by what was rolled (my line 10).

The following line is also defective:
 
percentage = ((double)dice/(double)diceThrows) * 100;

Assuming again that 7 was rolled, this will divide 7 by 0 (diceThrows was never incremented).

Your display of the histogram is also defective. Again assuming 7 was rolled at line 108, your histogram will display 7 "o"s for every value from 1-12.




Last edited on
That doesn't change anything I posted above except to put an upper limit on the number of rolls.
can you help me show the corrections that has to be made.
can you help me figure out what is causing the problem and also on the same link there is 5-8 problem
here is the new 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
void TwoDieSimulation() //Two Die Simulation
{
	int myArray [12];
	int diceThrows, point;
	//srand(time(NULL));
	int dice;
	double percentage = 0;
	
	for (int i = 1; i < 13; i++)
		myArray[i] = 0;   // Zero the array
	cout <<"Please type a value for ROLL TO MAKE: " ;
	cin >> point;
	do{
		dice = diceRoll();
		myArray[dice]++;  // increment count
		diceThrows++;      // Total rolls
	} while (dice != point);
	for(int k = 1; k < 13; k++){
		cout <<"The value " << k << " was rolled " << myArray[k] << " times";
		cout <<" or " << percentage << " %" << endl;
		for(int n = 0; n < k; n++){
		// calculate percentage
			percentage = ((double)dice/(double)diceThrows) * 100;
		}
	}
	cout <<"\nBelow are the results in histogram form: \n";
	for(int x = 1; x < 13; ++x){
		cout << x << " : ";
		for (int n = 1; n <= dice; n++){
			cout <<"o";
		}
		cout << endl;
	}
}
That's better. Still a few problems.

Line 3: Dice rolls are 2-12. You're allocating 12 entries (0-11). myArray[12] is out of bounds. MyArray should be declared as myArray[13];

Line 18: A roll of 1 is not possible. Loop should start at 2.

Line 20: percentage is displayed, but has not been calculated yet.

Line 21: What is the purpose of this loop? Not needed. Nothing in the loop depends on the loop variable n.

Line 27: A roll of 1 is not possible. Loop should start at 2.

Line 29: You don't want to use dice here. You want to use the value in the array.
 
  for (int n = 1; n <= myArray[x]; n++)





i have fixed option 6 by looking at your examples but one problem the highest number results in negative number.
option 4 updated but when i enter 8 it shows only 5 and the percentage gives 160%, and what happens if the users enters 400 as shown in the problem. how is that possible..
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
void TwoDieSimulation() //Two Die Simulation
{
	int myArray [13];
	int diceThrows = 0, point;
	//srand(time(NULL));
	int dice;
	double percentage = 0;
	
	for (int i = 1; i < 13; i++)
		myArray[i] = 0;   // Zero the array
	cout <<"Please type a value for ROLL TO MAKE: " ;
	cin >> point;
	do{
		dice = diceRoll();
		myArray[dice]++;  // increment count
		diceThrows++;      // Total rolls
	} while (dice != point);
	for(int k = 2; k < 13; k++){
		cout <<"The value " << k << " was rolled " << myArray[k] << " times";
		// calculate percentage
		percentage = ((double)dice/(double)diceThrows) * 100;
		cout <<" or " << percentage << " %" << endl;
	}
	cout <<"\nBelow are the results in histogram form: \n";
	for(int x = 2; x < 13; ++x){
		cout << x << " : ";
		for (int n = 1; n <= myArray[x]; n++){
			cout <<"o";
		}
		cout << endl;
	}
}
Last edited on
option 4 updated but when i enter 8 it shows only 5 and the percentage gives 160%, and what happens if the users enters 400 as shown in the problem. how is that possible.
Pages: 12