Basic Looping Issue

Hi there, pretty new to C++. I'm trying to write a program where I make two different arrays of random numbers, find the range (difference between largest and smallest number) in both arrays, and then take the array with the largest range, and print it on screen. I'm having issues with trying to find the array of both. I'm trying to create a loop to find the largest and smallest values of both arrays, and using those to calculate the range. However, whenever I input the code, I get an output of 0 and 999 for the small and large arrays, respectively. I'm looking around for logic errors I made, but I can't seem to find any. If anyone could give me a hand, I'd greatly appreciate it.

IDE is Dev C++ for anyone interested.

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
  #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void fillArray(int small[], int big[]);
void range(int mini[], int mega[]);

int main()
{
	int tiny[55];
	int huge[55000];
	
	fillArray(tiny, huge);
	range (tiny, huge);
	
	system("pause");
	return 0;
}

void fillArray(int small[], int big[])
{
	srand(time(0));
	
	int i, j;
	
	for (i=0; i <= 54; i++)
	{
		small[i] = (rand() % 1000) + 1;
	}
	
	for (j=0; j <= 54999; j++)
	{
		big[j] = (rand() % 1000) + 1;
	}
	return;
}

void range(int mini[], int mega[])
{
	int largeTiny, largeHuge = 0;
	int smallTiny, smallHuge = 1000;
	int rangeTiny, rangeHuge;
	int i, j = 0;
	
	while (i < 55)
	{
		if (mini[i] < smallTiny)
		{
			smallTiny = mini[i];
			i++;
		}
		else if (mini[i] > largeTiny)
		{
			largeTiny = mini[i];
			i++;
		}
		else
		{
			i++;
		}
	}
		
	while (j <= 54999)
	{
		if (mega[j] < smallHuge)
		{
			smallHuge = mega[j];
			j++;
		}
		else if (mega[j] > largeHuge)
		{
			largeHuge = mega[j];
			j++;
		}
		else
		{
			j++;
		}
	}
	
	rangeTiny = largeTiny - smallTiny;
	rangeHuge = largeHuge - smallHuge;
	
	cout << "The range for the smaller is " << rangeTiny << endl;
	cout << "The range for the larger is " << rangeHuge  << endl;
}
Last edited on
pretty new to C++.

Hope you are not doing a crappy course where they teach you old stuff.
To make things easier it's better to use small arrays with hard-coded values so you know what the correct results are. Once this is working do it with random values.

In modern C++ you shouldn't use plain arrays, prefer std::vector or maybe std::array.
Don't write you own code for the min and max value, use std::min_element and std::max_element instead.
Thank you! I am curious what you mean by "old stuff" though. I'm in pt.2 of my C++ course.
Arrays are old stuff for me. They go back decades - since the first version of C(?)
So are pointers, dynamic memory management with new and delete.

"Modern" is std::array since C++11, not sure about std::vector C++98(?)
Topic archived. No new replies allowed.