-1

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?

Drise
  • 4,310
  • 5
  • 41
  • 66
kbg2o
  • 15
  • 3
  • 5
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – NathanOliver Mar 13 '18 at 20:44
  • 8
    `i<=300` is causing undefined behavior. You'll execute the body of both for loops with `i == 300` and read/write to `arrayLength[300]` which you do not own. – scohe001 Mar 13 '18 at 20:46
  • 2
    Prefer [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to online resources when learning the language. – Ron Mar 13 '18 at 20:46
  • 2
    Unable to reproduce (after Undefined Behaviour that doesn't mean much, mind you) . Are you using a particular development environment? I've encountered IDEs where the debugging console did not flush correctly on program exit in the past. – user4581301 Mar 13 '18 at 20:55
  • 1
    Undefined behavior notwithstanding, it is unlikely to cause the issue you claim to observe. My crystal ball is telling me that you do see your `min`, but it is glued to command prompt, becayse there is no new line. Try outputting new line after min (and fix the range of iteration, of course!) – SergeyA Mar 13 '18 at 20:58
  • [Can not reproduce](https://www.ideone.com/90tVX2). Voting to close as unreproducible. – Drew Dormann Mar 13 '18 at 21:03

2 Answers2

1

The line for(int i=0; i<=300; i++) looks to be incorrect. Your array int arrayLength[300] contains 300 ints. If you count from 1 to 300 there are 300 numbers, but your loop counts from 0 to 300, which is 301 numbers.

The highest index into an array of length n is actually n-1 because the first array index is 0.

int arr[3];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4; // arr holds 3 ints not 4!

Also, the code printing the values might not be exactly what you expect either.

cout << "\n" << max;
cout << "\n" << min;

If we draw \n to show where your new lines are we get

\n
MAX\n
MINotherstuffblahblah

Changing your code to

cout << max << "\n";
cout << min << "\n";

Results in

MAX\n
MIN\n
otherstuffblahblah

making it easier to see your minimum value.

  • Thank you! Now it seems that I'm running into a problem of actually not getting an output. I am copying and pasting the given data that's supposed to be ran from Code Abbey (there are 300 numbers stored in the array. Numbers such as: 52988 48745 -55072 -54505 etc etc) but it isn't actually spitting out the results. I tried to change the array size to 4 and input my own numbers and it gave me the answers. What am I doing wrong? Is it because of the array size? – kbg2o Mar 14 '18 at 15:34
1

Change both of the <= to <. The array indices of an array of size 300 run from 0 to 299. When I ran it, the min was right, but the max was wrong. When you index out of bounds, results are not predictable. "Undefined behavior" it's called.

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65