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

Lab-05

The lab focuses on iterative algorithms and their implementation in C/C++ using loops. Students will complete various tasks including printing number series, checking for prime numbers, calculating city populations, simulating a grid game, and analyzing cricket statistics. The lab aims to enhance understanding of loops, flow charts, and real-world applications of programming concepts.

Uploaded by

contact.modenzo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab-05

The lab focuses on iterative algorithms and their implementation in C/C++ using loops. Students will complete various tasks including printing number series, checking for prime numbers, calculating city populations, simulating a grid game, and analyzing cricket statistics. The lab aims to enhance understanding of loops, flow charts, and real-world applications of programming concepts.

Uploaded by

contact.modenzo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Department of Computing

CS 114: Fundamentals of Programming

Class: BEEE - 15A


Fall 2023

Lab - 05
Iterative Algorithms,
Support for Iterative Algorithms
through C/C++ Loops,
Real-World Applications
[Azad’s Law: The one facilitating cheating will be punished with 0 marks]

The objective of this lab is to enable students to answer following questions:

● Why are iterative algorithms used for solving a problem?


● How does C/C++ support these algorithms using loops?
● Dissection of loop into distinct components ; initialization, termination and
increment
● How to use flow charts to capture iterative algorithms?
● What is the difference between for and while loop and when to use each of
them?
● How to apply these concepts to real-world applications

If you want to do something repetitively, you can do it effectively using loops. Consider
an example where one has to write a program to print 1,2,3, …..,N integers where N is
the input taken from the user.
TASK - 01 : Take the positive integer N from the user and print the following series:

a. Print the numbers from N to 0 in descending order


b. Print the numbers 1, 2, 3, 5, 6, 8, 9,......N
c. Print the numbers from 0 to N with an increment. This increment will be taken as
input from the user and it will be less than 1 e.g. 0.5. If the user enters increment
greater than 1, the program should print error and exit, otherwise it should print
numbers from 0 to N with the increment specified by the user.

Flow Chart : 15 minute


Coding : 15 minute

TASK - 02 : Write a program to figure out whether N is a prime number or not. N will be
taken as input from the user.

Flow Chart : 10 minute


Coding : 10 minute

TASK - 03 : Take the number of cities of Pakistan as input from the user. Ask the user to
enter the population of each city. Find out the mean, maximum and minimum
population?

Flow Chart : 15 minute


Coding : 10 minute

TASK - 04: Imagine you are playing a grid (4x4) game where the player can move in
four directions Up, Down, Right and Left. Take the initial position of the player from the
user e.g. (1,2). Now take the direction in which the player will move and keep doing it
until it collides with the boundary. Figure out whether it collides with the boundary or
not? If it collides, it should simply exit by printing the coordinate of the boundary in the
form “The player has reached the boundary at (x,y)” where x,y will be the coordinates of
the boundary.
(1,1) (4,1)

(1,4) (4,4)

Flow Chart : 15 minutes Coding : 25 minutes


TASK - 05: Histogram Calculation - Take the number of characters N as input from the
user. Now ask the user to enter each character. Your program should figure out the
frequency of each vowel (a,e,i,o,u) and others.

Sample Output for N = 10:


Frequency of a = 2
Frequency of e = 1
Frequency of i = 1
Frequency of o = 1
Frequency of u = 1
Frequency of others = 5

Flow Chart : 20 minutes


Coding : 20 minutes

TASK - 06: Cricket Statistics - There is cricket fever out there everywhere in Pakistan.
Consider Babar Azam statistics for the last few world cups. Take the number of matches
he has played in world cups. Now take the runs scored by Babar Azam in each match
along with the balls he faced. At the end your program should print the following for
Babar Azam:
● strike rate,
● batting average
● minimum score
● maximum score
● number of centuries

Flow Chart : 20 minutes


Coding : 20 minutes

Good Luck

You might also like