0% found this document useful (1 vote)
106 views

Lab 06: Arrays & Functions Objective(s) :: Exercises

This document outlines a lab assignment on arrays and functions in C++. The objectives are to understand arrays and functions. There are 13 exercises involving declaring and initializing arrays, taking user input to populate arrays, accessing array elements, merging arrays, performing operations on 2D and 3D arrays, and writing functions to calculate prices, grades, and perform basic math operations using arrays as parameters. Students are asked to write programs to complete the given tasks.

Uploaded by

Ahsan Ali Gopang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
106 views

Lab 06: Arrays & Functions Objective(s) :: Exercises

This document outlines a lab assignment on arrays and functions in C++. The objectives are to understand arrays and functions. There are 13 exercises involving declaring and initializing arrays, taking user input to populate arrays, accessing array elements, merging arrays, performing operations on 2D and 3D arrays, and writing functions to calculate prices, grades, and perform basic math operations using arrays as parameters. Students are asked to write programs to complete the given tasks.

Uploaded by

Ahsan Ali Gopang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSC-121 – Programming Fundamentals

Semester-I (Fall 2021)


Course Instructor(s): Muhammad Atif

Lab 06: Arrays & Functions

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).

 Access only 5th value from array

 Change value of 3rd value to 20.

 Print ODD VALUES stored in array.

 Print values which are on ODD INDICES ONLY.

 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:

#Sample Program Run#1


===================================
Matrix A – Original
===================================
12 23 25 4 6 8 2 7 9 11
===================================
Matrix A – Reverse
===================================
11 7 2 8 6 4 25 23 12
5. Write a C++ Program that checks whether the two arrays are equal or not. Declare
two Integer Arrays with 7 elements, and fill up the array with keyboard input. Test
if every element in Array 1 is equal to corresponding element in Array 2. For
example, the program should check A[0] = B[0], A[1] = B[1], and so for. 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:

Enter [0][0] of Matrix A: 2


Enter [0][1] of matrix A: 3
Enter [1][0] of matrix A: 4
Enter [1][1] of matrix A: 5

Enter [0][0] of Matrix B: 6


Enter [0][1] of matrix B: 7
Enter [1][0] of matrix B: 8
Enter [1][1] of matrix B: 9

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:

8. Write a program to declare 3D array of [2][2][4] size to store 16 values. Take 16


values from user and print all values.
9. Write a program to declare 4D array of [2][2][2][2] size to store 16 values. Take
16 values from user and print all values.

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
***************************************

You might also like