0% found this document useful (0 votes)
3 views18 pages

Array Lab Manual

This document outlines the learning outcomes and programming exercises related to arrays in C/C++. It includes definitions, examples of array declarations, initialization, and various programming challenges involving arrays and strings. Additionally, it provides practical coding tasks to reinforce understanding of arrays and their applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Array Lab Manual

This document outlines the learning outcomes and programming exercises related to arrays in C/C++. It includes definitions, examples of array declarations, initialization, and various programming challenges involving arrays and strings. Additionally, it provides practical coding tasks to reinforce understanding of arrays and their applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Programming Fundamentals

Week 9

_______________________________________________________________________​

Learning Outcomes:
After this lesson, students will be able to:

●​ define the term 'array'


●​ identify arrays in real world settings
●​ create arrays using manipulative objects
●​ draw arrays and write matching multiplication equations

Instructions

∙ Use proper indentation to make your programs readable.

∙ Use descriptive variables in your programs (Name of the variables should show their purposes)

Array
Introduction
Array :

An array in C/C++ or be it in any programming language is a collection of similar data


items stored at contiguous memory locations and elements can be accessed randomly
using indices of an array. They can be used to store collection of primitive data types
such as int, float, double, char, etc of any particular type. For example
●​ Snooker balls that have same round shape but in different colour/value
●​ Bunch of banana has contain only banana in bunch but different in size/value
●​ You put your favourite song on a repeat mode.
●​ A book contain pages

But First, recall these concepts that you were taught in the earlier class.
Declaration and initializing of different Type of Array:

Memory View of array with indexes:

Array element with indexes view in Memory


Array :
Example #1:
Write a program that declare an array of 5 elements, initialize them one by one and
display them.

Solution

The code produces the following output

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

The code produces the following output

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

The code produces the following output


Challenge#1:
​ ​ ​ ​ Write a program that prompt the user to enter 10 number and store in an
array and program should display all elements in reverse order

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

Declaration of Character array:


​ ​ ​ ​ Here is representation of an character array in memory

String Input from user using cin:

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

Congratulations, you have performed all basics the tasks of an array

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"

seriesResistance([16, 3.5, 6]) ➞ "25.5 ohms"

seriesResistance([0.5, 0.5]) ➞ "1.0 ohm"

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]

tuckIn([15,150], [45, 75, 35]) ➞ [15, 45, 75, 35, 150]

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

changeEnough([0, 0, 20, 5], 0.75) ➞ true

changeEnough([30, 40, 20, 5], 12.55) ➞ true

changeEnough([10, 0, 0, 50], 3.85) ➞ false

changeEnough([1, 0, 5, 219], 19.99) ➞ false

CP:
Write a function that returns the string "something" joined with a space " " and the given argument a.

giveMeSomething("is better than nothing") ➞ "something is better than nothing"

giveMeSomething("Bob Jane") ➞ "something Bob Jane"

giveMeSomething("something") ➞ "something something"

CP:
Create a function that takes a string and returns a new string with all vowels removed.
Test Cases:

removeVowels("I have never seen a thin person drinking Diet Coke.")


➞ " hv nvr sn thn prsn drnkng Dt Ck."
removeVowels("We're gonna build a wall!")
➞ "W'r gnn bld wll!"

removeVowels("Happy Thanksgiving to all--even the haters and losers!")


➞ "Hppy Thnksgvng t ll--vn th htrs nd lsrs!"

You might also like