0% found this document useful (0 votes)
11 views16 pages

C++ Array Declaration and Initialization Guide

This lab manual focuses on teaching students how to declare and initialize arrays of various data types in programming. It includes tasks that involve creating and manipulating arrays, such as storing student marks, handling user input, and performing operations on strings and numbers. The manual also introduces concepts like loops and functions to enhance programming skills.

Uploaded by

fatimahassan4293
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)
11 views16 pages

C++ Array Declaration and Initialization Guide

This lab manual focuses on teaching students how to declare and initialize arrays of various data types in programming. It includes tasks that involve creating and manipulating arrays, such as storing student marks, handling user input, and performing operations on strings and numbers. The manual also introduces concepts like loops and functions to enhance programming skills.

Uploaded by

fatimahassan4293
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

Programming Fundamental

Lab Manual - Week 09

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:
● Declare and initialize arrays of different data types.

Let's do some coding.

Skill: Declare and initialize arrays of different data types.

Introduction

By this week, you have learned how to write a program that contains functions, loops, and
conditional structures. In this class, we will learn about another very powerful concept
known as Arrays.

An array is a collection of similar data items stored at contiguous memory locations and
elements that can be accessed using the indices of an array.
Following are the types of arrays that are often used in programs.
● Integer arrays
● Float arrays
● Char arrays
● String arrays

Consider a task in which you want to store the mid-term marks of Programming
Fundamentals of 200 students. Let's create an Array to store the marks of 200 students of
the Computer Science Department of UET Lahore.

Array Declaration
Following is the Syntax of an Array For example,

dataType ArrayName[size]; int marks[200];

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

This C++ statement will declare an array of size in the memory of the computer. Now,
we can use the indices of the array to store the marks of the students at different
addresses.

Index of Array
An array is a collection of items on contiguous memory locations. Every item in the array
exists on a unique address in memory. Consider the following diagram for a better
understanding.

We can use the assignment operator to assign a value using the index of the array item.

Consider the following program for better understanding

Task 01(WP): Write a program that declares an array of 5 integer elements, initializes
them one by one and displays them.

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

Task 02(CA): Write a program that declares an array of 5 elements, initializes them one
by one and displays only the 2nd and 4th elements of an array.

Similarly, we can store the value entered by the user in the array by using the following
statement.
cin >> arrayName[index];

Task 03(WP): Write a program that declares an array of 5 elements, initializes them one
by one by user input, and displays 1st and last elements of the array.

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

� Problem �
We have used the same variable name to store multiple values but our code is repeating
for taking input.
Is there any better way ❓

� Solution �
We can use the counting loop for taking input in the array from the user

Consider the same question with loops below.

Task 04(WP): Write a program that declares an array of 5 elements, initialize them one
by one by user input and display 1st and last elements of the array.
Instruction: Use a loop for taking input from the user

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

Task 05(OP): Write a program that takes n numbers from the user, stores them in an
array, and displays those numbers on the screen.
Instruction: First of all the user will tell how many numbers he/she wants to enter.
Then take those numbers as input from the user and store them in the array.

Similarly, we can use the array for solving more complex problems.
Consider the following task for better understanding.

Task 06(CL): Write a program that prints the sum and average of the first 5 natural
numbers on the screen.

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

String as Character Array


String values are stored as a character array inside memory and the end of the string is
stored as ‘\0’ referred to as a null character.

Consider the following task for better understanding.


Task 07(WP): Write a program that prints whether a specific character is present in a
string or not.

Point to ponder:
We have used a counter loop in the check function.

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

Did you already knew how many times we had to iterate the loop..??
Which loop would be better in this case?

Consider the following amazing facts about the string variables.

Do you know? �
You can use the getline(cin, stringVariableName) function to take string input that
includes a space character.

Do you know? �
You can declare a string by using any of the following methods. Pretty cool, isn’t ?

Task 01(OP): (Word Postmortem)


Write a program that takes a word from the user and stores it in a character array and
passes it to a function that displays the location of all alphabets in the array.

Task 02(OP): (Reverse Word)

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

Write a function that takes a string as input parameter and displays it in reverse order.

Task 03(OP): (Next Letter)


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

Hint for solving this problem in an easy way:


ASCII (American Standard Code for Information Interchange) codes in C++ refer to
the numeric representations of characters in the ASCII character set. The ASCII
character set includes various characters, such as letters, digits, punctuation marks, and
control characters, each of which is assigned a unique numerical value.

