I decided to work on CodeAbbey exercises for some practice in C++. I'm currently working on Problem #15 (Maximum or Array), which tells me to: create a linear search on an array(of size 300), find the maximum and minimum values in the list, then print out the max and min.
It seems that I got everything BUT displaying the min, and I was wondering if you guys were able to point me in the right direction.
Here is my code so far:
#include <iostream>
using std::cout;
using std::cin;
int main() {
int arrayLength[300];
cout << "Please enter the numbers you would like to perform a linear search in: \n";
for(int i=0; i<=300; i++) {
cin >> arrayLength[i];
}
//Store the current maximum in a separate variable
int max=arrayLength[0];
int min=arrayLength[0];
for(int i=0; i<=300; i++) {
if(arrayLength[i] > max) {
max = arrayLength[i];
} else if(arrayLength[i] < min) {
min = arrayLength[i];
}
}
cout << "\n" << max;
cout << "\n" << min;
return 0;
}
Now when I run it, the code executes and prints the maximum number but not the min. How can I fix this?