Part 1 Write a function that will swap two integers values
Part 1 - Write a function that will swap two integers values using pass-by-reference (i.e. alias) parameters. Write a driver program to test your function. Your function will have two parameters...if you code them as \"pass by value,\" on return to \"main,\" they will not be switched, but instead will retain their original values...thus the importance of pass-by-reference.
Part 2 - Write a function with the following prototype...notice parameters \"one\" and \"two\" are pass-by-value: bool compute(int one, int two, int & sum, int & product, int & quotient); compute sum to be one plus two, compute product to be one times two, compute quotient to be one divided by two… provided that two is not 0. If the quotient can be computed (i.e. two is not 0) \"compute\" will return true, otherwise it will return false. Main will output the returned sum, product and quotient.
Part 3 - Overload the sum functions by using a different set of parameters for the four sum functions prototypes below... (notice we do not use reference parameters...only value parameters): int sum(int, int); int sum(int,int,int); int sum(int,int,int,int); int sum(int,int,int,int,int); Each of the four sum functions should return the sum of all of it\'s int parameters. You can write your code so that each of the last 3 sum functions calls the one listed before it. For example, inside the function with 4 parameters will be the call to the function with 3 parameters and likewise for the remaining two functions.
Part 4 - (Use box.cpp) This part does not use reference parameters, only value parameters, but does use \"overloaded\" displayBox functions...the four prototypes are as follows: void displayBox ( int length ); void displayBox ( int length, char fillChar ); void displayBox ( int width, int height ); void displayBox ( int width, int height, char FillChar ); The 3-argument function will use nested for loops for its logic. The other overloaded functions do not use any logic other than a calling statement to the 2 argument function or the 3 argument function. The 2-arguments functions will call the 3-argument function and the 1-argument function will call the 2-argument function. Use input data of 5 ? 15 4 $ Calling displayBox(5) will display xxxxx x x x x x x xxxxx Calling displayBox(5,7, a ) will display xxxxx xaaax xaaax xaaax xaaax xaaax xxxxx
Part 5 - Write a function that has two parameters - an integer that is a test score and a second parameter that is the letter grade. Use the standard grading scale for A, B, C, D, F. If the score is above 100 or below 0, a grade will not be assigned and a return value of false will be returned. If the score is within range, a return value of true will be returned and the letter grade will be returned. The prototype for the function is: bool figureGrade(int, char &); Write \"main\" to test the function you have written...main will output the letter grade which the function calculated and returned to main via the parameter letterGrade.
Solution
Part 1
#include<iostream>
using namespace std;
//Call by reference
void swap(int &x, int &y)
{
//Swapping two values
int t = x; x = y; y = t;
cout<<\"\ Within Swapping function x = \"<<x<<\" and y = \"<<y;
}
int main()
{
int x, y;
cout<<\"\ Enter 2 numbers: \";
cin>>x>>y;
cout<<\"\ Before Swapping x = \"<<x<<\" and y = \"<<y;
swap(x, y);
cout<<\"\ After Swapping x = \"<<x<<\" and y = \"<<y;
}
Output:
Enter 2 numbers: 10
20
Before Swapping x = 10 and y = 20
Within Swapping function x = 20 and y = 10
After Swapping x = 20 and y = 10
Part 2
#include<iostream>
using namespace std;
bool compute(int one, int two, int & sum, int & product, int & quotient)
{
//Checks for zero
if(two == 0)
return false;
//If second number is not zero calculates sum, product, quotient
else
{
sum = one + two;
product = one * two;
quotient = one / two;
return true;
}
}
int main()
{
int a, b, s, p, q;
cout<<\"\ Enter two numbers: \";
cin>>a>>b;
//Checks for true or false
if(compute(a,b, s, p, q))
{
cout<<\"\ Sum of two numbers: \"<<s;
cout<<\"\ Product of two numbers: \"<<p;
cout<<\"\ Quotient of two numbers: \"<<q;
}
else
{
cout<<\"\ ERROR: Second number cannot be zero\";
}
}
Output 1:
Enter two numbers: 20
10
Sum of two numbers: 30
Product of two numbers: 200
Quotient of two numbers: 2
Output 2:
Enter two numbers: 20
0
ERROR: Second number cannot be zero
Part 3
#include<iostream>
using namespace std;
int sum(int a, int b)
{
//Returns the sum of first 2 numbers
return (a + b);
}
int sum(int c,int d,int e)
{
//Calls 2 parameter sum function and adds the first parameter
c = c + sum(d, e);
return c;
}
int sum(int f,int g,int h,int i)
{
//Calls 3 parameter sum function and adds the first parameter
f = f + sum(g, h, i);
return f;
}
int sum(int j,int k,int l,int m,int n)
{
//Calls 4 parameter sum function and adds the first parameter
j = j + sum(k, l, m, n);
return j;
}
int main()
{
int a, b, c, d, e;
cout<<\"\ Enter 5 numbers: \";
cin>>a>>b>>c>>d>>e;
//Calls 5 parameter sum function
cout<<\"\ Sum = \"<<sum(a,b,c,d,e);
}
Output:
Enter 5 numbers: 1
2
3
4
5
Sum = 15
Part 4
Unable to under stand the calling procedure of function.
Make it little bit clear.
Part 5
#include<iostream>
using namespace std;
bool figureGrade(int mark, char &grade)
{
//Checks the marks validation
if(mark >= 0 && mark <= 100)
{
//Assigns grade
if(mark >= 90)
grade = \'A\';
else if(mark >= 80)
grade = \'B\';
else if(mark >= 70)
grade = \'C\';
else if(mark >= 60)
grade = \'D\';
else
grade = \'F\';
return true;
}
else
return false;
}
int main()
{
int mark;
char grade;
cout<<\"\ Enter student mark: \";
cin>>mark;
//Checks the marks validity and displays the grade
if(figureGrade(mark, grade))
cout<<\"\ Grade is: \"<<grade;
else
cout<<\"\ Invalid Mark: Must be within 0 - 100 \";
}
Output 1:
Enter student mark: 60
Grade is: D
Output 2:
Enter student mark: 120
Invalid Mark: Must be within 0 - 100



