Hey all,
I purchased the book 'coz I'm an eager learner of C++.
But I got stuck at arrays. Even though I think I understand how array works when I got to solve problems I got stuck on seemingly simple problem.
The problem is as follows:
Write a program that takes in 50 numbers and prints out the highest, the lowest, the average and then all 50 numbers, one per line.
This is my code:
The output fromCode:#include <iostream> #include <ctime> #include <cstdlib> int size; int theLowestNumber(int array[],int size) { int index_of_smallest_value; for (int i = 0; i < size; i++) { if (array[i] < array[index_of_smallest_value]) { index_of_smallest_value = i; } } std::cout << index_of_smallest_value; } int theGreatestNumber(int array[], int size) { int index_of_greatest_value; for (int i = 0; i < size; i++) { if (array[i] > i) { i = array[i]; index_of_greatest_value = i; } } std::cout << index_of_greatest_value; } void displayArray (int array[], int size) { std::cout << "{"; for (int i = 0; i < size; i++) { if (i != 0) { std::cout << ", "; } std::cout << array[i]; } std::cout << "}"; } int main() { int array[10]; srand(time(NULL)); for (int i = 0; i < 10; i++) { array[i] = rand() % 100; } theGreatestNumber(array, 10); std::cout << "\n"; theLowestNumber(array, 10); std::cout << "\n"; std::cout << "Array: "; displayArray(array, 10); std::cout << "\n"; }is giving me only the first number from the array despite clearly looping through the whole array and theCode:theGreatestNumber()function call is giving me a number that's not even in the array. Bizarre.Code:theLowestNumber()
I know this isn't telling anything good about my problem-solving skills ..... hopefully I'll get better with time.