Arrays (1D, 2D, 3D)
Arrays (1D, 2D, 3D)
Lab- 03 Manual
Lab Instructor: Riaz Ahmad
Department of Computer Science
Email: [email protected]
Lab 03: Arrays
1. Objective:
Learn how to declare, initialize and use one-dimensional arrays.
2. Scope:
The student should know the following:
1. Syntax of array declaration
2. assigning and processing and elements
3. Useful Concepts:
An array is a collection of two or more adjacent memory cells, called array elements that
are associated with a particular symbolic name. Arrays are very useful construct to store related
values together instead of declaring several variables for each value.
To set up an array in memory, we must declare both the name of the array and the
number of cells associated with it.
The following declaration will instruct the compiler to allocate eight memory cells with the
name x; these memory cells will be adjacent to each other. Each element may contain a value of
type double.
double x[8];
x
0 1 2 3 4 5 6 7
As you can see; the name of the array x points to the first element of the array. We can define a
constant for array size and use it whenever we declare an array.
To access the data stored in an array, we reference each individual element by specifying the
array name and identifying the element desired.
x[0] the value of the first element
x[3] the value of the fourth element
x[7] the value of the eighth element
NOTE: The indices of the elements start from zero, not from one.
Array initialization:
We can initialize an array directly by specifying each cell value individually as follows:
double values [] = {12.5,17.9,23.5,-2.5,115.75,-55.3};
Hence here we do not need to specify the size of the array or how many elements this array
should have. This number can be deduced from the initialization list.
We can also use for loop which is the common way to deal with arrays in general. The
following code initializes array square to squares of the indices:
Array Processing:
Elements of the array are dealt as normal variables, the only difference here is to
specify the index desired. These statements are examples of using array elements:
i = 5;
cout<< x[i+1]; display 14.0 (value of x[6])
x[i-1] = x[i]; assigns 12.0 (value of x[5]) to x[4]
cout<<x[i++]; display 12.0 (value of x[5])
cout<<x[2*i]; Invalid index. Attempt to display x[10]
4. Examples
Example 1: Write a program that contains an array of five elements, take five values from user,
assign the value to each element of array and display the all array elements.
#include<iostream>
using namespace std;
int main()
{
int n[5]; // n is an array of 5 integers int i;
int i;
for (i = 0; i < 5; i++ )
{
cout<<"Please enter the element number "<<i+1<<": ";
cin>>n[i];
}
cout<<endl;
// output each array element's value
for (i = 0; i < 5; i++ )
{
cout<<"Element stored at index "<<i<< " is: "<<[i]<<endl;
}
return 0;
The output of the program is:
Example 3: Suppose there are two arrays of the same size. Array A consists of five elements
while array B in not initialized yet. Write a program that copies the values of A into B. Verify
your code by displaying the contents of B.
#include<iostream>
using namespace std;
int main()
{
int A[] = {99,89,79,69,59};
int B[5], i;
for (i = 0; i < 5; i++)
{
B[i] = A[i];
cout<<"A["<<i<<"] = " <<A[i]<<", ";
cout<<"B["<<i<<"] = " <<B[i]<<", "<<endl;
}
return 0;
}
Example 4: Write a program that takes an integer value from user and search it in array, If
#include <iostream>
using namespace std;
int main(){
int input[100], count, i, num;
cout << "Enter Number of Elements in Array\n";
cin >> count;
cout << "Enter " << count << " numbers \n";
// Read array elements
for(i = 0; i < count; i++)
{
cin >> input[i];
}
cout << "Enter a number to serach in Array\n";
cin >> num;
// search num in inputArray from index 0 to elementCount-1
for(i = 0; i < count; i++)
{
if(input[i] == num)
{
cout << "Element found at index " << i+1;
break;
}
}
if(i == count)
{
cout << "Element Not Present in Input Array\n";
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int i, n;
float arr[100];
cout << "Enter total number of elements(1 to 100): ";
cin >> n;
cout << endl;
// Store number entered by the user
for(i = 0; i < n; ++i) {
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}
// Loop to store largest number to arr[0]
for(i = 1;i < n; ++i)
{
// Change < to > if you want to find the smallest element
if(arr[0] < arr[i])
arr[0] = arr[i];
}
cout << endl << "Largest element = " << arr[0];
return 0;
}
The output of the program is:
Write a program that declares an array of 5 double numbers. Use a loop to read 5 real
numbers from user and fill the array. Then print the following on screen:
Exercises 2:
64 36 56 47 40 54 61 60 58 64 54
48 59 45 63 54 50 49 51 60 58 59
Initialize an array with above grades and find the following things about the above data.
1. Objective:
Learn how to declare, assign and manipulate two dimensions array.
2. Scope:
The student should know the following:
1. What is 2-D array?
3. Useful Concepts:
or
b[0][0] = 51; b[0][1] = 52; b[0][2] = 53;
b[1][0] = 54; b[1][1] = 55; b[1][2] = 56;
or
int b[][3] = {{51, 52, 53}, {54, 55, 56}};
The output of two-dimensional arrays should be in the form of rows and columns for
readability. Nested for loops are used to print the rows and columns in row and column
order.
By increasing the index value of the array the elements stored at that index value are
printed on the output screen.
4. Examples
Example 11.1: A program to input elements in a two dimensional array and print it.
#i#include <iostream>
using namespace std;
int main()
{
int a[3][3]; int i,j;
cout<<"enter nine elements in the array:";
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
Example 11.2: Write a program that will add two matrixes entered by the user
and print it.
#include <iostream>
using namespace std;
int main()
{
int a[3][3],b[3][3],c[3][3];
int i,j;
cout<<"enter nine elements of a 3X3 Matrix A:";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>a[i][j];
}
cout<<"enter nine elements of a 3X3 Matrix B:";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>b[i][j];
}
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
#include <iostream>
using namespace std;
int main()
{
int a[3][3];
int i,j;
#include <iostream>
using namespace std;
int main() {
return 0;
}
int main( )
int Array[2][2][4];
cout<<"\n Below are the values you have stored in the array"<< endl;
cout << "[" << i << "][" << j << "][" << k << "] =" <<Array[i][j][k] << endl;
6. Home Work
1. A program to input a matrix and find its inverse (using function).
2. A program to input a matrix and find its singular or non-singular (using function).