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

Lab 09

This document contains instructions and tasks for a programming fundamentals lab. The tasks include writing programs to: (1) calculate terms in a sequence defined by a recurrence relation, (2) find the smallest number in an integer array, (3) calculate the sum and product of array elements divisible by 3, (4) print various patterns using for loops, (5) print triangle patterns with user-defined characters or ascending/descending numbers, and (6) separate the even and odd elements of an array into two arrays. Students are instructed to include their code and output in a formatted Word document and not to plagiarize or discuss solutions with others.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Lab 09

This document contains instructions and tasks for a programming fundamentals lab. The tasks include writing programs to: (1) calculate terms in a sequence defined by a recurrence relation, (2) find the smallest number in an integer array, (3) calculate the sum and product of array elements divisible by 3, (4) print various patterns using for loops, (5) print triangle patterns with user-defined characters or ascending/descending numbers, and (6) separate the even and odd elements of an array into two arrays. Students are instructed to include their code and output in a formatted Word document and not to plagiarize or discuss solutions with others.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

National University of Computer & Emerging Sciences – FAST (CFD)

Department of Computer Science: Programming Fundamentals - Lab

National University of Computer and Emerging Sciences

Lab # 09
For

Programming Fundamentals Lab


Lab Instructor(s) Mr. Mughees Ismail
Semester Fall 2022

FAST School of Computing

Page 1|4
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Programming Fundamentals - Lab

Instructions:

1. Make a word document with the convention “SECTION_Lab#_ROLLNO” and put all
your source code and snapshots of its output in it.
2. Plagiarism is strictly prohibited, do not copy anything from internet, or any of your friend
else your assignment will be marked 0.
3. Do not discuss solutions with one another.
4. Write the code with proper dialogues to improve the user interaction with your program.

Task 01:
Suppose that the first number of a sequence is x, in which x is an integer.
Define a0 = x;
an+1 = an/2 if an is even; an+1 = 3 * an + 1 if an is odd.
Then, there exists an integer k such that ak = 1.
Write a program that prompts the user to input the value of x. The program output the integer k
such that ak = 1 and the numbers a0, a1, a2, . . . , ak. (For example, if x = 75, then k = 14, and the
numbers a0, a1, a2, . . ., a14, respectively, are 75, 226, 113, 340, 170, 85, 256, 128, 64, 32, 16, 8,
4, 2, 1.)
Test your program for the following values of x: 75, 111, 678, 732, 873 2048, and 65535.
Use For Loops only

Task 02:
Find the smallest Number from an integer array of length 5.
For Example:

Take the numbers from the user as an input.

Task 03:
Write a C++ Program to Calculate Product and Sum of all Elements divisible by 3 in an Array.
The program takes an array of elements as input, and calculates the sum and product of all
elements divisible by 3 of the array.
For Example:

Page 2|4
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Programming Fundamentals - Lab

Task 04:
Make a menu driven program that prints the following patterns. Use For Loops only

11111
22222
33333
44444
55555

12345
12345
12345
12345
12345

55555
44444
33333
22222
11111

54321
54321
54321
54321
54321

Ask the user to choose the pattern they want to print on screen.

Task 05:
Consider the following triangle. Use For Loops only

#####
####
###
##
#

Make a menu driven program that asks the user for the following variations namely.
 Triangle with ‘x’, x being a character input by the user.

Page 3|4
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Programming Fundamentals - Lab

 Triangle with numbers in Ascending order.


 Triangle with numbers in Descending order.

Task 06:
Write a C++ Program to Put Even & Odd Elements of an Array in 2 Separate Arrays.
The program first finds the odd and even elements of the array. Then the odd elements of an
array are stored in one array and even elements of an array is stored in another array.
Problem Solution
1. Create three integer arrays. First to store all the elements. Second to store even elements
of first array. Third to store odd elements of first array. Assumption: take a fixed size
array for original elements of even size e.g., 10, Take two more arrays of size half than
the size of original array.
2. Using for loop, scan each element of the first array, check whether they are even or odd
by taking modulo of 2 on those numbers.
3. Even numbers will get stored(copied) in second array, while odd numbers will get stored
in third array.
4. Print both arrays.

Best of lucks

Page 4|4

You might also like