0

I wrote this simple code that should calculate the highest number of a series of array elements but the results are strange, i'm pretty sure it has something to do with the way I wrote but I can't figure out what I did wrong

I tried the same code on c++ shell and it gave me different results

#include <iostream>
using namespace std;
int main()
{
    int size,c,max;
    cout<< "enter the size of the array" << endl;
    cin>> size;
    int a[size];
    max=a[0];
    cout <<"fill the array" << endl;
    for(c=0; c<size; c++){
        cin>>a[size];
        if(max<a[size]){
            max=a[size];
        }
            }
   cout<<"the highest number is:"<<max;
}

enter the size of the array 5 fill the array 1 2 3 4 5 the highest number is:27 Process returned 0 (0x0) execution time : 14.653 s Press any key to continue.

enter the size of the array 7 fill the array 1 2 3 4 5 6 7 the highest number is:27 Process returned 0 (0x0) execution time : 14.653 s Press any key to continue.

enter the size of the array 8 fill the array 1 2 3 4 5 6 7 8 the highest number is:8 Process returned 0 (0x0) execution time : 5.915 s Press any key to continue. (if the size of the array is over 8 it works)

Stack Danny
  • 7,754
  • 2
  • 26
  • 55
Darkus
  • 3
  • 1
  • `cin>> size; int a[size];` -- This is not valid C++. Arrays in C++ must have their size denoted by a constant expression, not a variable. Use `std::vector a(size);` instead. – PaulMcKenzie Jul 13 '19 at 21:34
  • 1
    Possible duplicate of [Array\[n\] vs Array\[10\] - Initializing array with variable vs real number](https://stackoverflow.com/questions/15013077/arrayn-vs-array10-initializing-array-with-variable-vs-real-number) – Bitcoin M Jul 14 '19 at 00:26

3 Answers3

0

You loop is wrong because you use the size of the array instead of the loop index c. This is how your code should look.

for(c=0; c<size; c++){
    cin>>a[c];
    if(max<a[c]){
        max=a[c];
    }
}

The other error you have is that you use the value of the first element in the array before you have given it a value.

int a[size];
max=a[0]; // at this point a[0] does not have a value

You should recode like this

cout <<"fill the array" << endl;
cin>>a[0];
max=a[0];
for(c=1; c<size; c++){
    cin>>a[c];
    if(max<a[c]){
        max=a[c];
    }
}

Now a[0] is only being used after you've given it a value, and the loop has been changed to start at c=1.

john
  • 85,011
  • 4
  • 57
  • 81
0

I'm not familiar with C++, but i'll point the issue you have :

for(c=0; c<size; c++){
    cin>>a[size];
    if(max<a[size]){
        max=a[size];
    }
        }
cout<<"the highest number is:"<<max;

this is always putting the input into the last item of your array, thus you don't keep track of the input anymore (if you need it), you should put your loop variable into the index for the max comparison and array storing operation.

Another issue is that you use max=a[0]; but it's has not been already filled. This is can be a deal when you are using negative value or depending of the implentation of the language.

Another issue I saw (i'm unsure but happend to me in C), is that you are creating an array like this :

int size,c,max;
cout<< "enter the size of the array" << endl;
cin>> size;
int a[size];

If i'm correct (from my C experience), this shouldn't be done this way, because your array is created at compile time, but the value of size is not already initialized, writing somewhere in memory where you shouldn't write. This might cause the "27" issue by the way.

Sami Tahri
  • 1,077
  • 9
  • 19
0

Would firstly recommend you create your array like this, it will initialise the array with 0 at each index, as accessing an empty index could produce undefined behaviour:

int a[size] = {0};

Secondly, you are using size as the index to your array in the loop, you should be using the value you are incrementing (c) to access each index. Maybe you could use the later standard of c++ and write it like this (using for each):

for (auto &singleInput : a) // access by reference to avoid copying
{  
    cin >> singleInput;

    if(max < singleInput)
    {
        max = singleInput;
    }
}
vmetelz
  • 87
  • 10