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

CS210 DSA Lab 01

Here are the extra tasks: Task 1: Write a function that swaps the contents of two integer arrays: void swapArrays(int array1[], int array2[], int size){ int temp; for(int i=0; i<size; i++){ temp = array1[i]; array1[i] = array2[i]; array2[i] = temp; } } To test it: int main(){ int array1[25], array2[25]; //populate arrays swapArrays(array1, array2, 25); //arrays are now swapped return 0; } Task 2:

Uploaded by

Saqib Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

CS210 DSA Lab 01

Here are the extra tasks: Task 1: Write a function that swaps the contents of two integer arrays: void swapArrays(int array1[], int array2[], int size){ int temp; for(int i=0; i<size; i++){ temp = array1[i]; array1[i] = array2[i]; array2[i] = temp; } } To test it: int main(){ int array1[25], array2[25]; //populate arrays swapArrays(array1, array2, 25); //arrays are now swapped return 0; } Task 2:

Uploaded by

Saqib Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CS 210 – Data Structures and Algorithms Lab Manual

LAB NO. 01
PROGRAMMING CONCEPTS (REVISION)

Following are the lab objectives:


Lab Objectives

1. Apply decision structure

2. Apply repetitive structure

3. Apply string manipulation techniques

4. Apply array manipulation techniques

5. Define functions for

Student Saqib Altaf


ID 3249 Student Name
Obtained
Marks Comments
Marks
Task 1 10
Task 2 10
Task 3 10
Task 4 10
Task 5 10
Total
50
Marks

Lab Instructor

1
CS 210 – Data Structures and Algorithms Lab Manual

Lab Objectives and CLOs Mapping

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:

Enter a latitude in degrees, minutes, and seconds:


First, enter the degrees: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degree

#include<iostream>

using namespace std;

int main()

double degree, minutes , second, result;

const int sec=60;

cout<< "Enter latitude in degree :: ";

cin >> degree;

cout<< "Enter latitude in minutes :: ";

cin >> minutes;

3
CS 210 – Data Structures and Algorithms Lab Manual

cout<< "Enter latitude in second :: ";

cin >> second;

minutes = minutes + second / sec;

degree = degree + minutes / sec;

cout << "\n\t\tResult :: "<<degree<<endl;

return 0;

Task 2 (10 mark)

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:

Enter the number of seconds: 31600000

31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds

#include <iostream>

using namespace std;

int main()

using namespace std;

const int sec_per_minutes = 60;

const int min_per_hour = 60;

const int hour_per_day = 24;

cout << "Enter numbers of second: ";

long long Enter_Second;

5
CS 210 – Data Structures and Algorithms Lab Manual

cin >> Enter_Second;

int day, hour, minutes, second;

day = Enter_Second / (sec_per_minutes * min_per_hour * hour_per_day);

hour = (Enter_Second - day * hour_per_day * min_per_hour * sec_per_minutes) /


(sec_per_minutes * min_per_hour);

second = Enter_Second % sec_per_minutes;

minutes = (Enter_Second - second) / sec_per_minutes % min_per_hour;

cout << Enter_Second<< " seconds = ";

cout << day << " days, ";

cout << hour << " hours, ";

cout << minutes << " minutes, ";

cout << second << " seconds.";

return 0;

6
CS 210 – Data Structures and Algorithms Lab Manual

Task 3 (10 mark)

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>

using namespace std;

int main()

string str = "saqib altaf Auic";

int result = 0;

7
CS 210 – Data Structures and Algorithms Lab Manual

int count = 0;

for (int i = 0; i<str.length(); i++ )

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;

cout << "String :: "<<str<<endl;

cout << "Number of Vowel in string :: "<<result<<endl;

return 0;

8
CS 210 – Data Structures and Algorithms Lab Manual

Task 4 (10 mark)

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.

Write a function called item_order that takes as input a string named order. The


string contains only words for the items the customer can order separated by one
space. The function returns a string that counts the number of each item and
consolidates them in the following order: salad:[# salad] hamburger:[#
hambruger] water:[# 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:

If order = "salad water hamburger salad hamburger" then the


function returns "salad:2 hamburger:2 water:1"

If order = "hamburger water hamburger" then the function


returns "salad:0 hamburger:2 water:1"

(Paste your code here)

Task 5 (10 mark)

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>

using namespace std;

void perfectNumber(int number);

void AllPerfectNumber();

int main ()

int num;

cout << "Enter the number : ";

cin >> num;

perfectNumber(num);

AllPerfectNumber();

return 0;

void AllPerfectNumber()

10
CS 210 – Data Structures and Algorithms Lab Manual

int num = 1000;

cout<< "\n\nPerfect Number Between 1 to 1000 ";

for(int i=1; i<=num; i++){

perfectNumber(i);

cout<<"\nEnd of list\n";

void perfectNumber(int number)

int div;

int PerfectNumber=0;

for ( int i=1; i < number; i++)

div = number % i;

if (div == 0)

PerfectNumber = PerfectNumber + i;

if (PerfectNumber == number)

11
CS 210 – Data Structures and Algorithms Lab Manual

cout << endl <<"\t\t" << number <<" is a perfect number.";

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

a) Total distance travelled by the salesman


b) Total items sold by the salesman
c) Salary of the salesman for that particular week.

13

You might also like