Lab 06: Arrays & Functions Objective(s) :: Exercises
Lab 06: Arrays & Functions Objective(s) :: Exercises
Objective(s):
After performing lab, students will be able to:
1. Understands Array
2. Understands Function
1: Arrays
Lab Tasks:
Exercises
1. Write a program to declare and initialize an array with 10 values and display Sum and
Average of Array Elements Using for Loop.
2. Write programs to perform following operations:
Declare an array of size 10 & initialize an array to (55, 63, 78, 98, 10, 24, 32, 55,
20, 47) and print array without loop.
Take user input to declare N size of array & take N times input, store in array
and print (use loop).
Take input from user and find whether it is present in array or not.
Take an input from user and print number of values greater than input.
Take an input from user and print of values less than input.
3. Create two arrays arr1, arr2 of N, M integers respectively and store any values.
Create third array arr3 of N + M integers which should be empty initially. Create a
mergeList(arr1, arr2, arr3) function that merge both arr1, arr2 and store in arr3.
Input:
23 999
8 10 20 4 22
Output:
1 2
8 20 4 23 999 87 51 74
0 2
4.
Write a C++ program that ask user to enter 10 integer values. Store those values in one-
dimension array. Create another one-dimension array of same size, and store the values of
first array in reverse order. Print the results on screen.
Your Program should display output as follows:
6. Write a C++ Program that computes the sum of two matrices. Each matrix is of 2
rows and 2 columns and will be created from user input.
Output of the program is as follows:
A= 2 3 + B= 6 7 = C= 8 10
4 5 8 9 12 14
7. Write a C++ program, that read 12 integer values from user, store values in Matrix
of 4 X 3. Create another Matrix of 4 X 3, divide each element of Matrix1 by five,
and store the result in the Matrix2.
Print Matrix A, with heading shown, correctly spaced.
Print Matrix B, with heading shown, correctly spaced.
Your Program should display output as follows:
10. Write a C++ Program that contains one user defined function month().
In main() function:
Read an integer input in between (1 to 12) and store it in month_of_year.
Call month(month_of_year)
In month() function:
Print the corresponding month of year in month().
Example: Value of parameter is 4... Print “April”.
11. Write a C++ Program that contains one user defined function cal_grades().
In main() function:
Prompt user to enter obtained(0 - 100) marks for one subject.
Call cal_grades(marks_subject).
Print the corresponding Grade with respect to Marks.
In user defined function:
Perform conditioning with else if statement return char value.
Function must return value.
12. Write a C++ Program that contains four user defined function(s) addition(),
subtraction(), division(), multiplication(). Develop a calculator as follows
In main() function:
A menu with choices addition, subtraction, division and multiplication must
be displayed.
Get two numbers and a choice from user
Call the respective functions with user given number as parameter using
switch statement
Print the result from addition(), subtraction(), division(), multiplication().
In user defined functions:
Plus and Minus function get two integer values and return integer.
Multiply and Divide functions get two integer values and return float.
13. Write a C++ that calculate price of purchased fruits.
A shopkeer supplies following fruits.
Apple, Banana, Mango, Peach and Grapes
Unit of each fruit per kg is:
Apple = 160
Banana = 120
Mango = 110
Peach = 100
Grapes = 130
Ask user to enter purchased quantity of each fruits. Store values in variables.
Write a function Cal_Pric (int, int, int& total) that calculate the price for each fruit.
For example Cal_Price(160,2,total) saves 320 in variable total.
Print the results from main():
#Sample Run
==================================
How many Apples did you buy : 2
How many Banana did you buy : 1
How many Mango did you buy : 3
How many Peach did you buy : 4
How many Grapes did you buy : 2
===================================
Price for Apple: 2 * 160 = 320
Price for Banana 1 * 120 = 120
Price for Mango: 3 * 110 = 330
Price for Peach: 4 * 100 = 400
Price for Grapes: 2 * 130 = 260
***************************************
Total Price of your purchase is: 1430
***************************************