Array Lab Manual
Array Lab Manual
Week 9
_______________________________________________________________________
Learning Outcomes:
After this lesson, students will be able to:
Instructions
∙ Use descriptive variables in your programs (Name of the variables should show their purposes)
Array
Introduction
Array :
But First, recall these concepts that you were taught in the earlier class.
Declaration and initializing of different Type of Array:
Solution
Example #2
Write a program that declare an array of 5 elements, initialize them one by one and
display 2nd and 4th elements of an array.
Solution
Example #3
Write a program that declare an array of 5 elements, initialize them one by one by
user input and display 1st and last elements of an array.
Solution
The code produces the following output
Example #4:
Write a program that take input from user and store them in an array.
Solution
The code produces the following output
Example #2:
Display sum and average of array element using loop.
Solution
The code produces the following output
Example #5:
Write a program that take 10 input from user and store them in an array , program should also asked for
number from user and find that number whether exit in store array or not.
Solution
The code produces the following output
Example #6:
Write a program that take 10 input from user and store them in an array , program should also asked for
scalar value from user and show scalar product operation on array element.
Solution
The code produces the following output
Example #7:
Write a program that take 10 input from user and store them in an array , program should display largest
element of an array
Solution
Input:
2 4 5 6 7 8 3 9 23 21
Output
21 23 9 3 8 7 6 5 4 2
Challenge#2
Write a program that take 10 input from user and store them in an array , program should display
smallest element of an array
Input:
21 23 9 1 8 7 6 5 4 2
Output:
1
String:
Flow of characters is called string or array of character is called string; every key on the key
board is character except special and functional keys. Array of char
Example #1:
Write a program that store “Pakistan” in an array and display on the console.
Solution
The code produces the following output
Example #1:
Write a program that store “Pakistan” in an array and display the location of all alphabets in array
.
Solution
The code produces the following output
Challenge#1
Write a program that store “hello” and display in reverse order as “olleh”
Challenge # 2:
Write a C++ function to change every letter in a given string with the letter following it in the
alphabet (ie. a becomes b, p becomes q, z becomes a).
For Example:
Input: aslam
Output: btmbn
Challenge#4:
Write a C++ function to count all the vowels in a given string.
Example:
Sample Input: eagerer
Sample output: number of vowels: 4
CP:
When resistors are connected together in series, the same current passes through each resistor in the chain
and the total resistance, RT, of the circuit must be equal to the sum of all the individual resistors added
together. That is:
RT = R1 + R2 + R3 ...
Create a function that takes an array of values resistance that are connected in series, and calculates the
total resistance of the circuit in ohms. The ohm is the standard unit of electrical resistance in the
International System of Units ( SI ).
Test Cases:
seriesResistance([1, 5, 6, 3]) ➞ "15 ohms"
CP:
Create a function that takes two arrays and insert the second array in the middle of the first array.
The first array always has two elements.
Test Cases:
tuckIn([1, 10], [2, 3, 4, 5, 6, 7, 8, 9]) ➞ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
CP:
Given a total due and an array representing the amount of change in your pocket, determine whether or
not you are able to pay for the item. Change will always be represented in the following order: quarters,
dimes, nickels, pennies.
To illustrate: changeEnough([25, 20, 5, 0], 4.25) should yield true, since having 25 quarters, 20 dimes, 5
nickels and 0 pennies gives you 6.25 + 2 + .25 + 0 = 8.50.
NOTE:
● quarter: 25 cents / $0.25
● dime: 10 cents / $0.10
● nickel: 5 cents / $0.05
● penny: 1 cent / $0.01
Test Cases:
changeEnough([2, 100, 0, 0], 14.11) ➞ false
CP:
Write a function that returns the string "something" joined with a space " " and the given argument a.
CP:
Create a function that takes a string and returns a new string with all vowels removed.
Test Cases: