Array Functions Issue (srand)

Hello,

My code below is printing the same number (which is negative and shouldn't be with the if and while statements I put to filter them out?) 50 times. And it's not printing odds numbers at all. I plan to revisit my code tonight...for now I take a break but any hints as to what is going wrong is appreciated. I hope the comments in my code help you understand my thought process for the algorithum. I have a feeling the issue lies with syntax errors, maybe my variable declarations and my placement of 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

#include <iostream>
#include <time.h>
using namespace std;

//function declarations
void random_fill(int arr[]);
//populate the array by random numbers.
//produce random numbers between 10 and 100.
void print_array(int arr[], int size);
//print out the array and nothing else
void print_odd(int arr[], int size);
//declare an array and print out all odd elements.
void print_even(int arr[], int size);
//declare an array and print out all even elements.

//Main function: 
//declare an array of size 50.
//if the array element is an odd number go to function 3, otherwise go to function 4:

int main()

{	
	int size = 50;
	int i = 0;
	int arr[50];//0-49 is 50 elements in array
			
	//function calls begin
	srand((unsigned)time(NULL));//initialize seed is (time(NULL))
	//seeds random number generator with current time
	int num = arr[i] % 2;//remainder of element when it's divided by 2
	do//perform these functions while the array element is greater than 0.
	{	
		print_array(arr, size);//print out the array
		//Now check if the array element is an odd number
		//if odd, print_odd function, if even print_even function
		if (num == 0)
			print_even(arr, size);
		else
			print_odd(arr, size);
	} while (arr[i] > 0);
	return 0;
}

//function definitions begin
void random_fill(int arr[])
{
	for (int i = 0; i < 50; i++)
	{
		arr[i] = (rand() % 100) + 10;
		//generate random numbers between 10 and 100
		cout << endl;
	}
}

void print_array(int arr[], int size)
{
	for (int i = 0; i < 50; i++)
	{
		cout << "Items In Element " << arr[i] << endl;
		//print all the item in the element
	}
}

void print_odd(int arr[], int size)
{
	for (int i = 0; i < 50; i++)
	{
		cout << "Odd numbers are " << arr[i] << endl;
		//print all odd numbers in element
	}
}

void print_even(int arr[], int size)
{
	for (int i = 0; i < 50; i++)
	{
		cout << "Even numbers are " << arr[i] << endl;
		//print all even numbers in element
	}
}
Line 31: You're referencing arr[i], arr[i] is uninitialized (garbage).

Line 34: You're trying to print the array, but the array is uninitialized.

Looks line you intended to call random_fill(), but never did.

Line 37: You're testing num to see if an element is odd or even, num was set outside the loop based on whether the garbage content was even or odd.

Lines 38,40: These lines will print the entire array. Compare these functions to print_array(). There is no difference except the prompt.

Lines 69, 78: There is no check here if an element is even or odd.

Line 32-41: This loop is not needed. You need to check if an element is even or odd inside print_even or print_odd.



Last edited on
Thank you, this is my new code. Can you tell me if I initialized the array correctly? I am still getting the same sorts of problems but I think I'm closer.

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
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;

//function declarations
void random_fill(int arr[]);
//populate the array by random numbers using random numbers generator.
//Numbers should not be less than 10 or greater than 100.
//produce "size" random numbers between 10 and 100.
void print_array(int arr[], int size);
//print out the array
void print_odd(int arr[], int size);
//declare an array and print out all odd elements.
void print_even(int arr[], int size);
//declare an array and print out all even elements.

//main function
//Main function: declare an array of size 50.
//if the array element is an odd number go to function 3, otherwise go to 4:

int main()

{
	int size = 50;
	int i = 0;
	int arr[50] = { i };//0-49 is 50 elements in array and initialize
			
	//function calls begin
	random_fill(arr);//fill array with random numbers
	print_array(arr, size);//print out the array
	print_even(arr, size);//print all even numbers in element
	print_odd(arr, size);//print all odd numbers in element
		
	return 0;
}

//function definitions begin
void random_fill(int arr[])
{
	for (int i = 0; i < 50; i++)
	{
		srand((unsigned)time(NULL));//initialize seed is (time(NULL))
		//seeds random number generator with current time
		arr[i] = (rand() % 100) + 10;
		//generate random numbers between 10 and 100
		cout << endl;
	}
}

void print_array(int arr[], int size)
{
	for (int i = 0; i < 50; i++)
	{
		cout << "Items In Element " << arr[i] << endl;
		//print all the item in the element
	}
}

void print_odd(int arr[], int size)
{
	for (int i = 0; i < 50; i++)
	{
		//Now check if the array element is an odd number
		//if odd, print_odd function, if even print_even function
		int num = arr[i] % 2;//remainder of element when it's divided by 2
		if (num > 0)
		cout << "Odd numbers are " << arr[i] << endl;
		//print all odd numbers in element
	}
}

void print_even(int arr[], int size)
{
	for (int i = 0; i < 50; i++)
	{
		//Now check if the array element is an even number
		//if odd, print_odd function, if even print_even function
		int num = arr[i] % 2;//remainder of element when it's divided by 2
		if (num == 0)
		cout << "Even numbers are " << arr[i] << endl;
		//print all even numbers in element
	}
}
Line 43: srand() should be called one and only once at the start of your program. You don't want to want to reinitialize the random number generator each time through the loop. Move to line 28.

Line 45: Your formula is defective. This will generate numbers from 10 - 110. That's not what you want according to your comment.

Line 47: Is going to generate 50 line feeds. Don't think you intended that.

Other than the above, your code looks like it should work.

Some comments on style that don't currently affect your program.

Line 25: Should be const int size = 50;

Line 27: Your can use a const int to declare the size of your array.
1
2
  const int size = 50;
  int arr[size]; 


Line 39: You don't pass size to random_fill. size is hard coded inside random_fill. You should consider passing size as an argument so that random_fill can fill an array of any size.

Line 51, 60, 73: You pass size to these routines, but don't use it. These routines are hard coded to assume arr is 50. Since you're passing size as an argument, you should use it. Consider that if you want to change the size of the array at line 27, you have multiple places in your program that you would have to modify.

You say "I am still getting the same sorts of problems". Please be specific. I ran it after making the above changes and got the expected results.
Last edited on
Thanks AbstractionAnon, I starting out doing as you said...actually I wanted the user to be able to input the array size and used the syntax I found on this site: int arr* = new int (or whatever the correct syntax is) so that is why I included the size arguments in the functions. When my program kept outputting the same numbers over and over again I thought I would make things less complex to zone in and find the issues and so then I decided to hard code a size for the array (but in a rush/frustration forgot to remove the size arguments).

I also found out that srand((unsigned)time(NULL)); placed above the for loop in the random_fill function works as well...my end goal with this was to let user input size and keep main clear except for function calls.

Thanks for checking it out so quick!



Topic archived. No new replies allowed.