0
#include <iostream>
using namespace std;

int main(){
int i;
int size;
float Array[9];
float max;
float min;

cout << "Enter size of the array: ";
for(int i=0. i<5; i++){
cin << Array[i];
}

max = Array[1];
min = Array[1];

for(int i=0; i<size; i++){
if(Array[i] > max){
  max = Array[i];
}
if(Array[i] < min){
  min = Array[i]
}
}

cout << "Maximum value " << max << endl;
cout << "Minimum value " << min << endl;
return 0;

}

Just started to learn C/C++ and I am stuck trying to finding the minimum and maximum value of the input. Also I have to set the range at 9 inputs if its more or its a letter, then I have to display a warning to the user input certain amount.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This is C++, not C. – Barmar Feb 18 '20 at 07:36
  • 8
    `cin << Array[i];` `int i=0.` it's time to pick up a [good book](https://stackoverflow.com/q/388242/10411602) or two to learn the language, not just trying random things. Also please read [this](https://stackoverflow.com/q/1452721/10411602). – Blaze Feb 18 '20 at 07:37
  • 1
    You never set `size`. – Barmar Feb 18 '20 at 07:37
  • Array access starts with index 0 in programming languages. Not to be confused with array declarations, that look similar. `float Array[9];` means an array of 9 items, but to access the 9th item later on, you have to do `Array[8]`. Fix those bugs first. – Lundin Feb 18 '20 at 07:37
  • https://en.cppreference.com/w/cpp/algorithm/minmax , https://en.cppreference.com/w/cpp/algorithm/minmax_element – Jesper Juhl Feb 18 '20 at 07:39
  • You have a typo here: `for(int i=0. i<5; i++){` the `.` should be `;`. This code can't possibly compile and run. – Barmar Feb 18 '20 at 07:39
  • 1
    Closing this as too broad, since there's just too many basic errors. An answer has to teach you how to use arrays, loops, input/output, a compiler, a debugger. Essentially what the first 4-5 chapters of a C++ book will do. – Lundin Feb 18 '20 at 07:40
  • Closing might be an option, but *actually*, this question contains lots of *common mistakes* done by newbies. So, I guess it might be useful to keep this quesiton open for the other newbies. – Just Shadow Feb 18 '20 at 07:59

1 Answers1

1

Well, there are a bunch of wrong things done in that code.

So, here is the fixed version:

#include <iostream>
using namespace std;

int main(){
    //int i; // No Need of this. Inside the for loops we already declare int with (int i = 0)
    int size;
    // float Array[9];  // The array should be dynamically allocated based on the size
    float * Array;
    float max;
    float min;

    cout << "Enter size of the array: ";

    cin >> size; // This line was missing, added to get the size
    Array = new float[size]; // Initializing the array with the given sie

    // for(int i=0. i<5; i++){ // Instead of "." should be ";", also instead of 5 used "size" 
    for(int i=0; i<size; i++){
        //cin << Array[i]; // With cin use ">>" instead of "<<"
        cin >> Array[i];
    }

    // max = Array[1]; // Arrays always start with index 0 instead of 1
    // min = Array[1]; // Arrays always start with index 0 instead of 1
    max = Array[0];
    min = Array[0];

    for(int i=0; i<size; i++){
        if(Array[i] > max){
          max = Array[i];
        }
        if(Array[i] < min){
          // min = Array[i] // Semicolon was missing there
          min = Array[i];
        }
    }

    cout << "Maximum value " << max << endl;
    cout << "Minimum value " << min << endl;

    delete Array; // Deleting the dynamic array

    return 0;

}

P.S. I've commented the wrong parts in the original code, added some comments describing why they are wrong and added the correct ones right after those comments.

Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • 1
    It would be more C++ idiomatic to use an `std::vector` instead of a dynamically allocated C-array. Also, what happens if the entered `size` is negative? But other than that, a very nice answer with clear explanations. +1 – Frodyne Feb 18 '20 at 08:30
  • Agreed! Additionally I'd add size checks and rename the name of the Array (to avoid confusion). Here I've just tried to fix the code with the least amount of necessary changes. – Just Shadow Feb 18 '20 at 08:40