0% found this document useful (0 votes)
28 views2 pages

C++ LAB-5 - Function

Uploaded by

pahambyjulie
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)
28 views2 pages

C++ LAB-5 - Function

Uploaded by

pahambyjulie
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/ 2

PROGRAMMING FUNDAMENTALS (USING C++)

Lab 5 -Function
Due Date: Beginning of Next Class
Instructor: Dr. Najia Saher email: [email protected]

Perform these questions on computer in Lab and also submit this as hand written assignment.

Task -1: Do the first exercise by using built-in library function

Question 1: Write a program to compute area of triangle. Sides are input by user.
Area = sqrt(s*(s-a)*(s-b)*(s-c))
where s=(a+b+c)/2.
Question 2: Write a program to check character entered is alphabet, digit or special character.
Question 3: Write a program which displays a number between 10 to 100 randomly.
Question 4: Write a program which accept a letter and display it in uppercase letter.

Task - 2: User defined Function

Question 5: Write a program using function which accept two integers as an argument and return its
sum. Call this function from main( ) and print the results in main( ).
Question 6: Write a function to calculate the factorial value of any integer as an argument. Call this
function from main( ) and print the results in main( ).
Question 7: Raising a number to a power p is the same as multiplying n by itself p times. Write a
function called power that takes two arguments, a double value for n and an int value for
p, and return the result as double value. Use default argument of 2 for p, so that if this
argument is omitted the number will be squared. Write the main function that gets value
from the user to test power function.

Question 8: Write a function called zero_small() that has two integer arguments being passed by
reference and sets the smaller of the two numbers to 0. Write the main program to access
the function.

Question 9: Write a function multiple that determines for a pair of integers whether the second integer is
a multiple of the first. The function should take two integer arguments and return true if the
second is a multiple of the first, false otherwise. Use this function in a program that inputs a
series of pairs of integers.
Lab + Assignment: Programming Fundamentals (C++)
1
Question 10: Write a program that lets the user perform arithmetic operations on two numbers. Your
program must be menu driven, allowing the use to select the operation (+, -, *, or /) and input
the numbers. Furthermore, your program must consist of following functions:
1. Function show Choice: This function shows the options to the user and explains how to enter data.
2. Function add: This function accepts two number as arguments and returns sum.
3. Function subtract: This function accepts two number as arguments and returns their difference.
4. Function multiply: This function accepts two number as arguments and returns product.
5. Function divide: This function accepts two number as arguments and returns quotient.

Question 11: Write a function that takes the time as three integer arguments (for hours, minutes and
seconds), and returns the number of seconds since the last time the clock “struck 12.” Use
this function to calculate the amount of time in seconds between two times, both of which
are within one 12-hour cycle of the clock.

Question 12: Write a recursive function power( base, exponent)that, when invoked, returns base exponent.
For example, power(3,4)=3*3*3*3. Assume that exponent is an integer greater than or equal
to 1. Hint: The recursion step would use the relationship base exponent = base · base
exponent- 1 and the terminating occurs when exponent is equal to 1 because base1 = base

Question 13: Write a complete C++ program with the two alternate functions specified below, of which
each simply triples the variable count defined in main. Then compare and contrast the two
approaches. These two functions are
a) Function tripleCallByValue that passes a copy of count call-by-value, triples the copy and
returns the new value.
b) Function tripleByReference that passes count with true call-by-reference via a reference

Lab + Assignment: Programming Fundamentals (C++)


2

You might also like