Write program using C++ code Exercise 1: Function basics Your program will two n
ID: 3729157 • Letter: W
Question
Write program using C++ code Exercise 1: Function basics Your program will two numbers from the command line and display the sum of all values from one to the other (indlusive) ·/exercise! 5 8 Summation from 5 to 8: 26 Order of the values doesnt matter ·/exercise! 8 5 Summation from 5 to 8:26 Note the program displays Summation from to larger value" Define the following functions in main.cpp int smaller(int n1, int n2) returns the smaller of the two values (or either if tied) int larger(int n1, int n2) Returns the larger of the two values (or either if tied) int sum(int n1, int n2) . Retuns the sum of values from the smaller value to the larger value Make calls to your previous defined functions to helpl int main(int argc, char argv Handles all printingl . Your other function do not print anythingExplanation / Answer
below is the solution code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int smaller(int n1,int n2); //declare the smaller function
int larger(int n1,int n2); //declare the larger function
int sum(int n1,int n2); //declare the sum function
int main(int argc, char* argv[])
{
if(argc != 3) //check if the argument passed is not equal to 3 then prints the message and exit otherwise program will continue to execute
{
cout<< " Invalid number of operands" << endl;
return 1;
}
int n1 = atoi (argv[1]); //store the first argument in n1 variable
int n2 = atoi (argv[2]); //store the second argument in n2 variable
smaller(n1,n2); //call the smaller function
larger(n1,n2); //call the larger function
return 0;
}
//small function to sum of given smaller to larger number
int smaller(int n1,int n2){
int s=sum(n1,n2); //store the sum of two value isn sum variable after call the sum function
return s;
}
//larger function to check the larger value
int larger(int n1,int n2){
int num; //declare the num value to assign the larger value into it
if(n1==n2){ //if both the given number is equal
cout<< " Tied" << endl; //prints tied
return 1;
}
else if(n1>n2) //check the n1>n2 then prints the larger value is n1 otherwise n2 is larger
cout<<" Larger value is: "<<n1;
else
cout<<" Larger value is: "<<n2;
return 1;
}
//sum of the value from smaller to larger
int sum(int n1,int n2){
int s1=0; //declare the s1 value to sum of the smaller to larger
if(n1<n2){ //check n1 is smaller
for(int i=n1;i<=n2;i++) //loop from smaller to larger
s1+=i; //sum the number
cout<<" Summation from "<<n1<<" to "<< n2 <<": " <<s1; //prints the sum value
}
else{
for(int i=n2;i<=n1;i++) //loop from larger to smaller
s1+=i; //sum the number
cout<<" Summation from "<<n2<<" to "<< n1 <<": " <<s1; //prints the sum value
}
return 1;
}
smaple output:
./excercise1 5 8
Summation from 5 to 8: 26
Larger value is 8
./excercise1 8 5
Summation from 8 to 5: 26
Larger value is 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.