Manipulations with Arrays
You can also use for loops in arrays to find the sum of all elements:
12345678int numbers[4] = {42, 12, 56, 98}; int sum = 0; for (int i = 0; i <= 3; i++) { sum += numbers[i]; } cout << sum;
Here the variable sum will store the sum of all elements, initially, it’s equal to 0. Then we iterate through the array using for loops and add each element to the variable sum.
You can also store in arrays the data from the user:
int numbers[4];
int sum = 0;
for (int i = 0; i <= 3; i++) {
cout << "Type the number: ";
cin >> numbers[i];
sum += numbers[i];
}
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 3.7
Manipulations with Arrays
Sveip for å vise menyen
You can also use for loops in arrays to find the sum of all elements:
12345678int numbers[4] = {42, 12, 56, 98}; int sum = 0; for (int i = 0; i <= 3; i++) { sum += numbers[i]; } cout << sum;
Here the variable sum will store the sum of all elements, initially, it’s equal to 0. Then we iterate through the array using for loops and add each element to the variable sum.
You can also store in arrays the data from the user:
int numbers[4];
int sum = 0;
for (int i = 0; i <= 3; i++) {
cout << "Type the number: ";
cin >> numbers[i];
sum += numbers[i];
}
Takk for tilbakemeldingene dine!