-2

Given an array of N integers. My task is to print the sum of all of the integers.

Input:

First line of input file contains a single integer T which denotes the number of test cases. For each test case, there will be two lines. First line contains N which denotes number of elements in an array, and second line contains N space seperated integers.

Output:

Corresponding to each test case, print the sum of array in a new line.

Constraints:

1 <= T <= 100

1 <= N <= 1000

0 <= Arr[i] <= 200

#include <iostream>
using namespace std;

int main()
{
    int n, no_of_elem_array;
    int arr[50], sum[50];

    cin >> n;

    int j = 0;

    while (n--) {
        cin >> no_of_elem_array;

        for (int i = 0; i < no_of_elem_array; i++) {
            cin >> arr[i];
        }

        for (int i = 0; i < no_of_elem_array; i++) {
            sum[j] = sum[j] + arr[i];
        }
        j++;
    }
    for (int i = 0; i < n; i++) {
        cout << sum[i] << endl;
    }

    return 0;
}

Output

2

4

1 2 3 4

6

5 8 3 10 22 45

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60

3 Answers3

2

There is n in your final loop

   for(int i=0; i<n; i++){
   cout<<sum[i]<<endl;
    }

which is become 0 in

         while(n--)

that is why it is not print anything

0

That is not how one adds arrays in C++:

This is the code I propose:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> vec_of_ints;
    std::cout << "Enter the size of the array: ";
    unsigned size = 0;
    std::cin >> size;
    vec_of_ints.resize(size);
    for(auto& integer : vec_of_ints) {
        std::cin >> integer;
    }
    std::cout << std::accumulate(vec_of_ints.begin(), vec_of_ints.end(), 0);
    return 0;
}

Also, as @Alan Birtles has suggested, there is another (and better) alternative:

you do not need to store the input at all:

int main() {
    unsigned size = 0;
    std::cin >> size;
    long sum = 0;
    for (int i = 0; i < size; i++) {
        int num = 0;
        std::cin >> num;
        sum += num;
    }
    std::cout << sum << "\n";
    return 0;
}
kesarling He-Him
  • 1,944
  • 3
  • 14
  • 39
  • But, if we don't store the array here for sum how would we get 2 different sum for the 2 different arrays entered? – Aditya Sarin Jul 17 '20 at 07:27
  • Yes, the first line of the output statement indicates that 2 arrays are there. The line after that indicates that the size of the first array is 2 and then the elements of that array in the next line. – Aditya Sarin Jul 17 '20 at 09:27
-1

Two issues:

  1. Your arrays are not big enough, so there will be invalid access, which is undefined behaviour.

  2. Array sum is local variable, so the value is uninitialized (i.e. It contains arbitrary values when allocated), you need to set them to zero by yourself.

LIU Qingyuan
  • 524
  • 5
  • 18
  • 1
    It's better to add a good solution alongside issues in the OP's code. – Rohan Bari Jul 17 '20 at 07:17
  • @d4rk4ng31 I double checked with gcc version 9.3 and -std=c++17. Your statement is wrong and the value of local raw array's elements ARE uninit'ed. – LIU Qingyuan Jul 17 '20 at 12:37
  • @d4rk4ng31 Microsoft production expected. What I can say is standard never ask initialization with zero as it is not zero-overhead in running time. MSVC is notorious for the non-standard impl'. – LIU Qingyuan Jul 17 '20 at 13:13