Lab 10-Programming Fundamentals
Lab 10-Programming Fundamentals
Lab 10-Programming Fundamentals
Introduction
Welcome back to your favorite Programming Lab students. In this lab manual, we
shall work together to learn and implement new programming concepts.
Skills to be learned:
Decompose the problem into small set of problems and then use arrays to solve
them
Skill: Decompose the problem into small set of problems and then use arrays to solve
them
Introduction
By this week, you have learned how to write a program that contains functions, loops,
arrays and conditional structures. In this class, we will learn how to decompose
difficult problems into small sets of easy problems and then solve them easily.
Task 01(CP): To train for an upcoming marathon, Kaka goes on one long-distance
run each Saturday. He wants to track how often the number of miles he runs this
Saturday exceeds the number of miles run the previous Saturday. This is called a
progress day.
Create a program that takes in an array of miles run every Saturday and returns Kaka's
total number of progress days.
Test Cases:
Input Output Explanation
[3, 4, 1, 2] 2 There are two progress days, (3->4) and (1-
>2)
[10, 11, 12, 9, 10] 3
[6, 5, 4, 3, 2, 9] 1
[9, 9] 0
Task 02(CP): Declare a String array and take input from the user.
Suppose the user has entered the following data in the array.
{"programming", "is", "my", "life", "now"}
Now, ask the user to Enter a letter.
Suppose the user has entered the letter “o”.
Now your task is to count the number of times a particular letter shows up in the array
search.
Test Cases:
Input Output
Enter how many words you want to Enter: 5 o shows up 2 times in the data.
Enter word 1: programming
Enter word 2: is
Enter word 3: my
Enter word 4: life
Enter word 5: now
Enter the letter you want to count: o
Enter how many words you want to Enter: 5 m shows up 3 times in the data.
Enter word 1: programming
Enter word 2: is
Enter word 3: my
Enter word 4: life
Enter word 5: now
Enter the letter you want to count: m
Task 03(CP): Write a program that stores all the elements in an array that are strictly
greater than their adjacent left and right neighbours in a new array and then display
that array.
Note:
Do not count boundary numbers, since they only have one left/right neighbor.
If no such numbers exist, display “No Peak found”.
Test Cases:
Input Output
How many Elements you want to Enter: 8 [5, 9]
[4, 5, 2, 1, 4, 9, 7, 2] //5 has neighbours 4 and 2, both are less
than 5.
How many Elements you want to Enter: 9 [2, 3, 5]
[1, 2, 1, 1, 3, 2, 5, 4, 4]
How many Elements you want to Enter: 6 “No Peak found”
[1, 2, 3, 4, 5, 6]
Task 04(CP): Given an array of boxes, create a program that displays the total
volume of all those boxes combined together. A box is represented by consecutive 3
elements in the array: length, width and height.
For instance, [2, 3, 2, 6, 6, 7, 1, 2, 1] should display 266 since (2 x 3 x 2) + (6 x 6 x 7)
+ (1 x 2 x 1) = 12 + 252 + 2 = 266.
Test Cases:
Input Output
[4, 2, 4, 3, 3, 3, 1, 1, 2, 2, 1, 1] 63
[2, 2, 2, 2, 1, 1] 10
[1, 1, 1] 1
Notes
Task 05(CP): Imagine you are a warehouse manager and you have a list of 10
packages with their respective weights. Your task is to sort the packages in ascending
order of weight so that the lighter packages can be loaded onto delivery trucks first.
How would you sort the packages based on their weight in the same array? Write a C+
+ program to sort the same array in ascending order.
Input Output
Here is an example of the data in the array Here is the same sorted array in ascending
of packages: order:
[120, 45, 78, 23, 56, 89, 34, 67, 101, 243] [23, 34, 45, 56, 67, 78, 89, 101, 120, 243]
Task 06(CP): Given an input string, reverse the string word by word, the first word
will be the last, and so on.
Test Cases:
reverseWords("the sky is blue") ➞ "blue is sky the"
reverseWords("hello world!") ➞ "world! hello"
reverseWords("a good example") ➞ "example good a"