Why am I getting huge integers as the output for my code?

So I'm supposed to do the following:
bigger, a, and b are parallel arrays. function's job is to set each element of big to larger of the corresponding element of a and the corresponding element of b. all 3 arrays are the same size, each having els elements. For example, if a held {1, 2, 3, 4, 5, 6, 7} and b held {10, 8, 6, 4, 2, 0, -2}, and bigger were an array of 7 doubles, then then
setBigger( bigger, a, b, 7 ) would set the array bigger to hold
{10, 8, 6, 4, 5, 6, 7}
function to be used:
void setBigger( double bigger[], const double a[], const double b[], unsigned els );

However, when I run the function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void setBigger(double bigger[], const double a[], const double b[], unsigned els)
{
	double bignuma = a[0];
	double bignumb = b[0];
	for (int i = 0; i < els; i++) //getting big numbers from array a
	{
		if (a[i] > bignuma)
		{
			bigger[i] = a[i];
		}
	}
	for (int i = 0; i < els; i++) //getting big numbers from array b
	{
		if (b[i] > bignumb)
		{
			bigger[i] = b[i];
		}
	}
	cout << "Array bigger consists of "; //printing array bigger
	for (int i = 0; i < els; i++)
		cout << bigger[i] << ", ";
}


This is what I have in main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Start 7th Function
	cout << "Seventh Function" << endl;
	double arraya[7];
	double arrayb[7];
	double arraybigger[7];
	cout << "Enter 7 elements for the first array: ";
	for (int i = 0; i < 7; i++)
	{
		cin >> arraya[i];
	}
	cout << "Enter 7 elements for the Second array: ";
	for (int i = 0; i < 7; i++)
	{
		cin >> arrayb[i];
	}
	setBigger(arraybigger, arraya, arrayb, 7);
	return 0;


I get some pretty crazy numbers for the output. Can someone explain why I'm getting such huge numbers?
Last edited on
If a[i] < bignuma and b[i] < bignumb then bigger[i] won't get set to anything.

You're making this too complicated. All you really need is something like
bigger[i] = max(a[i], b[i]);
inside a loop.

I suggest that you move the code to print the array into a function. For testing, print both a and b arrays before calling setBigger() so you can make sure your input code is correct.
Topic archived. No new replies allowed.