Hi,

Okay, I really need help with this program please...

I have to create a program which;
Contains a function called sumN() which takes an int n as an argument and returns an int which is the sum of all integers between 1 and n.

In the main() it asks the user;
How many values (s)he wants to enter (maximum 50);
Asks for the values and stores them into an array int a[].
Prints to the console sumN(a) for all the elements of a[] that have been entered by the user.

I've started it, but I don't know how to do the rest because I'm sooo confused.
Here's what I have so far;


#include <iostream>

using namespace std;

int main()
{
    double temp, set[50];
    int size, t, i, value ;

    cout << "How many values do you wish to enter?\n";
    cin >> size;

    while (size>50)
    {
        cout << "Sorry, that was above 50" << endl;
        cout << "How many values do you wish to enter?\n";
        cin >> size;
    }

    cout << "Enter the values:\n";
    for (value=0; value<size; value++)
    cin >> set[value];

    cout << "Here are your values:\n";
    for (t=0; t<size; t++)
    cout << set[t] << "\n";

    return 0;
}

I'd be very grateful if someone could help me as soon as possible!
Thanks :)

Recommended Answers

All 2 Replies

Hi its a simple task to add all elements

double SumN(double *array, int size)
{
  double total = 0;
  int i;
  for(i = 0; i < size; ++i)
    total += array[i];
  return total;
}

Okay thank you,but em I always get confused when I add in new little bits so I was just wondering if you could tell me where I'm going wrong..

#include <iostream>

using namespace std;

int main()
{
    double array, set[50];
    int size, t, value ;

    cout << "How many values do you wish to enter?\n";
    cin >> size;

    while (size>50)
    {
        cout << "Sorry, that was above 50" << endl;
        cout << "How many values do you wish to enter?\n";
        cin >> size;
    }

    cout << "Enter the values:\n";
    for (value=0; value<size; value++)
    cin >> set[value];

    double SumN(double *array, int size);{

        double total = 0;
        int i;
        for(i = 0; i < size; ++i)
            total += *array[i];
        return total;}


    cout << "Here are your values:\n";
    for (t=0; t<size; t++)
    cout << set[t] << "\n";

    return 0;
}

I added it in like this, but when I run it, it keeps saying about line 29..
error: invalid types 'double[int]' for array subscript

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.