Answer the question
In order to leave comments, you need to log in
How to add all elements of an array without using a C++ loop?
There is such a code, you need to add all the elements of an array without using a loop. Help me please.
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
int main() {
int sum;
int students[3] = {
1,
4,
7
};
sum = ?????????????;
printf("answer = %d", sum);
return 0;
}
Answer the question
In order to leave comments, you need to log in
#include <iostream>
#include <numeric>
int main (){
const int arrayLength = 3; // раз уж вы пользуетесь массивом, то и длину точно знаете
int students[arrayLength] = { 1, 4, 7 };
int sum = std::accumulate(students, students + arrayLength, 0);
std::cout << "The array sum is " << sum << std::endl;
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question