Put elements value in array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Put elements value in array

In example we can put value 42 in all elements. What if i want to put various values for various elements? What will be the code?

15th Mar 2017, 4:20 PM
Shuvam Pal
Shuvam Pal - avatar
4 Answers
+ 15
Did somebody call me?
15th Mar 2017, 4:35 PM
Jafca
Jafca - avatar
+ 14
Depends on how your values vary. For your first query where all elements be 42: // assuming int array of size 10 for (int i = 0; i < 9; i++) { array[i] = 42; } // for other cases where you need to manually set the values to other values, you can always do it the manual way, or by initiating those values directly in the array declaration line.
15th Mar 2017, 4:27 PM
Hatsy Rei
Hatsy Rei - avatar
+ 14
int array_size; cin >> array_size; int array[array_size]; for (int i = 0; i < array_size; i++) { cin >> array[i]; } // left out on the program couts. Please implement them. =^=
16th Mar 2017, 1:02 AM
Hatsy Rei
Hatsy Rei - avatar
0
But can i do it by for loop. Or suppose i just tell the size of array and i want user will define arrays elements. And the result will show. How can i implement that?
15th Mar 2017, 4:43 PM
Shuvam Pal
Shuvam Pal - avatar