In C++, you can obtain the ASCII code of a character by storing it in an int variable.
Here's a simple example:

char character = 'A'; // Change this to the character you want to find ASCII code for
int asciiCode = character;

cout << "The ASCII code for " << character << " is " << asciiCode;

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

Ignore the Hex column, just consider the Decimal and Char column.

Task 04(OP): (Reverse Numbers)


Write a program that takes n numbers from the user, stores them in an array, and passes
them to a function that prints them in reverse order.

Solution:

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

When you have to pass an array to a function you have to just give the name of the
array.

printReverseArray(arr, n);

But when to define a function that is receiving the array. You must give 2 things.
1. Complete Array (just add the bracket signs but do not give the size of the array
in the brackets)
2. Array Size

void printReverseArray(int arr[], int n)

Task 05(OP): (Validate Input)


Write a program that takes n numbers from the user and stores them in an array.
The program should print “Already Entered” if the user has already entered that number.

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

Instructions:
You must make a function to check if the number is already present in the array or not.

bool isAlreadyEntered(int arr[], int size, int number)

Task 06(OP): (Find Largest)


Write a program that takes n numbers from the user and stores them in an array and call a
function that returns the largest number entered by the user.
Function Prototype should be:
int findLargestNumber(int arr[], int size)

Task 07(CP): (Resistors)


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 program 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 ).
Create the function with the following prototype

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

double calculateTotalResistance(double resistance[], int size)

Test Cases:
[1, 5, 6, 3] ➞ "15"

[16, 3.5, 6] ➞ "25.5"


[0.5, 0.5] ➞ "1.0"

Task 08(CP): (Insert Array in Middle)


Create a program that takes two arrays and inserts the second array in the middle of the
first array. The first array always has two elements.
Function prototype should be

Test Cases:
[1, 10], [2, 3, 4, 5, 6, 7, 8, 9] ➞ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[15,150], [45, 75, 35] ➞ [15, 45, 75, 35, 150]

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

Task 09(CP): (Pay the Bill)


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. The change will always be represented in
the following order: quarters, dimes, nickels, and pennies.
[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:
[2, 100, 0, 0], 14.11 ➞ false
[0, 0, 20, 5], 0.75 ➞ true

[30, 40, 20, 5], 12.55 ➞ true


[10, 0, 0, 50], 3.85 ➞ false

[1, 0, 5, 219], 19.99 ➞ false


Function Prototype should be:
bool canPayWithChange(double change[], double totalDue)

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

Task 10(CP): (Something)


Write a program that prints the string "something" joined with a space " " and the given
argument a.
Test Cases:
"is better than nothing" ➞ "something is better than nothing"

"Bob Jane" ➞ "something Bob Jane"


"something" ➞ "something something"

Task 11(CP): (Vowels Removed)


Create a program that takes a string and returns a new string with all vowels removed.
Test Cases:
Input:
"I have never seen a thin person drinking Diet Coke."
Output:
" hv nvr sn thn prsn drnkng Dt Ck."

Task 12(CP): (Special Array or Not)


An array is special if every even index contains an even number and every odd index
contains an odd number. Create a function that returns t r ue if an array is special, and
f a l s e otherwise.
Function prototype should be

bool isSpecialArray(int arr[], int size)


Examples
isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3]) ➞ true
// Even indices: [2, 4, 6, 6]; Odd indices: [7, 9, 1, 3]

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

isSpecialArray([2, 7, 9, 1, 6, 1, 6, 3]) ➞ false


// Index 2 has an odd number 9.

isSpecialArray([2, 7, 8, 8, 6, 1, 6, 3]) ➞ false


// Index 3 has an even number 8.

Task 13(CP): (Jazzify)


Create a function which concatenates the number 7 to the end of every chord in an array.
Ignore all chords which already end with 7.

Function Prototype should be:


void jazzifyChords(string chords[], int numChords)

Examples
jazzify(["G", "F", "C"]) ➞ ["G7", "F7", "C7"]

jazzify(["Dm", "G", "E", "A"]) ➞ ["Dm7", "G7", "E7", "A7"]

Skill: Declare and initialize arrays of different data types


Programming Fundamental
Lab Manual - Week 09

jazzify(["F7", "E7", "A7", "Ab7", "Gm7", "C7"]) ➞ ["F7", "E7", "A7", "Ab7", "Gm7",
"C7"]

Good Luck and Best Wishes !!


Happy Coding ahead :)

Skill: Declare and initialize arrays of different data types

You might also like