Question #37765

Write a function that, using default arguments, allows user to take the sum of anywhere between 2 and 4 integers. This is what i got so far....#include <cstdlib>
#include <iostream>

using namespace std;


void sum(float a,float b);
void sum(int a,int b, int c = 0);
double sum(double a, double b, double c, double d);

int main(int argc, char *argv[])
{
int no[4];

cout<< "Enter any number from 2 to 4: "<<endl;

//Loop until EOF
for(int i=0; no[i]-1 != EOF; i++){
cout<< ">> ";
cin>> no[i]; //Ask value array no[]
i++;
}

sum(no[0],no[1],no[2],no[3]); //call function

system("PAUSE");
return EXIT_SUCCESS;
}
//FUNCTION
int sum(int a, int b){
return a + b;
}

float sum(float a, float b, float c){
return a + b + c;
}
double sum(double a, double b, double c, double d){
return a + b + c + d;
}

Expert's answer

Answer on Question#37765- Programming, C++

1. Write a function that, using default arguments, allows user to take the sum of anywhere between 2 and 4 integers.?

Solution.

#include <iostream>
using namespace std;
int sum(int a, int b, int c = 0, int d = 0)
{
    return a + b + c + d;
}
int main(int argc, char *argv[])
{
    int no[4] = {0};
    cout << "Enter 2 to 4 numbers: " << endl;
    for (int i = 0; i < 4; i++)
    {
        cout << "Enter number " << i + 1 << ": ";
        cin >> no[i]; //Ask value array no[]
    }
    cout << "Answer: " << sum(no[0], no[1], no[2], no[3]) << endl;
    system("pause");
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS