### 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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788`` `````` #include #include #include 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.