简体   繁体   中英

adding all numbers inside an Array in c++

I just started learning C++ in college and my task is to do the following: I have to write some code that will use iteration (ie looping) to calculate the cumulative sum of the items in an array of integers; my code is:

int main() {
    int myArray[] = {1,2,3,4,5};
    int i;
    int j;
    j+= myArray[]; 
    for(i=0;i<5;i++){
    printf("%d\n",myArray[j]);
  }
}    

Although this code does not produce what I am looking for and I am confused as to what I should do next.

int main() {
    int myArray[] = {1,2,3,4,5};
    int sum = 0;
    for(int i=0; i<5; i++)
        sum += myArray[i] ;

    std::cout << sum;   
}

Here sum is initialized to 0 and each element in the array is added to the sum in a loop.

you can use std::accumulate to do the same, hence you dont worry about the size of the array.

#include <iostream>
#include <algorithm>

int main() {
    int myArray[] = {1,2,3,4,5};
    std::cout << std::accumulate(std::begin(myArray), std::end(myArray), 0);
} 

Note that std::begin() and std::end() were introduced in C++11 . For earlier versions, you will have to use pointers instead:

std::accumulate(myArray, myArray + 5, 0);

I've edited your code with comments and a line of code. please review them.

#include <cstdio>

int main() {
    // Array and index into it.

    int myArray[] = {1,2,3,4,5};
    int i;

    // Initialise sum to zero for starting.

    int sum = 0;

    // Adding whole array will not work (though it would be nice).
    // Instead, go through array element by element.

    // j += yArray[];
    for (i = 0; i < 5; i++) { 
        // Add element to sum and output results.

        sum += myArray[i];
        printf ("Adding %d to get %d\n", myArray[i], sum);
    }

    // Output final result.

    printf ("Final sum is: %d\n", sum);
}

Also note that I've used printf as per your question but you really should be using the C++ streams facilities for input and output.

The output of that code is:

Adding 1 to get 1
Adding 2 to get 3
Adding 3 to get 6
Adding 4 to get 10
Adding 5 to get 15
Final sum is: 15

Note that <algorithm> has a function for that:

const int myArray[] = {1,2,3,4,5};
const int sum = std::accumulate(std::begin(myArray), std::end(myArray), 0);

If you want to do the loop yourself, you may use the for-range (since c++11):

const int myArray[] = {1, 2, 3, 4, 5};
int sum = 0;

for (auto e : myArray) {
    sum += e;
}

You need to put j+= myArray[] inside the loop and put i inside [] of myArray in order to perform the summation operation. Thereby, your code could be modified as follows to be matched to what you want to do. After summation of all the elements in the array, it exits for-loop, and print the final summation as in the second printf . Note that j was replaced by sum in order to be readable.

int main() {
    int myArray[] = {1,2,3,4,5};
    int sum=0; // sum
    for(int i=0; i<5; i++){
        sum += myArray[i];
        printf("%d\n", myArray[i]);
    }
    printf ("Sum: %d \n", sum);
}

You can see a runnable code at this link . Hope this help.

int main() {
    int yourArray[] = {1,2,3,4,5};
    int sum = 0;
    for(int i=0; i<5; i++) {
        sum = sum + yourArray[i] ;
        std::cout << sum;   
    }
}

In the above code, the for loop will iterate 5 times, each time a value in the array will be added to the sum variable.

In the first iteration, the value of sum will be 0, and the value at yourArray[0] will be 1, so sum = 0 + 1; .

In the second iteration, the value of sum will be 1, and the value at yourArray[1] will be 2, so sum = 1 + 2; .

And so on...

After each iteration is complete, we output the sum , which will be 1, 3, 6, 10, 15.

So 15 is the complete sum of all the values of the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM