Note: Try to solve these questions on your own before checking the solutions.
Filter:
Sort:
Exercise 1: Calculate Y
Write a function named GetY(), which receives the value of X and computes and returns the value of Y, as follows:
\[Y = X^2 + 3\sqrt{X} + b\]
Where b is a constant value defined in the program (let b = 7).
In main(), you need to input the value for X and pass it to the function, then receive the value and print it out to the user.
Example:
Enter value for X: 4
The value of Y is: 29.00
(Calculation: \(16 + 3(2) + 7 = 29\))
Solution
#include <stdio.h>
#include <math.h>
doubleGetY(doublex){intb=7;doubley=(x*x)+(3*sqrt(x))+b;returny;}intmain(){doubleX,result;printf("Enter value for X: ");scanf("%lf",&X);result=GetY(X);printf("The value of Y is: %.2lf\n",result);return0;}
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespacestd;doubleGetY(doublex){intb=7;doubley=(x*x)+(3*sqrt(x))+b;returny;}intmain(){doubleX,result;cout<<"Enter value for X: ";cin>>X;result=GetY(X);cout<<"The value of Y is: "<<fixed<<setprecision(2)<<result<<endl;return0;}
Exercise 2: Circle Properties
Write a function named CircleArea(), which receives the R (Radius) of the circle and computes both the Area and Perimeter of the Circle.
You need to display the values of Area and Perimeter within the same function.
In main(), you need to input the value for R and pass it to the function, then print out the results within the same function.
Example:
Enter the radius (R): 5
Area of the circle: 78.54
Perimeter of the circle: 31.42
Solution
#include <stdio.h>
constdoublePI=3.14159;voidCircleArea(doubler){doublearea=PI*r*r;doubleperimeter=2*PI*r;printf("Area of the circle: %.2lf\n",area);printf("Perimeter of the circle: %.2lf\n",perimeter);}intmain(){doubleradius;printf("Enter the radius (R): ");scanf("%lf",&radius);CircleArea(radius);return0;}
#include <iostream>
#include <iomanip>
usingnamespacestd;constdoublePI=3.14159;voidCircleArea(doubler){doublearea=PI*r*r;doubleperimeter=2*PI*r;cout<<fixed<<setprecision(2);cout<<"Area of the circle: "<<area<<endl;cout<<"Perimeter of the circle: "<<perimeter<<endl;}intmain(){doubleradius;cout<<"Enter the radius (R): ";cin>>radius;CircleArea(radius);return0;}
Exercise 3: Calculator Function
Implement a function named Calc(), that receives 2 integer arguments n1, n2 and performs the basic mathematical operations (you need to display the results within the same function):
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)
In main(), you need to input the value for n1, n2 and pass them to the function, then display the results within the same function.
Note: The solution does not handle division by zero as conditional statements have not been covered yet.
Example:
Enter integer number 1: 10
Enter integer number 2: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulation: 0
Solution
#include <stdio.h>
voidCalc(intn1,intn2){printf("Addition: %d\n",n1+n2);printf("Subtraction: %d\n",n1-n2);printf("Multiplication: %d\n",n1*n2);printf("Division: %d\n",n1/n2);printf("Modulation: %d\n",n1%n2);}intmain(){intnum1,num2;printf("Enter integer number 1: ");scanf("%d",&num1);printf("Enter integer number 2: ");scanf("%d",&num2);Calc(num1,num2);return0;}
#include <iostream>
usingnamespacestd;voidCalc(intn1,intn2){cout<<"Addition: "<<n1+n2<<endl;cout<<"Subtraction: "<<n1-n2<<endl;cout<<"Multiplication: "<<n1*n2<<endl;cout<<"Division: "<<n1/n2<<endl;cout<<"Modulation: "<<n1%n2<<endl;}intmain(){intnum1,num2;cout<<"Enter integer number 1: ";cin>>num1;cout<<"Enter integer number 2: ";cin>>num2;Calc(num1,num2);return0;}
Exercise 4: Average of Digits
Implement a function named DigitsNum(), that receives a 3-digit integer argument num and returns the average of its digits.
In main(), you need to input the value for num and pass it to the function, then receive the value and print it out to the user.
Example:
Enter a 3-digit integer: 123
The average of the digits is: 2.00
(Calculation: $$ (1+2+3)/3 = 2 $$)
Solution
#include <stdio.h>
doubleDigitsNum(intnum){intd1,d2,d3;d1=num%10;d2=(num/10)%10;d3=num/100;doublesum=d1+d2+d3;returnsum/3;}intmain(){intnumber;doubleaverage;printf("Enter a 3-digit integer: ");scanf("%d",&number);average=DigitsNum(number);printf("The average of the digits is: %.2lf\n",average);return0;}
#include <iostream>
#include <iomanip>
usingnamespacestd;doubleDigitsNum(intnum){intd1,d2,d3;d1=num%10;d2=(num/10)%10;d3=num/100;doublesum=d1+d2+d3;returnsum/3;}intmain(){intnumber;doubleaverage;cout<<"Enter a 3-digit integer: ";cin>>number;average=DigitsNum(number);cout<<"The average of the digits is: "<<fixed<<setprecision(2)<<average<<endl;return0;}
Exercise 5: Currency Converter
Implement a function named CurrencyConvertor(), which receives an amount of money (double value) in JoD (Jordanian Dinar) then converts it to USD, suppose that:
\[1 \text{ JoD} = 1.4112 \text{ USD}\]
Example:
Enter amount in JOD: 100
Amount in USD: 141.1200
Solution
#include <stdio.h>
doubleCurrencyConvertor(doublejod){constdoublerate=1.4112;returnjod*rate;}intmain(){doublemoney_jod,money_usd;printf("Enter amount in JOD: ");scanf("%lf",&money_jod);money_usd=CurrencyConvertor(money_jod);printf("Amount in USD: %.4lf\n",money_usd);return0;}
#include <iostream>
#include <iomanip>
usingnamespacestd;doubleCurrencyConvertor(doublejod){constdoublerate=1.4112;returnjod*rate;}intmain(){doublemoney_jod,money_usd;cout<<"Enter amount in JOD: ";cin>>money_jod;money_usd=CurrencyConvertor(money_jod);cout<<"Amount in USD: "<<fixed<<setprecision(4)<<money_usd<<endl;return0;}
Exercise 6: Distance in 2D Space
Write a function named Distance2D() that receives the 2D coordinates \((x, y)\) of two points and computes the Euclidean distance between them.
In main(), you need to input the coordinates for both points, pass them to the function, and print the calculated distance to the user.
Example:
Enter coordinates for Point 1 (x1 y1): 1.5 2.0
Enter coordinates for Point 2 (x2 y2): 4.5 6.0
The distance between the points is: 5.00
Solution
#include <stdio.h>
#include <math.h>
doubleDistance2D(doublex1,doubley1,doublex2,doubley2);intmain(){doublex1,y1,x2,y2;printf("Enter coordinates for Point 1 (x1 y1): ");scanf("%lf %lf",&x1,&y1);printf("Enter coordinates for Point 2 (x2 y2): ");scanf("%lf %lf",&x2,&y2);doubledist=Distance2D(x1,y1,x2,y2);printf("The distance between the points is: %.2f\n",dist);return0;}doubleDistance2D(doublex1,doubley1,doublex2,doubley2){returnsqrt(pow(x2-x1,2)+pow(y2-y1,2));}
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespacestd;doubleDistance2D(doublex1,doubley1,doublex2,doubley2);intmain(){doublex1,y1,x2,y2;cout<<"Enter coordinates for Point 1 (x1 y1): ";cin>>x1>>y1;cout<<"Enter coordinates for Point 2 (x2 y2): ";cin>>x2>>y2;doubledist=Distance2D(x1,y1,x2,y2);cout<<"The distance between the points is: "<<fixed<<setprecision(2)<<dist<<endl;return0;}doubleDistance2D(doublex1,doubley1,doublex2,doubley2){returnsqrt(pow(x2-x1,2)+pow(y2-y1,2));}
Exercise 7: Defining a Function
Which of the following correctly defines a function that takes two integers and returns their sum?
a.void sum(int a, int b) { return a + b; }
b.int sum(int a, int b) { return a + b; }
c.int sum(a, b) { return a + b; }
d.int sum(int a, b) { return a + b; }
Solution
// b. int sum(int a, int b) { return a + b; }//// A function that returns a value needs a non-void return type (option a uses void), and every// parameter needs its own type (options c and d leave a type out).
// b. int sum(int a, int b) { return a + b; }//// A function that returns a value needs a non-void return type (option a uses void), and every// parameter needs its own type (options c and d leave a type out).
Exercise 8: Calling a Function
Given the following function, how do you properly execute it inside main()?
// c. printWelcome();//// A function is called by writing its name followed by parentheses.// - printWelcome; only names the function, it does not call it.// - void printWelcome(); is a declaration, not a call.// - call printWelcome(); "call" is not a C/C++ keyword.
// c. printWelcome();//// A function is called by writing its name followed by parentheses.// - printWelcome; only names the function, it does not call it.// - void printWelcome(); is a declaration, not a call.// - call printWelcome(); "call" is not a C/C++ keyword.
Exercise 9: Spot the Prototype Error
What is the syntax error in this function prototype?
floatgetTemperature(floatcelsius)
Solution
// Answer: the prototype is missing the semicolon at the end.//// Correct prototype:floatgetTemperature(floatcelsius);
// Answer: the prototype is missing the semicolon at the end.//// Correct prototype:floatgetTemperature(floatcelsius);
Exercise 10: Parameters or Arguments?
In the function int calculate(int a, int b), what do a and b represent?
a. Global variables
b. Arguments
c. Parameters
d. Return values
Solution
// c. Parameters//// a and b are parameters: the variables listed in the function's definition.// The values you pass when you call the function, for example calculate(3, 4),// are called arguments.
// c. Parameters//// a and b are parameters: the variables listed in the function's definition.// The values you pass when you call the function, for example calculate(3, 4),// are called arguments.
Exercise 11: Why Does It Not Compile?
Look at this code. Why will it fail to compile? (Give all the reasons.)
// The code fails to compile for two reasons:// 1. score is declared inside main(), so it only exists there. showScore() cannot see it.// 2. showScore() is called in main() before the compiler has seen it (no prototype before main).// Fixed version:#include <stdio.h>
voidshowScore(intscore);// prototype before mainintmain(){intscore=100;showScore(score);// pass the value as an argumentreturn0;}voidshowScore(intscore){printf("%d",score);}
// The code fails to compile for two reasons:// 1. score is declared inside main(), so it only exists there. showScore() cannot see it.// 2. showScore() is called in main() before the compiler has seen it (no prototype before main).// Fixed version:#include <iostream>
usingnamespacestd;voidshowScore(intscore);// prototype before mainintmain(){intscore=100;showScore(score);// pass the value as an argumentreturn0;}voidshowScore(intscore){cout<<score;}
Exercise 12: Same Name, Different Function
Can you declare variables inside a function that have the same name as variables in main()?
a. No, every variable name in the program must be unique.
b. Yes, each function has its own scope.
c. Yes, but only if the variables have the same type.
d. Yes, but only if the variable in main() is declared const.
Solution
// b. Yes. Each function has its own scope, so a variable declared inside a function is// independent of a variable with the same name in main() (or in any other function).#include <stdio.h>
voidshow(){intx=5;// a different x, local to show()printf("%d\n",x);}intmain(){intx=10;// local to main()show();printf("%d\n",x);return0;}// Output:// 5// 10
// b. Yes. Each function has its own scope, so a variable declared inside a function is// independent of a variable with the same name in main() (or in any other function).#include <iostream>
usingnamespacestd;voidshow(){intx=5;// a different x, local to show()cout<<x<<endl;}intmain(){intx=10;// local to main()show();cout<<x<<endl;return0;}// Output:// 5// 10