+ 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?
4 Respostas
+ 15
Did somebody call me?
+ 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.
+ 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. =^=
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?