0% found this document useful (0 votes)
33 views

Assignment 1

The document provides instructions for an assignment with 3 questions. Question 1 asks the student to write a C++ program with 5 functions - DisplayOptions(), ReadUserChoice(), SquareRoot(), POWER(), and Lower(). Question 2 tests understanding of function prototypes, return types, and parameters. It asks for the output of a sample program for different user inputs. Question 3 tracks the values of variables as they are passed into and out of functions without running the code. It asks for the output of 4 cout statements. The deadline for the assignment is January 28th.

Uploaded by

Ali Saleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Assignment 1

The document provides instructions for an assignment with 3 questions. Question 1 asks the student to write a C++ program with 5 functions - DisplayOptions(), ReadUserChoice(), SquareRoot(), POWER(), and Lower(). Question 2 tests understanding of function prototypes, return types, and parameters. It asks for the output of a sample program for different user inputs. Question 3 tracks the values of variables as they are passed into and out of functions without running the code. It asks for the output of 4 cout statements. The deadline for the assignment is January 28th.

Uploaded by

Ali Saleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 1

Deadline Saturday 28 January

Q1: Write a program that has the following functions (4 marks):

1. Void function named DisplayOptions, that displays the following calculation options to
users:

(1) to find a square root of a number using SquareRoot function


(2) to get a power of a number using POWER function
(3) to check if a letter is a lower letter or not using Lower function

2. A returning-value function named ReadUserChoice that will read and return the user
choice to the main function. The user should enter 1,2 or 3 otherwise, the function will
ask the user to re-enter his choice again.

Based on the entered value by the user, the program will call one of the three user-defined
functions: SquareRoot, POWER, Lower.

3. SquareRoot function is a is a returning-value function that read one number from user
and return its square root (Hint*: use a pre-defined function).
4. POWER function is a returning-value function that read two numbers from user and
returned the result where the first number is the base, and second number is the exponent
(Hint*: you can use a pre-defined function).
5. Lower function is a void function that take a character from a user and check if the
passed character is lower or not, if it is not lower, it will return the lower letter (Hint*:
use reference parameter).

The program then prints the results to users within the main function. Write all the required
libraires and the right function and variables data type.
Q2: What is the output of the following program? (2 marks)
1. #include <iostream>
2. using namespace std;
3. void welcomsg();
4. int sum(int a,int b);
5. int main()
6. {
7. int Choice;
8. int x,y;
9. cout << "Please select from the following option: "<<endl
10. <<"1 to display welcoming message"<<endl
11. << "2 to calculate the summation of two numbers"<<endl;
12.
13. cin >> Choice;
14. if (Choice == 1)
15. welcomsg();
16. else if (Choice == 2)
17. cout<<sum(x,y)<<endl;
18. else
19. cout << "Invalid input" << endl;
20. return 0;
21. }
22. void welcomsg()
23. {
24. cout << "Welcome to Programming 2 course" <<endl;
25. }
26. int sum(int a,int b)
27. {
28. cout<<"please enter two numbers"<<endl;
29. cin>>a;
30. cin>>b;
31. return a+b;
32. }

1. What is the output if the user entered 1?


2. What is the output if the user entered 0?
3. What is the type of the sum function? How many arguments does the function sum have?
4. What is the type of the welcomsg function? What does the function return?
5. What is the line number that shows the actual parameters of sum function?
6. What is the line number that shows the formal parameters of the sum function?
7. What is the line number that shows the welcomsg function prototype?
8. What is the line number that shows the welcomsg function heading?

Q3: Track the following code, what are the outputs in lines 10, 12, 21 and 28? (Do not run the
code) (2 marks)

1. #include <iostream>
2. using namespace std;

3. void f1(int& a, int b);


4. void f2(int x, int& y);
5. int main()
6. {
7. int Num1 = 2;
8. int Num2 = 5;
9. f1(Num1, Num2);
10. cout << "The value of Num1 is: " << Num1<<"The value of Num2 is: "<< Num2 <<
endl;
11. f2(Num1, Num2);
12. cout << "The value of Num1 is: " << Num1<<"The value of Num2 is: " << Num2 <<
endl;
13. return 0;
14. }
15. void f1(int& a, int b)
16. {
17. int Num1;
18. Num1 = b + 12;
19. a = 2 * b + 5;
20. b = Num1 + 4;
21. cout << "The value of a is: " << a<<"The value of b is: " << b<< endl;
22. }
23. void f2(int x, int& y)
24. {
25. int Num2=8;
26. y = Num2 * 2;
27. x= y-x;
28. cout << "The value of x is: " << x<<"The value of y is: " << y << endl;
29. }

Good luck J

You might also like