Arrays

I want to check if an array of numbers input by the user is
increasing. This happens if each element of the array contains a value that is larger than
the value contained in previous elements. The program will work as follows:
First, you should ask the user to enter the size of the array, by outputting:
“Enter the size of the array: ”
to the console. If the user enters an incorrect size, you should output the error message
“ERROR: you entered an incorrect value for the array size!”
and exit the program. If the input is a valid size for the array, ask the user to enter the
data, by outputting
“Enter the numbers in the array, separated by a space, and press enter: ”
Hints: Think about how to enter individual elements in an array. “cin” can only read one
word/number without space.
Once the input is complete, check if the array is increasing:
If it is, write “This IS an increasing array!” to the console output.
If it is not, write “This is NOT an increasing array!”.
Print your array in one line with elements separated by a space.


This is what I have so far.

#include <iostream>
using namespace std;
int max[arraysize],arraysize;
int main()

{


cout << "Enter the size of array: " ;
cin >> arraysize;
if (arraysize > 0)
{
for (int i = 0; i < arraysize; i++)
{
cout << "Enter the numbers in the array, separated by a space, and press enter: " ;
cin >> max[i];
if (max[i] == i)
{
cout << "This Is and increasing array!" ;
}
else
{
cout << "This is NOT an increasing array!" ;
}
}
}
else
{
cout << "ERROR: you entered an incorrect value for the array size!" ;
}

You define "max" and "arraysize" as global variables. Don't. It is neither necessary nor beneficial.

You do use "arraysize" before you have declared it. That is an error.
Furthermore, current C++ standard requires that the size of array is known at time of compilation. You don't assing a value to "arraysize" until runtime.

What are you allowed to use for "array"? This smells like dynamic allocation excercise. Pray tell, smart pointers are not forbidden?

You should test for the success of the input operation. Lets say that I write "zebra" for size of array. That is not a number. What is the value of arraysize in the test then? What it was before input operation? Initialization of variables is good.

Your test for increase does not cover all cases. Is {2, 4, 7} an increasing array? In your program 2!=0, 4!=1, and 7!=2.

The instructions of your homework tell to read first and test later. You do test after every number.

Is there direction in a series of one element? No. The minimum allowed size for the array is thus more than one.

The test is obviously '<'. What to test are two consecutive elements in the series.
Topic archived. No new replies allowed.