Nothing Special   »   [go: up one dir, main page]

PF Lab 4 Summer

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Programming Fundamentals

Lab-4
Topic Basics of functions
Learning objectives of this lab is to be able to learn the purpose and usage of
Objective functions.

Question 1: (Demo)

(a)Write a C++ program to calculate power of a number. Use built-in function for calculation.

(b) Write a C++ program that calls a function print_table(). The function is supposed to print the table
of a user entered number till 6 on console.
Sample Output:

Enter a number:3
3*1=3
3*2=6
3*3=9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18

(c)Now modify this above function and pass parameter (integer) to it for which you want to print table
of.
Hint: Output will be same but function prototype will be changed i.e. print_table(int number)

Question 2: Write a C++ program which works as a basic calculator. Make a function calculator() that
accepts two integer values and a character value. Your function should perform the operation on the
entered integer values depending on the character value entered by the user and return the output to
main().
Make sure to create separate functions of sum(int,int), subtraction(int,int), multiplication(int,int)
and division(int,int). The function calculator(int,int,char) should be able to call the necessary function
as required.
Sample Output:

Enter Number 1: 100


Enter Number 2: 2
Enter Character: +
The result after addition is: 102
Task-3: Write a C++ program to evaluate the following expression using the same implemented
functions as above. Also make sure to use find_mul(int,int) where ever you feel the need to calculate
square instead of creating a separate function for square.
𝑎2 + 𝑏2
To evaluate the above expression, you can only use following functions and no other function should
be created:

int calcMul(int,int);
int calcSum(int,int);

NOTE: Copy paste the function of multiplication and sum which has been previously
implemented.

Sample Output:

Enter a: 10
Enter b: 2
Result is: 104

Task-4: Write a C++ program to evaluate the following expression using the find_mul(int,int)
function. And print the result through main.

3𝑎𝑏

NOTE: Copy paste the function of multiplication which has been previously implemented.

Sample Output:

Enter a: 10
Enter b: 2
Result is: 60

Task-5: Write a C++ program to evaluate the following expression using the same implemented
functions as above.
32 + 𝑎2 + 𝑏 + 3𝑎𝑏
To evaluate the above expression, you can only use following functions and no other function should
be created:

int calcMul(int,int);
int calcSum(int,int);

NOTE: Copy paste the function of multiplication and sum which has been previously
implemented.
Sample Output:

Enter a: 10
Sample
Enter b:Output:
2
Result is: 171
Task 7:

b) Use the above function of sum which you have created in task-03. Now, using the same
function you have to calculate sum of two arrays separately and display the results of each
on screen from main.
Sample Output:
Array1:2 3 4 5 6 1 2 1 2
Array2:2 3 1 2 1 3 1
Sum of array1 is: 26
Sum of array 2 is: 13

c) Make a function findmax that accepts the sum of arrays (calculated in previous step) and finds
the maximum value.
Sample Output:
Maximum value is: 26

d) Make a function ‘void update(int a*+,int,int) that accepts an array, size and maximum
value(i.e.26) and update array values by subtracting from maximum value.

Sample Output:

Array1:24 23 22 21 20 25 24 25 24
Array2:24 23 25 24 25 23 25
Task 6:
Make a program that takes full name from user. Make a function check() that accepts the name.
Function should display the index of vowels and return the total number of vowel present in array.
To calculate the total number of vowels you can use the function of sum previously implemented.

Sample Output:
Enter Full name: ali ahmad
Vowel is at index: 0 2 4 7
Total vowel: 4

You might also like