0

I'm new to c++ but long story short , i want to write a c++ program that will accept input from user through an array and it will sum each array element input

 #include <iostream>
    #include <string>
    #include <math.h>
    using namespace std;
    int main() {
        int array[100];
        int sum;
    for(int i=0; i<100; i++){
      cout<<"Insert element "<<i<<": ";
      cin>>array[i];
      sum = array[i]+ //summ with the next array input;
cout<<sum;
      }
      return 0;
    }

means that if i enter any integer the program should be able to give the summation of the inputs in sequence from the first input to the last input

6
  • 3
    int sum = 0; and sum += array[i]; should work fine. Also you probably want to output sum only once, after the loop. Commented May 28, 2022 at 9:34
  • @willy Are you going to input 100 values?:) Commented May 28, 2022 at 9:36
  • 1
    @willy You need to initialize the variable sum: int sum = 0; And this output cout<<sum; should be placed after the compound statement of the for loop. Commented May 28, 2022 at 9:37
  • You don't need an array to sum 100 input numbers; you need an accumulator initialized to zero and one variable you (a) read, and (b) add to your accumulator, then repeat 99 more times. Commented May 28, 2022 at 9:38
  • @Vlad from Moscow yes bro but every when i enter a number the program must give me a sum of the entered number with previous entered number Commented May 28, 2022 at 9:44

4 Answers 4

2

C++ standard library already has a function to do this which is std::accumulate:

#include <iostream>
#include <numeric>

int main() {
    int array[5] = {1,2,3,4,5};
    int total = std::accumulate(std::begin(array), std::end(array), 0);
    return 0;
}

If you plan to use not a full array you should use std::begin(array), std::begin(array) + amount as ranges.

Sign up to request clarification or add additional context in comments.

1 Comment

You should use a std::array for a fixed size (which user input certainly isn't) or std::vector for dynamic size.
1

initialize your sum var to 0 initially and write sum+=array[i] instead of what you have written, there is also a limitation in this program as value of sum after all user input should be <=10^9 as int datatype store no. approximately upto 10^9 so take note of this fact also. Write cout<<sum<<endl; instead to be able to distinguish till previous input sum to the new input sum. Hope this will help.

Comments

1

You can use a do while loop:

#include<iostream>
#include<string>
#include<math.h>

int main()
{
    int array[100];
    int sum=0;
    int i=0;
    std::cout<<"Insert element"<<" "<<i<<": ";
    std::cin>>array[i];
    do
    { 
        sum=sum+array[i];
        std::cout<<sum<<std::endl;
        i++;
        std::cout<<"Insert element"<<" "<<i<<": ";
    }while(std::cin>>array[i]);
    return 0;
}

1 Comment

This will fail if I want the sum of 0 elements since the first input isn't checked.
1

If all you want is the sum then just compute the sum. No need to store anything:

#include <iostream>
#include <string>
#include <math.h>

int main() {
    int sum = 0;
    for(int i=0; i<100; i++){
        std::cout << "Insert element " << i << ": ";
        int t;
        std::cin >> t;
        sum += t
        std::cout << sum;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.