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

Computer Programming 09

Uploaded by

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

Computer Programming 09

Uploaded by

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

Department of Biomedical Engineering Technology

The Superior University, Lahore

Computer Programming

Experiment No.9
Introduction to Arrays

Prepared for:
Mam Rania Muqadas
By:
Name: Haider Ali
ID: SU91-BBETM-F23-014
Section: Fall
Semester: 2nd

Total Marks: ______10_________


Obtained Marks: ______________
Signature: __________________
Date: __________________
Computer Programming Lab 9

Experiment No.9
Introduction to Arrays
(Rubrics)

Name: Haider Ali ID: 014

A. PSYCHOMOTOR
Serial Criteria Allocated Unacceptable Poor Fair Good Excellent Total
No. Marks 0% 25% 50% 75% 100% Obtained

1 Syntax 1 0 0.25 0.5 0.75 1


Handling

2 Program 2 0 0.5 1 1.5 2


Coding Skills

3 Accuracy in 3 0 0.75 1.5 2.25 3


Output
Results
Sub-Total 6 Sub Total Marks Obtained in Psychomotor (P)

B. AFFECTIVE
Serial Criteria Allocated Unacceptable Poor Fair Good Excellent Total
No. Marks 0% 25% 50% 75% 100% Obtained
1 Lab Report 2 0 0.5 1 1.5 2

2 Assigned 2 0 0.5 1 1.5 2


Task
Sub-Total 4 Sub Total Marks Obtained in Affective (A)

Instructor Name: ______________ Total Marks (P+A): ______________

Instructor Signature: ______________ Obtained Marks (P+A): ______________

2
Computer Programming Lab 9

CONTENTS OF LAB 9
1 Objectives ............................................................................................................................................. 4
2 Arrays in C++ ....................................................................................................................................... 4
2.1 Declaration of Arrays .................................................................................................................... 4
2.2 Initializing Arrays ........................................................................................................................... 4
2.2.1 Example 2 .............................................................................................................................. 5
2.3 Accessing Array Elements ............................................................................................................. 5
2.3.1 Few Things to Remember: .................................................................................................... 6
2.4 Array Initialization ......................................................................................................................... 6
2.4.1 Example 3 .............................................................................................................................. 6
2.5 Array With Empty Members ......................................................................................................... 6
2.5.1 Example 4 .............................................................................................................................. 6
2.6 Inserting and Printing Array Elements .......................................................................................... 7
2.6.1 Example 5 .............................................................................................................................. 7
2.7 Displaying Array Elements ............................................................................................................ 7
2.7.1 Example 6 .............................................................................................................................. 7
2.8 Take Inputs From User To Store Elements in Array and Printing the Stored Values .................... 8
2.8.1 Example 7 .............................................................................................................................. 8
2.8.2 Example 8 .............................................................................................................................. 9
3 Lab Tasks ............................................................................................................................................ 10
3.1 Task 1 .......................................................................................................................................... 10
3.2 Task 2 .......................................................................................................................................... 10
3.3 Task 3 .......................................................................................................................................... 10
3.4 Task 4 .......................................................................................................................................... 10
4 Home Assignment:.............................................................................................................................. 11
4.1 Task 1 .......................................................................................................................................... 11
4.2 Task 2 .......................................................................................................................................... 11

3
Computer Programming Lab 9

Introduction to Arrays

1 OBJECTIVES
• To get a basic understanding of arrays in C++.
• To understand the basics of 1D arrays.
• To get hands-on experience working with 1D arrays.
• To declare, initialize, and access array elements in C++ programming with the help of examples.

2 ARRAYS IN C++
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is often more useful to think of an array as
a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one
array variable such as numbers and use numbers [0], numbers [1], and ..., numbers [99] to represent
individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element
and the highest address to the last element.

2.1 DECLARATION OF ARRAYS


To declare an array in C++, the programmer specifies the type of the elements and the number of
elements required by an array as follows −

dataType arrayName [arraySize];

This is called a single-dimension array. The arraySize must be an integer constant greater than zero and
type can be any valid C++ data type.

Example 1

An example of declaration of an array is given as follows:

int x [6];

Here,

• int - type of element to be stored


• x - name of the array
• 6 - size of the array

2.2 INITIALIZING ARRAYS


You can initialize C++ array elements either one by one or using a single statement as follows −

