+ 1
what is tjis code problem! #include <iostream> using namespace std; int main() { int b = 5; int arr[b] = {11, 35, 62, 555, 989}; int sum = 0; for (int x = 0; x < 5; x++) { sum += arr[x]; } cout << sum << endl; return 0; } I cannot make an array using int variable in order to define array's element number!?
7 Antworten
+ 7
You cannot assign a non constant value to the number of elements your array can have.
Use keyword const, in order to assign int b as a constant (note that you cannot manipulate the variable).
If you do not know how many elements you need in an array, use dynamic memory by using keyword new:
int *dArr = new int[x]
Where x is the max number of elements the array can hold.
In the example you have given, it seems int b is rather redundant. Just use int arr[5].
+ 2
causes you put b in int arr[b], remember its int, just write int arr[5]
0
Do you need use a MACRO of C lenguage to delcare the length of array, like #define MAX_ARRAY 5
0
u cant define int as an array element and dont put x<5 in for loop use the length keyword its a good practice
0
int is a keyword. which have a special meaning reserved in c++ so you can't use int as a variable
0
can any1 explain abv the pgm how came the output is 1652 i cannot understand the calculation
- 1
1,544