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

Functions: Practical No.68

The document contains descriptions of 96 programming tasks involving functions and arrays in C++. The tasks cover a range of concepts including defining and calling functions that perform calculations like calculating squares, testing for even/odd numbers, finding minimum/maximum values. Other tasks involve functions that reverse digits, find factors, calculate series. Several tasks involve passing arguments to functions by value and reference. Other tasks use arrays to store and process data, find sum/average, search for elements, and demonstrate concepts like linear/binary search.

Uploaded by

Waqar Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Functions: Practical No.68

The document contains descriptions of 96 programming tasks involving functions and arrays in C++. The tasks cover a range of concepts including defining and calling functions that perform calculations like calculating squares, testing for even/odd numbers, finding minimum/maximum values. Other tasks involve functions that reverse digits, find factors, calculate series. Several tasks involve passing arguments to functions by value and reference. Other tasks use arrays to store and process data, find sum/average, search for elements, and demonstrate concepts like linear/binary search.

Uploaded by

Waqar Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Functions

Practical No.68

Task: Write a program that accept a number from the user as an input and calculates the
square of a given number using function.

Practical No. 69

Task: Write a program that by defining a function “even_odd”, to test whether a given
integer is even or odd. Pass an integer value as an argument to a function.

Practical No. 70

Task: Write a program that accepts three numbers from the user as an input and display
minimum of three numbers using function.

Practical No. 71

Task: Write a program that calculates the area of the Ring using a single function.

Practical No. 72

Task: Write a function integerPower ( base, exponent ) that returns the value of

base exponent

For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero


integer and that base is an integer. The function integerPower should use for or while to
control the calculation. Do not use any math library functions.

Practical No. 73

Task: Write an integer number is said to be a perfect number if the sum of its factors,
including 1 (but not the number itself), is equal to the number.

For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a function perfect that


determines whether parameter number is a perfect number. Use this function in a program
that determines and prints all the perfect numbers between 1 and 1000. Print the factors of
each perfect number to confirm that the number is indeed perfect.

Practical No. 74

Task: A positive integer is entered through the keyboard. Write a function to obtain the
prime factors of this number.

For example: Prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.
Practical No. 75

Task: Write a function that takes an integer value and returns the number with its digits
reversed.

For example, given the number 7631, the function should return 1367.

Practical No. 76

Task: The greatest common divisor (GCD) of two integers is the largest integer that evenly
divides each of the numbers. Write a function gcd that returns the greatest common divisor
of two integers.

Practical No. 77

Task: Write a function to evaluate the series

to five significant digits.

Practical No. 78

Task: Write a program using function to calculate the factorial value of any integer entered
by the user as an input.

(1) Without using recursion.

(2) Using recursion.

Practical No. 79

Task: A 5-digit positive integer is entered through the keyboard, write a function to
calculate sum of digits of the 5-digit number:

(1) Without using recursion.

(2) Using recursion.

Practical No. 80

Task: Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a
Fibonacci sequence the sum of two successive terms gives the third term. Following are the
first few terms of the Fibonacci sequence:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...

Practical No. 81

Task: Write a C++ program that uses an inline function circleArea to prompt the user for
the radius of a circle and to calculate and print the area of that circle.
Practical No. 82

Task: Write a C++ program that uses an inline function pound_kg to prompt the user for
the weight in pound and to calculate and print the equivalent in kilogram.

Practical No. 83

Task: Write a program by defining a function swap to exchange the values by passing
argument by reference to the function.

Practical No. 84

Task: Computers are playing an increasing role in education. Write a program that will help
an elementary school student learn multiplication. Use rand to produce two positive one-
digit integers. It should then type a question such as:

How much is 6 times 7?

The student then types the answer. Your program checks the student's answer. If it is
correct, print "Very good!", and then ask another multiplication question. If the answer is
wrong, print "No. Please try again." and then let the student try the same question again
repeatedly until the student finally gets it right.

Practical No. 85

Task: Write a program that plays the game of “guess the number” as follows: Your program
chooses the number to be guessed by selecting an integer at random in the range 1 to 1000.
The program then types:

I have a number between 1 and 1000.

Can you guess my number?

Please type your first guess.

The player then types a first guess. The program responds with one of the following:

1. Excellent! You guessed the number!

Would you like to play again (y or n)?

2. Too low. Try again.

3. Too high. Try again.

If the player's guess is incorrect, your program should loop until the player finally gets the
number right. Your program should keep telling the player Too high or Too low to help
the player “zero in” on the correct answer.
Practical No. 86

Task: 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 parameter and triples the original copy of count through its alias (i.e., the
reference parameter).

Practical No. 87

Task: Write a C++ program that demonstrates the concept of function overloading by
calculating the cube of any number. The function cube calculate cube of different data type
having same or different parameter.

Practical No. 88

