Lab 6 Arrays

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

LAB MANUAL

Programming Fundamentals
CS-323

University Institute of Information Technology


PMAS-Arid Agriculture University, Rawalpindi
Lab 6 Arrays

 Objectives of this lab


 Declaration of Array
 Initialization of Array
 Accessing elements of Array
 Printing arrays
 Copying arrays
 Scanning array elements using cin
 Dealing with characters using arrays

C++ Arrays

We will learn to work with arrays. We will learn to declare, initialize, and access array elements in C++
programming with the help of examples.

In C++, an array is a variable that can store multiple values of the same type. For example,

Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating 27
separate variables, we can simply create an array:

double grade[27];

Here, grade is an array that can hold a maximum of 27 elements of double type.

In C++, the size and type of arrays cannot be changed after its declaration.

Access Elements in C++ Array

In C++, each element in an array is associated with a number. The number is known as an array
index. We can access elements of an array by using those indices.

// syntax to access array elements


array[index];

Consider the array  x  we have seen above.

CS-323 Programming Fundamentals


Elements of an array in C++

Few Things to Remember:

 The array indices start with 0. Meaning x[0] is the first element stored at index 0.
 If the size of an array is n, the last element is stored at index (n-1). In this
example, x[5] is the last element.
 Elements of an array have consecutive addresses. For example, suppose the starting
address of x[0] is 2120.
 Then, the address of the next element x[1] will be 2124, the address of x[2] will be 2128,
and so on.
 Here, the size of each element is increased by 4. This is because the size of int is 4 bytes.

C++ Array Initialization

In C++, it's possible to initialize an array during declaration. For example,

// declare and initialize and array


int x[6] = {19, 10, 8, 17, 9, 15};

Another method to initialize array during declaration:

// declare and initialize an array


int x[] = {19, 10, 8, 17, 9, 15};

Here, we have not mentioned the size of the array. In such cases, the compiler automatically
computes the size.

CS-323 Programming Fundamentals


C++ Array With Empty Members

In C++, if an array has a size n, we can store upto n number of elements in the array. However,
what will happen if we store less than n number of elements.

For example,

// store only 3 elements in the array


int x[6] = {19, 10, 8};

Here, the array x has a size of 6. However, we have initialized it with only 3 elements.

In such cases, the compiler assigns random values to the remaining places. Oftentimes, this
random value is simply 0.

How to insert and print array elements?

Copying arrays

CS-323 Programming Fundamentals


Suppose that after filling our 4 element array with values, we need to copy that array
to another array of 4 int ? Try this:

#include <iostream.h>

int main()

int age[4];

int same_age[4];

int i=0;

age[0]=23;

age[1]=34;

age[2]=65;

age[3]=74;

for (;i<4;i++)

same_age[i]=age[i];

for (i=0;i<4;i++)

cout<<same_age[i]<<endl;

return 0;

CS-323 Programming Fundamentals


Scanning array elements using cin

int a[5];

cin>>a[0]; // this will scan the value for the very first location of the array.

cout<<a[0]<<endl;

You can also scan the entire elements of the array using a loop.

Dealing with characters using arrays

You can also store characters and other type data (float etc.) in the arrays. Just
declare it as we’ve done in the case with int. There is no difference in dealing
with characters except you’ve to enclose the value in a single quote.

Char ar[3];

ar[0]=’a’ ; ar[1]=’b’ …..


Example 1: Displaying Array Elements

CS-323 Programming Fundamentals


Output

Here, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we have
printed numbers[i].

Example 2: Take Inputs from User and Store Them in an Array

CS-323 Programming Fundamentals


Output

Once again, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we took an
input from the user and stored it in numbers[i].

Then, we used another for loop to print all the array elements.

Exercise

1. Write a program that asks for the number of hours worked by six employees. It stores the
values in an array.
2. Write a program that asks the user to type 10 integers of an array. The program must
compute and write how many integers are greater than or equal to 10.
3. Write a C++ program to swap first and last element of an integer 1-d array.
4. Write a C++ program to input 5 numbers and find their sum and average.
5. Write a C++ program to reverse the perfect number of an array.
6. Write a C++ program to display the even and odd of N numbers.
7. Write a C++ program to find the largest three elements in an array.

CS-323 Programming Fundamentals


8. Write a C++ program to sort a given unsorted array of integers, in wave form.
Note: An array is in wave form when array[0] >= array[1] <= array[2] >= array[3] <=
array[4] >= . . . .
9. Write a C++ program to rearrange the elements of a given array of integers in zig-zag
fashion way.
Note: The format zig-zag array in form a < b > c < d > e < f.
10. Write a C++ program to rearrange a given sorted array of positive integers.
Note: In final array, first element should be maximum value, second minimum value,
third second maximum value, fourth second minimum value, fifth third maximum and so
on.
11. Write a C++ program to sort (in descending order) an array of distinct elements
according to absolute difference of array elements and with a given value.
12. Write a C++ program to find the two repeating elements in a given array of integers.

CS-323 Programming Fundamentals

You might also like