CS210 DSA Lab 01
CS210 DSA Lab 01
LAB NO. 01
PROGRAMMING CONCEPTS (REVISION)
Lab Instructor
1
CS 210 – Data Structures and Algorithms Lab Manual
CLOs
Lab Objectives
a b c
1
2
3
4
5
Instructions
This is individual Lab work/task.
Complete this lab work within lab timing.
Discussion with peers is not allowed.
You can consult any book, notes & Internet.
Copy paste from Internet will give you negative marks.
Lab work is divided into small tasks, complete all tasks sequentially.
Show solution of each lab task to your Lab Instructor.
In-Lab Exercises/Tasks
Write your code at provided space after each question
You need to upload code for all tasks at Google Class.
2
CS 210 – Data Structures and Algorithms Lab Manual
LAB TASKS
Task 1 (10 mark)
Write a program that asks the user to enter a latitude in degrees, minutes, and seconds
and that then displays the latitude in decimal format. There are 60 seconds of arc to a
minute and 60 minutes of arc to a degree; represent these values with symbolic
constants. You should use a separate variable for each input value. A sample run
should look like this:
#include<iostream>
int main()
3
CS 210 – Data Structures and Algorithms Lab Manual
return 0;
Write a program that asks the user to enter the number of seconds as an integer value
(use type long, or, if available, long long) and that then displays the equivalent
time in days, hours, minutes, and seconds. Use symbolic constants to represent the
4
CS 210 – Data Structures and Algorithms Lab Manual
number of hours in the day, the number of minutes in an hour, and the number of
seconds in a minute. The output should look like this:
#include <iostream>
int main()
5
CS 210 – Data Structures and Algorithms Lab Manual
return 0;
6
CS 210 – Data Structures and Algorithms Lab Manual
Assume str is a string of lower case characters. Write a program that counts up the
number of vowels contained in the string str. Valid vowels are: 'a', 'e', 'i',
'o', and 'u'. For example, if str = 'azcbobobegghakl', your program
should print:
Number of vowels: 5
#include<iostream>
#include<cstring>
#include<stdlib.h>
int main()
int result = 0;
7
CS 210 – Data Structures and Algorithms Lab Manual
int count = 0;
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' )
result = result + 1;
return 0;
8
CS 210 – Data Structures and Algorithms Lab Manual
A catering company has hired you to help with organizing and preparing customer's
orders. You are given a list of each customer's desired items, and must write a
program that will count the number of each items needed for the chefs to prepare. The
items that a customer can order are: salad, hamburger, and water.
If an order does not contain an item, then the count for that item is 0. Notice that each
item is formatted as [name of the item][a colon symbol][count of the item] and all
item groups are separated by a space.
For example:
9
CS 210 – Data Structures and Algorithms Lab Manual
An integer is said to be a perfect number if the sum of its divisors, including 1 (but
not the number itself), is equal to the number.
For example, 6 is a perfect number, because 6 = 1 + 2 + 3.
a) Write a function that determines whether a given number is a perfect number or
not.
b) Write another function that prints all the perfect numbers between 1 and 1000.
(Hint: You can use function which you’ll define in part a.)
#include<iostream>
void AllPerfectNumber();
int main ()
int num;
perfectNumber(num);
AllPerfectNumber();
return 0;
void AllPerfectNumber()
10
CS 210 – Data Structures and Algorithms Lab Manual
perfectNumber(i);
cout<<"\nEnd of list\n";
int div;
int PerfectNumber=0;
div = number % i;
if (div == 0)
PerfectNumber = PerfectNumber + i;
if (PerfectNumber == number)
11
CS 210 – Data Structures and Algorithms Lab Manual
12
CS 210 – Data Structures and Algorithms Lab Manual
EXTRA TASKS
Task 1
The arrays array1 and array2 each hold 25 integer elements. Populate the arrays with
some random values. Write code that swap the contents of array1 and array2.
Task 2
Suppose that you are given a problem in which a salesman has to visit different cities
on the map and cities are represented by x and y coordinates in the Cartesian
coordinate system. The weekly salary of the salesman will depend on total distance
travelled in the week and number of items sold.
For example, if the salesman has travelled p meters and sold q items then his weekly
salary will be p*10+ q*15. (* represents the multiplication operation)
You are required to make a software program to calculate weekly salary of the
salesman if the salesman visits 6 cities in a week. Coordinates of the cities have to be
taken as input for the user. Number of items sold in each city also has to be taken as
input from the user.
At the end of execution your program should show the following information
13