using C++ Make sure to use const arguments where appropriate throughout this pro
ID: 3573965 • Letter: U
Question
using C++
Make sure to use const arguments where appropriate throughout this problem (and all the others). Write a single sum function that returns the sum of two integers. Also write the equivalent function for taking the sum of two doubles. Explain why, given your functions from part 1, sum(1, 10.0) is a syntax error. Write 2 more functions such that you can find the sum of anywhere between 2 and 4 integers by writing sum (num1, num2, .. .). Now write just one function that, using default arguments, allows you to take the sum of anywhere between 2 and 4 integers. What would happen if you put both this definition and your 3-argument function from part 3 into the same file, and called sum (3, 5, 7)? Why? Write a single sum function capable of handling an arbitrary' number of integers. It should take two arguments, include a loop, and return an integer. Now rewrite your function from e. to use recursion instead of a loop. The function signature should not change. Thinking about pointer arithmetic may help you.Explanation / Answer
question a)
#include <iostream>
public class A{
public int sum(int a, int b){
return a+b;
}
public double sum(double x,double y){
return x+y;
}
}
int main(void){
A a;
cout<<"Enter integer values ";
cin>>a>>b;
int c=a.sum(a,b);
cout<<c;
cout<<"Enter double values ";
cin>> x>>y;
double z=a.sum(x,y);
cout<<z;
}
question b)
Answer: In part 1 function int(int a,int b), takes 2 integer values. When we pass double values instead of integer it shows an array because of type casting.
double value can't be downcast to integer but integer can.
Question c)
Answer:
#include <iostream>
public class A{
public int sum(int a, int b){
cout<< a+b;
}
public int sum(int a,int b,int c){
cout<< a+b+c;
}
public int sum(int a, int b,int c,int d){
cout<< a+b+c+d;
}
}
int main(void){
clrscr();
A a;
a.(2,2);
a.(2,2,2);
a.(3,3,3,3);
}
question D)
answer:
#include <iostream>
public class A{
public int sum(int a, int b,int c=0,int d=0){
cout<< a+b+c+d;
}
}
int void(main){
A a;
cout<<"enter 2 ineger values";
cin>>a>>b;
a.sum(a,b);
cout<<"enter 3 ineger values";
cin>>a>>b>>c;
a.sum(a,b,c);
cout<<"enter 4 ineger values";
cin>>a>>b>>c>>d;
a.sum(a,b,c,d);
}
question E)
Answer:
#include <iostream>
public class A{
int c=0;
public int sum( const int numbers[ ], int arraySize){
for(int i=0;i<=arraySize;i++){
int c= c+number[i];
}
cout<<c;
}
}
int main(void){
A a;
cout<<"enter size of array";
cin>>arraySize;
int number[arraySize];
coot<<"enter values";
for(int i=0;i<=arraySize;i++){
cin>>number[i];
}
a.sum(number,arraySize);
getch();
}
Question f)
Amswer:
#include <iostream>
public class A{
int value=0;
int sum(int number[], int arraySize){
if (arraySize == 0) {
// base case
return 0;
} else {
int value = number[arraySize-1];
return value + sum(number, arraySize - 1);
}
}
cout<<value;
}
}
int main(void){
A a;
cout<<"enter size of array";
cin>>arraySize;
int number[arraySize];
coot<<"enter values";
for(int i=0;i<=arraySize;i++){
cin>>number[i];
}
a.sum(number,arraySize);
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.