Course Materials

Week 2 Practice Questions

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>

double GetY(double x) {
    int b = 7;
    double y = (x * x) + (3 * sqrt(x)) + b;
    return y;
}

int main() {
    double X, result;

    printf("Enter value for X: ");
    scanf("%lf", &X);

    result = GetY(X);

    printf("The value of Y is: %.2lf\n", result);

    return 0;
}

                        
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double GetY(double x) {
    int b = 7;
    double y = (x * x) + (3 * sqrt(x)) + b;
    return y;
}

int main() {
    double X, result;

    cout << "Enter value for X: ";
    cin >> X;

    result = GetY(X);

    cout << "The value of Y is: " << fixed << setprecision(2) << result << endl;

    return 0;
}

                        

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>

const double PI = 3.14159;

void CircleArea(double r) {
    double area = PI * r * r;
    double perimeter = 2 * PI * r;

    printf("Area of the circle: %.2lf\n", area);
    printf("Perimeter of the circle: %.2lf\n", perimeter);
}

int main() {
    double radius;

    printf("Enter the radius (R): ");
    scanf("%lf", &radius);

    CircleArea(radius);

    return 0;
}

                        
#include <iostream>
#include <iomanip>

using namespace std;

const double PI = 3.14159;

void CircleArea(double r) {
    double area = PI * r * r;
    double perimeter = 2 * PI * r;

    cout << fixed << setprecision(2);
    cout << "Area of the circle: " << area << endl;
    cout << "Perimeter of the circle: " << perimeter << endl;
}

int main() {
    double radius;

    cout << "Enter the radius (R): ";
    cin >> radius;

    CircleArea(radius);

    return 0;
}

                        

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>

void Calc(int n1, int n2) {
    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);
}

int main() {
    int num1, num2;

    printf("Enter integer number 1: ");
    scanf("%d", &num1);

    printf("Enter integer number 2: ");
    scanf("%d", &num2);

    Calc(num1, num2);

    return 0;
}

                        
#include <iostream>

using namespace std;

void Calc(int n1, int n2) {
    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;
}

int main() {
    int num1, num2;

    cout << "Enter integer number 1: ";
    cin >> num1;

    cout << "Enter integer number 2: ";
    cin >> num2;

    Calc(num1, num2);

    return 0;
}

                        

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>

double DigitsNum(int num) {
    int d1, d2, d3;
    
    d1 = num % 10;
    d2 = (num / 10) % 10;
    d3 = num / 100;
    
    double sum = d1 + d2 + d3;
    
    return sum / 3;
}

int main() {
    int number;
    double average;

    printf("Enter a 3-digit integer: ");
    scanf("%d", &number);

    average = DigitsNum(number);

    printf("The average of the digits is: %.2lf\n", average);

    return 0;
}

                        
#include <iostream>
#include <iomanip>

using namespace std;

double DigitsNum(int num) {
    int d1, d2, d3;
    
    d1 = num % 10;
    d2 = (num / 10) % 10;
    d3 = num / 100;
    
    double sum = d1 + d2 + d3;
    
    return sum / 3;
}

int main() {
    int number;
    double average;

    cout << "Enter a 3-digit integer: ";
    cin >> number;

    average = DigitsNum(number);

    cout << "The average of the digits is: " << fixed << setprecision(2) << average << endl;

    return 0;
}

                        

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>

double CurrencyConvertor(double jod) {
    const double rate = 1.4112;
    return jod * rate;
}

int main() {
    double money_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);

    return 0;
}

                        
#include <iostream>
#include <iomanip>

using namespace std;

double CurrencyConvertor(double jod) {
    const double rate = 1.4112;
    return jod * rate;
}

int main() {
    double money_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;

    return 0;
}

                        

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>

double Distance2D(double x1, double y1, double x2, double y2);

int main() {
    double x1, 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);

    double dist = Distance2D(x1, y1, x2, y2);

    printf("The distance between the points is: %.2f\n", dist);

    return 0;
}

double Distance2D(double x1, double y1, double x2, double y2) {
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

                        
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

double Distance2D(double x1, double y1, double x2, double y2);

int main() {
    double x1, 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;

    double dist = Distance2D(x1, y1, x2, y2);

    cout << "The distance between the points is: " << fixed << setprecision(2) << dist << endl;

    return 0;
}

double Distance2D(double x1, double y1, double x2, double y2) {
    return sqrt(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()?

#include <stdio.h>

void printWelcome() {
    printf("Welcome!");
}
#include <iostream>
using namespace std;

void printWelcome() {
    cout << "Welcome!";
}
  • a. printWelcome;
  • b. void printWelcome();
  • c. printWelcome();
  • d. call printWelcome();
Solution
// 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?

float getTemperature(float celsius)
Solution
// Answer: the prototype is missing the semicolon at the end.
//
// Correct prototype:
float getTemperature(float celsius);

                        
// Answer: the prototype is missing the semicolon at the end.
//
// Correct prototype:
float getTemperature(float celsius);

                        

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.)

#include <stdio.h>

int main() {
    int score = 100;
    showScore();
    return 0;
}

void showScore() {
    printf("%d", score);
}
#include <iostream>
using namespace std;

int main() {
    int score = 100;
    showScore();
    return 0;
}

void showScore() {
    cout << score;
}
Solution
// 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>

void showScore(int score);   // prototype before main

int main() {
    int score = 100;
    showScore(score);        // pass the value as an argument
    return 0;
}

void showScore(int score) {
    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>
using namespace std;

void showScore(int score);   // prototype before main

int main() {
    int score = 100;
    showScore(score);        // pass the value as an argument
    return 0;
}

void showScore(int score) {
    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>

void show() {
    int x = 5;               // a different x, local to show()
    printf("%d\n", x);
}

int main() {
    int x = 10;              // local to main()
    show();
    printf("%d\n", x);
    return 0;
}
// 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>
using namespace std;

void show() {
    int x = 5;               // a different x, local to show()
    cout << x << endl;
}

int main() {
    int x = 10;              // local to main()
    show();
    cout << x << endl;
    return 0;
}
// Output:
// 5
// 10