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!? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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!?

30th Jul 2016, 9:14 PM
Hesam Alinejad
Hesam Alinejad - avatar
7 Respostas
+ 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].
30th Jul 2016, 9:26 PM
Cohen Creber
Cohen Creber - avatar
+ 2
causes you put b in int arr[b], remember its int, just write int arr[5]
31st Jul 2016, 4:01 PM
yopi andriyansah
yopi andriyansah - avatar
0
Do you need use a MACRO of C lenguage to delcare the length of array, like #define MAX_ARRAY 5
30th Jul 2016, 9:42 PM
Leopoldo Javier Flores Contreras
Leopoldo Javier Flores Contreras - avatar
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
31st Jul 2016, 5:27 AM
Suhail Pappu
Suhail Pappu - avatar
0
int is a keyword. which have a special meaning reserved in c++ so you can't use int as a variable
31st Jul 2016, 9:31 AM
Vishal kumar mandal
Vishal kumar mandal - avatar
0
can any1 explain abv the pgm how came the output is 1652 i cannot understand the calculation
13th Nov 2016, 4:39 PM
prashanth jerry
prashanth jerry - avatar
- 1
1,544
31st Jul 2016, 4:41 AM
S.NAGA DURGA SAI BABU
S.NAGA DURGA SAI BABU - avatar