Task: Write a C++ program that demonstrates the concept of default argument in
calculating the volume of the box. This function calculate volume of the box 4 time having
default values at first and changing them until having no default vales in last call.

Arrays
Array: An Array is a set of contigious memory locations i.e. all the elements of an array are stored
contigiously / continuously in memory. All the elements of array have same name as that of array
but have different indexes or subscripts. So every element of array is referenced by
1. Array Name
2. Corresponding index or Subscript
That’s why array is also known as "subscripted variable". Moreover the length of array is always
fixed (defined at the time of declaration) and all the elements of an array have same data type e.g.
int, float, long etc...
Note that the index of an array starts from 0 (instead of 1) and ends at N-1 where N is the size of
array. i.e. an array of 5 variables will have its indexes from 0 to 4 as a[0],a[1],a[2],a[3] and a[4].

Practical No.88

Task: Write a program that uses an array of five elements. It just accepts the elements of
array from the user and displays them in the same order using for loop.
Practical No.89

Task: Write a program that uses an array of five elements. It just accepts the elements of
array from the user and displays them in the reverse order using for loop.

Practical No.90

Task: Write a program that uses an array of 8 elements entered by user and then find out
total number of odd and even values entered by the user in a one dimensional array.

Practical No.91

Task: Write a program to enter the data in two linear arrays, add the two arrays and store
the sum in third array.

Practical No.92

Task: Write a program that calculates the sum of square of numbers stored in an array of
10 elements.

Practical No. 93

Task: Write a program that accepts five elements from a user in an array and find their
maximum and minimum.

Logic: Main Logic of this program is


1. We assume that first element (0) of array is maximum or minimum.
2. We compare the assumed max / min with the remaining (1... N-1) elements of the arrays and if a
number greater than max or less than min is found ,then that is put in the max or min variable,
overwriting the previous value.

Practical No. 94

Task: Write a program that should accepts five elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array its
index and contents (value) is displayed, else (not found) a massage “Element not found in
an array” is displayed. (Using Linear /Sequential Search Algorithm)

Practical No. 95

Task: Write a program that should accepts five elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array its
all (apparent) and total number of occurrences are displayed, else (not found) a massage
“Element not found in an array” is displayed.

Practical No. 96

Task: Write a program that should accepts 10 elements from the user in an array and search
a particular element (entered by the user) in it. If the element is found in an array its index
is displayed, else (not found) a massage “Element not found in an array” is displayed.
(Using Binary Search Algorithm)

Practical No. 97

Task: Write a program that accepts ten elements from a user in an array and sorts them in
ascending order using:

1. Bubble sort
2. Selection sort
Practical No. 98

Task: Write a program that accepts ten elements from a user in an array and sorts them in
Descending order.

Practical No. 99

Task: Write a program that accepts ten elements from a user in an array and sorts first five
elements in ascending order and rest of the five elements in descending order using bubble
sort.
Practical No. 100

Task: Write a program that accepts two arrays (A and B) of ten elements each from the user
and merges them in a single array (c) of twenty elements.

Practical No. 101

Task: Write a program to insert a new value at specified location in a 1D array


(Using Insertion Algorithm).

Practical No. 102

Task: Write a program to delete a value from a specified location in a 1D array


(Using Deletion Algorithm).
Practical No. 103

Task: Write a program that takes values from user to fill a two-dimensional array (Matrix)
having two rows and three columns and display the values in row column format.

Practical No. 104

Task: Write a program that takes values from user to fill a two-dimensional array (Matrix)
having three rows and three columns and display it’s contents and also display the flipping
the same matrix (i.e. reversing the row order) using functions.

Practical No. 105

Task: Write a program that should accepts five elements from the user in a 2D-array and
search a particular element (entered by the user) in it. If the element is found in an array its
indexes are displayed, else (not found) a massage “Element not found in an array” is
displayed (Using Linear /Sequential Search Algorithm).

Practical No. 106

Task: Write a program that takes values from user to fill a 2 two-dimensional array (Matrix)
having three rows and three columns and then add these two matrixes and store the result
in another matrix and display all matrixes values in row column format (Addition of two
matrixes).

Practical No. 107

Task: Write a program that takes values from user to fill a 2 two-dimensional array (Matrix)
having any order and then multiply these two matrixes if possible (i.e. Checking multiply
rule first) and store the result in another matrix and display all matrixes values in row
column format (Multiplication of two matrixes).

Practical No. 108

Task: Write a program that takes values from user to fill a two-dimensional array (Square
Matrix) then calculate the transpose of that matrix and display its contents in matrix form.

Practical No. 109

Task: Write a program use a single-subscripted array to solve the following problem. Read
in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print
it only if it is not a duplicate of a number already read. Provide for the “worst case” in which
all 20 numbers are different. Use the smallest possible array to solve this problem.

You might also like