4
Computer Programming Lab 9
double balance [5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

The number of values between braces { } cannot be larger than the number of elements that we declare
for the array between square brackets [ ].

2.2.1 Example 2
Following is an example to assign a single element of the array:

If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if
you write:

double balance [] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create exactly the same array as you did in the previous example.

balance [4] = 50.0;

The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will
be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called
base index. Following is the pictorial representation of the same array we discussed above −

Figure 1: Example of Array

2.3 ACCESSING ARRAY ELEMENTS


An element is accessed by indexing the array name. This is done by placing the index of the element
within square brackets after the name of the 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.

Figure 2: Elements of Array in C++

5
Computer Programming Lab 9
2.3.1 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 above example, x[5] is the
last element.
• Elements of an array have consecutive addresses. For example, suppose the starting address of
x[0] is 2120d. Then, the address of the next element x[1] will be 2124d, the address of x[2] will be
2128d and so on.

Here, the size of each element is increased by 4. This is because the size of int is 4 bytes.

2.4 ARRAY INITIALIZATION


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

2.4.1 Example 3
For example,

// declare and initialize and array

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

Figure 3: Array Elements and Their Data

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.

2.5 ARRAY WITH EMPTY MEMBERS


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

2.5.1 Example 4
For example,

// store only 3 elements in the array

6
Computer Programming Lab 9
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.

Figure 4: Empty Array Elements Are Automatically Assigned 0

2.6 INSERTING AND PRINTING ARRAY ELEMENTS


The example of inserting and printing elements in an array is as follows:

2.6.1 Example 5
The code and output is given as follows:

Code
int mark[5] = {19, 10, 8, 17, 9}

// change 4th element to 9


mark[3] = 9;
// take input from the user
// store the value at third position
cin >> mark[2];

// print first element of the array


cout << mark[0];

2.7 DISPLAYING ARRAY ELEMENTS


The example of displaying array elements is shown below:

2.7.1 Example 6
Code Output
#include <iostream> Value Stored at Index 0 is :1
using namespace std; Value Stored at Index 1 is :2
Value Stored at Index 2 is :3
int main() { Value Stored at Index 3 is :4
int n[5]; //Declaring Array Value Stored at Index 4 is :5

7
Computer Programming Lab 9

//Storing Array Elements


n[0]=1;
n[1]=2;
n[2]=3;
n[3]=4;
n[4]=5;

// Printing array elements


cout<<”Value Stored at Index 0 is :”<<n[0]<<endl;
cout<<”Value Stored at Index 1 is :”<<n[1]<<endl;
cout<<”Value Stored at Index 2 is :”<<n[2]<<endl;
cout<<” Value Stored at Index 3 is :”<<n[3]<<endl;
cout<<”Value Stored at Index 4 is :”<<n[4]<<endl;

return 0;
}

2.8 TAKE INPUTS FROM USER TO STORE ELEMENTS IN ARRAY AND PRINTING THE STORED VALUES
The given example uses for-loop to take input from user.

2.8.1 Example 7
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.

Code Output
#include <iostream> Enter 5 numbers:
using namespace std; 11
12
int main() { 13
int numbers[5]; 14
15
cout << "Enter 5 numbers: " << endl; The numbers are: 11 12 13 14 15

// store input from user to array


for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}

cout << "The numbers are: ";


8
Computer Programming Lab 9

// print array elements


for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}

return 0;
}

2.8.2 Example 8
Following program is displaying and printing one dimensional array.

Code Output
#include<iostream> How Many Elements You Want to Store into
using namespace std; an Array?
int main() 6
{ Enter 6 Elements to Store into an Array
int arr[50], num, i; 1
cout<<"\n How Many Elements You Want to Store into an 2
Array? \n"; 3
cin>>num; 4
cout<<"\n Enter "<<num<<" Elements to Store into an 5
Array : \n"; The Elements in the Array are:
for(i=0; i<num; i++) 1 2 3 4 5 6
{
cin>>arr[i];
}
cout<<"\n The Elements in the Array are : \n";
for(i=0; i<num; i++)
{
cout<<arr[i]<<"\t";
}
return 0;
}

9
Computer Programming Lab 9

3 LAB TASKS

3.1 TASK 1
Write a program to check the largest value stored in the given array:

int a [7] = {23,1,13,4,34,67,15}


Also, write a program to check for the smallest value.
You can also take input from the user consisting of 7 elements.

3.2 TASK 2
Write a program to calculate the sum and product of values stored in an array. You have to take input
from the user, then display the stored values, and finally print the sum and product.

3.3 TASK 3
Write a program to calculate the average of the marks using an array. The program should display the
name of the subject before storing the value. The size of the array must be based on five subjects i.e.
English, Programming, Calculus, Computing, and Electronics.

3.4 TASK 4
Write a program to calculate the arithmetic mean of 5 numbers stored in an array.
Note: Arithmetic mean is the sum of a collection of numbers divided by the number of numbers in the collection.

10
Computer Programming Lab 9

4 HOME ASSIGNMENT:

4.1 TASK 1
Writ a program to count even and odd numbers in an array.

4.2 TASK 2
Write a C++ program to reverse an array.

11

You might also like