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

Lab Manual Week 10

Uploaded by

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

Lab Manual Week 10

Uploaded by

syedmeg4036s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Riphah International University

I-14 Main Campus


Faculty of Computing

Class: Fall-2024 Subject: PF


Course Code: Lab Instructor: Mr. Awais Nawaz

-------------------- LAB 10--------------------

Learning Objective:
1. Introduction to Arrays in C++?
2. Syntax of Arrays in C++.
3. Practical walkthrough
4. Lab Task

Introduction to Arrays
An array is basically a collection of data-items or values all based on same data-type. For
example: A student’s marks in five courses in a semester: 78, 91, 83, 67, and 89; represent a
collection of five int type numbers where each value represents his/her marks in a certain
course. An array is a structured data-type (collection of values) of some fixed size (number of
elements/values) where all values are of same type.

Syntax of Arrays
To define an array you have to mention its type, name and size (number of elements/values the
array will contain). For example:
int marks[5];

Above statements defines an int type array named marks, having size: 5 (capable of storing
five values).In the similar way, you can create array of any basic type, Examples:

Individual array elements can be accessed using same name (array name) along its index. An
index (which must be an integer value) indicates relative position of an element/value within
an array. In C++, array are 0-index based, it means that index of the starting/first element of
an array is 0. For example: int marks[5]; defines an int-type array having five elements, where
first value or element will be at index 0, second on index 1, thirds element at index 2, fourth at
index 3, and the fifth element at index 4. Examples:

As you have learned that array elements can be accessed using index values. Therefore, it is
common and a very convenient way to access array elements using loops (mainly for-loop).
For example, below shown code gets input from the user for all five elements of array marks.

The C-style character string originated within the C language and continues to be supported
within C++. This string is actually a one-dimensional array of characters which is terminated
by a null character '\0'. Thus a null-terminated string contains the characters that comprise the
string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing the
string is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

Practical Walkthrough
Write a C++ program that reads marks of a class (having 5 students) from the user and places
them in an array of type float. After that, it should compute average marks obtained by the
class. In the end, display student number and their marks for all those students who got marks
above average. To achieve such output, you need to follow the following instructions.
Output
Lab Task
1. Write the output of the following code (assume code is error free)

Output

2. Write a program that is able to compute some operations on an integer array of size 15.
The program writes the value of the integer and writes the following menu:
1. Add 2 if even otherwise convert it to even by adding 1 to the number

2. Count numbers divisible by 6.

3. Multiply a number by 2.

4. Subtract 4 from numbers

5. How many integers are greater than or equal to 10.

6. Quit
The programs ask the user to type a value between 1 and 5. If the user types a value
from 1 to 5 the operation is computed, the integer is written and the menu is displayed
again. If the user types 6, the program quits.

3. Write a program which takes 2 arrays of 10 integers each, “a” and “b”. c is an array
with 20 integers. The program should put into c the appending of b to a, the first 10
integers of c from array a, the latter 10 from b. Then the program should display c.
4. Write a program that takes character array as an input from user and count the number
of vowels.
5. Write a program that takes character array as an input and count the number of capital
character in it.
6. Write a C++ program that creates two one dimensional arrays “A” and “B” of size 10
elements each. The main program should get the input in the array “A” from the user
and initialize array “B” with values “0”. Your program should calculates the square
(number * number) of each element of array “A” and should store in the square value
in corresponding positions (index) in the array “B”. In the end, the main program (main
function) prints the calculated values of array “B”. Also display the values in array B
which are divisible by 3 (if any).

• Sample Inputs: Please enter values in array A: 2 8 3 4 10 5 9 3 2 1

• Sample Outputs: The values in Array B are: 4 64 9 16 100 25 81 9 4 1

• Values in Array B which are divisible by 3: 9 819


7. Write a C++ program that creates an array “A” having 5 elements (in the main
function). Then, the program asks the user to enter values in this array. After that, the
program should replace each array element which is odd with an even value (by adding
1 to it). In the end, the main program (“main”) shows the updated array elements in
ascending order.
• Sample Inputs: Enter 5 elements in array A: 4 5 9 8 2
• Sample Outputs: Sorted Array 2 4 6 8 10

You might also like