J.Chu
J.Chu

Reputation: 33

C++ How to add numbers in an array within a given range?

I'm working on an assignment for class and am having a bit of a hard time putting it together. I had just started learning arrays and am not sure exactly how to get user input in the array.

Here is the assignment prompt: Create a program that inputs up to 100 integers (space separated!) and outputs their sum. For example:

1 2 3 4 5 6 7 8 9 10 55

This is what I have so far (edit because I forgot to change comments):

#include <iostream>
using namespace std;

int addNum(int n);

int main() {
   int n;

   // prompt user to input numbers
   cout << "Please enter in values to add together: ";
   cin >> n;

   cout << addNum(n);

   // pause and exit
   getchar();
   getchar();
   return 0;
}

// function
int addNum(int n) {
   int arr[99] = {};
   int sum = 0;

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

Upvotes: 0

Views: 7015

Answers (3)

J.Chu
J.Chu

Reputation: 33

Was able to get a working code after talking to the professor about it! Here's the working code:

#include <iostream>
using namespace std;

int main() {
    // variable declarations
    int sum = 0, count = 0;
    int c;

    // array declaration
    int arr[100] = { 0 };

    // prompt user to input numbers & add
    cout << "Please enter in values to add together: ";
    for (int i = 0; i < 100; i++) {
        cin >> arr[i];
        c = cin.peek();
        sum = sum + arr[i];

        // if user presses enter, skip to outputting sum without waiting for 100 values
        if (c == '\n'){
            break;
        }
    }

    // output the sum of input
    cout << sum;

    // pause and exit
    getchar();
    getchar();
    return 0;
}

Upvotes: 0

Russley Shaw
Russley Shaw

Reputation: 441

In order to provide a diverse range of answers, std::accumulate is pretty cool.

http://en.cppreference.com/w/cpp/algorithm/accumulate

int sum = std::accumulate(v.begin(), v.end(), 0);

or in your case

int sum = std::accumulate(arr, arr + 99, 0);

Another functional approach is to use std::reduce introduced in C++17

http://en.cppreference.com/w/cpp/algorithm/reduce

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

Since this is a learning exercise, I wouldn't correct your code, but explain what you've missed so far:

  • The assignment asks to read integers until there's no more input; your code prompts the user for the count upfront, which should be removed.
  • You do not need an array to store the individual numbers, because the assignment asks only for the total. This can be computed on the fly: read a number, add it to sum, and forget the number.
  • You can read numbers until the end of input with a simple loop that uses >> operator below.

Here is an example that limits the input to 100 numbers, or stops when the input stream ends:

int limit = 0;
int nextNumber;
while ((limit++ != 100) && (cin >> nextNumber)) {
    ... // Process the next number
}

If you are giving your program input from console (as opposed to feeding it a file with numbers) and you need to end your input sequence, press Ctrl+z on Windows or Ctrl+d on UNIX.

Upvotes: 4

Related Questions