Could someone take a look to see if I made these arrays correctly?

We were given a starter program with the instructions below to write loops for an array. This is my final output and I wanted to see if someone could take a look and see if I wrote these loops for the arrays correctly. I am unclear about what the output should really look like and could use some fresh eyes.
Thanks in advance


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
/ * The program generates 500 numbers between 1 and 6
*     and stores them in an array
* The program loops and counts how many times each number occurs*/
*/
#include <iostream>
#include <cmath>
using namespace::std;
void generateData(int [] , int count);

int countValue(const int [], int howMany, int value);

void main()
{
	int data[500];
	
	generateData(data, sizeof(data)/sizeof(data[0]) );	// call the function to generate 500 numbers
								//   between 1 and 6
								// store the values in the data array
	
	for ( int i = 1 ; i <= 6 ; i++ )
	{
		cout << i << " " << countValue(  data, sizeof(data)/sizeof(data[0]), 4       ) << endl; // call the
													//countValue function
	}
}



void generateData(int d[], int cnt)
{
    srand(12345);
	int dice;
	for(int i = 0; i < cnt ; i++)
	{
		dice =       rand() % 6+1      ; // generate a random number between 1 and 6
		d[i] = dice;
	}
 }

int countValue(const int d[], int howMany, int value)
{
	int total = 0;
	for( int i = 0; i < howMany ; i++)
	{
		if (    i == value )	// adjust the total if number in cell i is
			{
				total++;
			}//   equal to the parameter value
	}
	return total;
}

/*

Output

1 1
2 1
3 1
4 1
5 1
6 1
Press any key to continue . . .

*/
 
sizeof(data)/sizeof(data[0])

Playing with pointer arithmetic is generally not a good idea. Just put in 500.


As for the output it probably means you store your data in a histogram. In other words I don't think your CountValues function is going to work. Accordingly, I would also pass another array that contains 6 index spots in order to contain that histogram.

https://en.wikipedia.org/wiki/Histogram


kingkong200 replied as I was writing you an example :)

Anyway, here is a much shorter version without the need for the functions etc. Just set the NUM_COUNT to the number of generated numbers you require and compile it.

If you have any questions please don't hesitate to ask.

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
#include <iostream>
using namespace std;

const int NUM_COUNT = 10;	        // how many numbers do we want to generate.

int genNumbers[NUM_COUNT];	// set array to hold numbers
int numCount[6];			        // serves as a storage for the 6 possible numbers.

int main()
{
	int dice;

	// clear our count array to zeros as we will be updating
	// them as the numbers jump out from our rand function.
	memset(numCount, 0, sizeof(numCount));

	// loop and generate numbers set in NUM_COUNT and
	// storing the results in genNumbers array, each
	// result in dice is used to access the numCount
	// array and increases by 1.
	for (int count = 0; count < NUM_COUNT; count++)
	{
		dice = rand() % 6 + 1;
		genNumbers[count] = dice;
		numCount[dice-1]++;			// since we can only have 1 - 6, subtract 1 as arrays start from 0		
	}

	// Display numbers generated - for debugging if you want to check  :)
//	for (int i = 0; i < NUM_COUNT; i++)
//		cout << "Number " << genNumbers[i] << endl;

	cout << endl<<endl;

	// Display the numbers and how many times they came out.
	for (int i = 0; i < 6; i++)
		cout << "Number " << i + 1 << " came out " << numCount[i]<<" times."<<endl;

	return 0;
}
Unfortunately, the functions for this assignment have to stay so I have to include them.
Well you should at least get a good idea on how to achieve your results using my example code as a reference.
I'm not sure why I am having such a heck of a time with arrays
Still not seeming to be able to get the right information for calling the functions.

Yes, learning arrays sometimes can be a little tricky at first. A good source of information is right here on this website:

http://www.cplusplus.com/doc/tutorial/arrays/

Anyway, had a little time this morning and decided to split it up a little (with a few more comments) and hopefully will give you a little idea what's needed.

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

#include <iostream>
using namespace std;


const int NUM_COUNT = 10;	        // how many numbers do we want to generate.

// function declarations
void generateData(int data1[], int cnt);
void countValue(int data1[], int data2[], int cnt);

int main()
{
	// our arrays
	int genNumbers[NUM_COUNT];		// set array to hold numbers
	int numCount[6] = {0,0,0,0,0,0};		// serves as a storage for the 6 possible numbers.

	int dice;

	// build the array with numbers between 1 and 6
	generateData(genNumbers, NUM_COUNT);

	// Display numbers generated - for debugging if you want to check  :)
	for (int i = 0; i < NUM_COUNT; i++)
		cout << "Number " << genNumbers[i] << endl;

	cout << endl << endl;

	// now lets count those numbers generated...
	countValue(genNumbers, numCount, NUM_COUNT);

	// at this point the numCount array has 6 counters stored
	// for the 6 possible numbers.  Array indexes start at 0
	// therefore...  numCount[0]..numCount[1]..numCount[2] etc..

	// Because we have numbers between 1 to 6 we subtracted 1
	// from the index (generated by the rand) so that we would
	// correctly land in index 0..  with that it mind, 1 - 6
	// is actually 0 - 5 in the array.

	// lets check this...
	for (int i = 0; i < 6; i++)
		cout << "Number " << i + 1 << " came out " << numCount[i] << " times." << endl;

	return 0;
}

//
//	$function:		Write a batch of numbers betwwen 1 and 6 to an array
//					
void generateData(int data1[], int cnt)
{
	// data1 is the array of numbers passed to the function.
	int dice;
	for (int count = 0; count < cnt; count++)
	{
		dice = rand() % 6 + 1;
		data1[count] = dice;
	}
}

//
//	$function:		Check the number of times a given value has generated
//					
void countValue(int data1[], int data2[], int cnt)
{
	// data1 is the array of numbers passed to the function.
	// data2 is the array holding the total number for each number
	int numValue;
	for (int count = 0; count < cnt; count++)
	{
		// grab the value from the numbers array and use that
		// as an index to our total number array. 
		numValue = data1[count];	// get the number generated at that position
		data2[numValue - 1]++;		// array indexes start at 0, our number is between 1 and 6
									// so we subtract one so we are indexed correctly.
	}
}


Last edited on
Can I ask why you would pass 2 arrays in countValue? Additionally why the type for the countValue function is void instead of int?
data1[] is the array of numbers we generated from the rand function.
data2[] is the array holding the total of each number generated.

Since I passed genNumbers into data1 and numCount into data2 when I called the function those arrays are referenced and/or updated by the function so really no need to return a value from the function as its updating the arrays internally.

As stated above the data2[] is pointing to numCount, we use the number generated in data1's current index to reference the index of the numCount array and increment it - remember that arrays start at index 0 so we needed to subtract 1 from whatever was in data1 so we indexed correctly :)

so...

numCount[0]...[1]...[2]...[3]...[4]...[5] = 1 to 6

would store the total number of generated numbers in the following:

numCount[0] = total of number 1's
numCount[1] = total of number 2's
numCount[2] = total of number 3's

...and so on.

Topic archived. No new replies allowed.