arrays!


Hi,

I got these errors ,how to solve them?
error C2057: expected constant expression
: error C2466: cannot allocate an array of constant size 0
: error C2133: 'array1' : unknown size
: error C2065: 'array1' : undeclared identifier



here is my program :


#include<iostream>
using namespace std;

int main()
{
int size;

cout << "Enter the size of the array: " << endl;
cin >> size;

if (size > 100)
cout << "The maximun size of the array is 100 " << endl;
else

int array1[size];

double value;
int i;
for (i=0;i<size;i++)
{
cout << "Enter a value for element " << (i+1) << endl;
cin >> value;

array1[i] = value;

}

return 0;
}
You declare array1 in an else statement, which means that the scope of array1 will be that else, which means that array1 will be destroyed when you exit the else statement's scope.
The scope is basically the curly braces in which an object is declared. So, if you declare object1 in a function called ExampleFunction, you can't use object1 outside of that function. Read more here:
http://msdn.microsoft.com/en-us/library/b7kfh662(v=vs.80).aspx

Here is a fixed code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
 using namespace std;

 int main()
{
 int size;

 do
 {
    cout << "Enter the size of the array: " << endl;
    cin >> size;

    if (size > 100)
    cout << "The maximun size of the array is 100 " << endl;
 }
 while (size > 100);   // This asks the user to input a size until it is smaller or equal to 100

  int array1[size];

 double value;   // No use declaring as double if the array is declared as int
 int i;
 for (i=0;i<size;i++)
 {
 cout << "Enter a value for element " << (i+1) << endl;
 cin >> value;

 array1[i] = value;

 }

 return 0;
 }


thanks ,
but I don't want to repeat asking about the size I want only
one value for the size which should be less than 100

You don't want to ask again if the value inputted is over 100? Then just delete the do while loop, and you'll get your program.


aha ,

Ok,

the question said to call another function to rarrange the values in increasing order
I know how to call a function , But, I don't know how to do the increasing order??

e.g :

void rearrange(int values[], int length)
{

what to do ?

}


,
Think on it for some time. If you can't figure it out, read here:
http://en.wikipedia.org/wiki/Sorting_algorithm
Topic archived. No new replies allowed.