You are on page 1of 12

LAB 10 – ARRAYS-I

Objectives
1. Declaring and initializing one-dimensional arrays.
2. Inserting values into and printing values out of one-dimensional arrays.
Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.

To declare an array, define the variable type, specify the name of the array followed
by square brackets and specify the number of elements it should store:

Syntax:

//declaration without initialization

string cars[4];
char ch_arr[5];
int myNum[3];
float myNum2[3];

//Initialization at time of declaration


string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
char ch_arr[5]={'A','a','b','c','d'};
int myNum[3] = {10, 20, 30};
float myNum2[3] = {10.1, 20.2, 30.3};

//output specific location of array

cout << cars[0];//will display value at 0 index

cout << ch_arr[1];//will display value at 1 index

cout << myNum [2]; //will display value at 2 index

cout << myNum2 [1]; //will display value at 3 index

Remember that the first location / index of array starts from 0


e,g, if you declare an array of size 10 the valid index values for
the array would be from 0 to 9.

Exercice # 10.1
Write a C++ program to enter and display student attendance records using array as
discussed in uploaded lecture.

Exercise # 10.2
Write a C++ program, which;

CSC 102L Programming Fundamentals Labs Page 1 of 110


 Declares and initializes an integer array of 20 indexes
 Finds the minimum, maximum, sum and average of the elements of this array
using functions and,
 Prints these results on screen
Your program should have the following interface.

The integer array is :


90, 87, 23, 3, 8, 34, 67, 75, 45, 90,
1, 2, 3, 4, 34, 44, 4, 78, 56, 12
The minimum value is 1 found at index [10]
The maximum value is 90 found at index [0] [9]
The sum is 760
The average is 38

Web Resources

http://www.cprogramming.com/

CSC 102L Programming Fundamentals Labs Page 2 of 110


LAB 11 – 2-DIMENSIONAL ARRAYS-I

Objectives
1. Declaring and initializing two-dimensional arrays.
2. Inserting values into and reading values out of two-dimensional arrays.
3. Traversing through two-dimensional arrays.

The simplest form of the multidimensional array is the two-dimensional array.


Syntax:
datatype arrayName [ x ][ y ];
A two-dimensional array can be think as a table, which will have x number of rows and
y number of columns. A 2-dimensional array a, which contains three rows and four
columns can be shown as below −

Thus, every element in array a is identified by an element name of the form a[ i ][ j ],


where a is the name of the array, and i and j are the subscripts that uniquely identify
each element in a.
Initializing Two-Dimensional Arrays
Multidimensioned arrays may be initialized by specifying bracketed values for each
row. Following is an array with 3 rows and each row have 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are optional. The following
initialization is equivalent to previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Accessing Two-Dimensional Array Elements
An element in 2-dimensional array is accessed by using the subscripts, i.e., row index
and column index of the array. For example −
int val = a[2][3];
cin>>a[2][3];
cout<<a[2][3];//it will display 11

CSC 102L Programming Fundamentals Labs Page 3 of 110


The above statement will take 4th element from the 3rd row of the array. You can verify
it in the above digram.

Same is the case with other datatypes like string, float and double.

2D char array doesn’t work in the same fashion i.e. if you declare a char array char
ch_Arr[3][3], it means it can have 3 location and each location can store maximum 3
characters.

Example discussed in Lectures


#include "iostream"

using namespace std;


void search(int i_ar[][3], int v);

int main()
{
//display matrix index
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 4; j++ )
{
cout << "[" << i << "][" << j << "]\t";
}
cout<<endl;
}

string ch_Arr [3][3] = {


{"r0c0","r0c1","r0c2"},{"r1c0","r1c1","r1c2"},
{"r2c0","r2c1","r2c2"}
};

ch_Arr[2][2]="Row2Col2";
//input all values of ch_Arr from user
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 3; j++ )
{

cout << "ch_Arr[" << i << "][" << j << "]: ";
cin>>ch_Arr[i][j];
}
cout<<endl;
}
//display matrix/stringarray(2D) i.e. ch_Arr
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 3; j++ )
{

cout << "ch_Arr1[" << i << "][" << j << "]: ";
cout << ch_Arr[i][j]<<"\t";
}
cout<<endl;
CSC 102L Programming Fundamentals Labs Page 4 of 110
}
//declaration of 2D int array
int a[3][3] = { {0,1,2}, {3,4,5}, {6,7,8}};

// output each array element's value


for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 3; j++ )
{

cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<<"\t";
}
cout<<endl;
}

search(a,8); //searching from 3x3 array

return 0;
}///

void search(int i_ar[][3], int v)


{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i_ar[i][j]==v)
{
cout<<"index of "<<v<<" is [" <<i<<"]
["<<j<<"]";
}
}
}
}

Exercise # 11.1
Write a C++ program which declares two 3 x 3int arrays (Matrices). Store random values
using nested for loops in array and perform Addition and Multiplication of Matrices,
stores the result of operation in another Matrix (output arrays). And displays the results.

Web Resources

http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/
http://projectsyapa.com/cpp

CSC 102L Programming Fundamentals Labs Page 5 of 110


LAB 12 – 2-DIMENSIONAL ARRAYS-II

Objectives
1. Traversing through two-dimensional arrays.
2. Passing two-dimensional arrays to functions.
Exercise 12.1 Searching through Two-Dimensional Arrays
Write a C++ program, which
 Populates a two-dimensional array of size 10 x 10 with random numbers between 0
and 9 (Hint: C++ has a built-in function for random number generation)
 Prompts the user to enter a number
 Searches for this number in the 10 x 10 array and
 Displays location where the number is found in the array
Your program should display ‘Value not found’ if the user’s specified number is not found in
the 10 x 10 array. Your program should have the following interface.

Enter a value to find in the 10 x 10 array :


5
Found at location [0][5]
Found at location [1][9]
 In-Lab task # 11.5
Found at location [4][6]
Found at location [7][0]
4 instances found.

The 10 x 10 array is :
8 9 6 4 6 5 7 8 1 2
4 6 3 1 1 2 8 7 4 5
6 7 8 3 4 2 3 2 3 1
2 3 2 1 4 0 6 7 8 4
0 1 9 4 7 6 5 3 2 3
2 1 2 1 3 4 3 7 8 6
6 7 8 1 2 1 3 2 4 1
5 1 2 1 1 1 3 4 7 6
7 8 7 6 4 4 3 1 2 3
4 3 2 1 4 7 8 3 0 1

Exercise 12.2 Traversing through Two-Dimensional Arrays


Write C++ program such that the program searches through the array to find maximum and
minimum values and displays them with their respective location numbers. Your program
should have the following . Use the same array you declared in 12.1

CSC 102L Programming Fundamentals Labs Page 6 of 110


Minimum Value is : 0
Found at location [3][5]
Found at location [4][0]
Found at location [9][8]
3 instances found.

Maximum Value is : 9
Found at location [0][1]
Found at location [4][2]
2 instances found.

The 10 x 10 array is :
8 9 6 4 6 5 7 8 1 2
4 6 3 1 1 2 8 7 4 5
6 7 8 3 4 2 3 2 3 1
2 3 2 1 4 0 6 7 8 4
0 1 9 4 7 6 5 3 2 3
2 1 2 1 3 4 3 7 8 6
6 7 8 1 2 1 3 2 4 1
5 1 2 1 1 1 3 4 7 6
7 8 7 6 4 4 3 1 2 3
4 3 2 1 4 7 8 3 0 1

SOURCE CODE OF SORTING DISCUSSED IN LECTURES – COPY IN DEV C++ AND


TRY TO UNDERSTAND AS EXPLAINED IN UPLOADED LECTURE

#include <iostream>

#include <stdlib.h> /* srand, rand */

#include<string.h>

using namespace std;

int main()

{
CSC 102L Programming Fundamentals Labs Page 7 of 110
//integer Arrays sorting

//array declaration

int arr[10];

int i,j;

int temp;

//read n elements

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

//cout<<"Enter element at index ["<<i<<"] ";

arr[i]=rand() % 100;

//cin>>arr[i];

//print input elements

cout<<"Unsorted Array elements:"<<endl;

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

cout<<arr[i]<<"\t";

cout<<endl;

//sorting - ASCENDING ORDER

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

for(j=i+1;j<10;j++)

CSC 102L Programming Fundamentals Labs Page 8 of 110


if(arr[i]>arr[j])//12,50,33

temp =arr[i];//50

arr[i]=arr[j];//12

arr[j]=temp;//50

//print sorted array elements

cout<<"Sorted (Ascending Order) Array elements:"<<endl;

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

cout<<arr[i]<<"\t";

cout<<endl;

//String Arrays

string st_name[3] = {"Bilal","Zubair","Ali"};

string stemp;

//sorting - ASCENDING ORDER

cout<<"\n\nUnSorted (Ascending Order) Array elements:"<<endl;

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

cout<<st_name[i]<<"\t";

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

CSC 102L Programming Fundamentals Labs Page 9 of 110


{

for(j=i+1;j<3;j++)

if(st_name[i]>st_name[j])

stemp =st_name[i];

st_name[i]=st_name[j];

st_name[j]=stemp;

cout<<"\nSorted (Ascending Order) Array elements:"<<endl;

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

cout<<st_name[i]<<"\t";

//CHAR ARRAYS

//strcpy( destination, source ); copy from destinition to source

//strcmp (str1, str2 );Compares str1 to str2

//Returns an integral value indicating the relationship between the strings:

// return value indicates

// <0 it will return less than 0 if first char of str1 is greater


than first char of str2

// 0 the contents of both strings are equal

// >0 it will return greater than 0 if first char of value 1 is


less than first char of valu 2
CSC 102L Programming Fundamentals Labs Page 10 of 110
char chst_name [3][20];

char t[20];//temp variariable

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

cout<<"Enter Value in char array ";

cin>>chst_name[i];

cout<<"UNSorted (Ascending Order) Array elements:"<<endl;

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

cout<<chst_name[i]<<"\t";

cout<<endl;

//sorting algo

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

for(j=0; j<3; j++)

if(strcmp(chst_name[i], chst_name[j])<0)//it will return less than 0 if first


char of value 1 is greater than first char of valu 2

strcpy(t, chst_name[i]); //strcpy( destination, source )

strcpy(chst_name[i], chst_name[j]);

strcpy(chst_name[j], t);

CSC 102L Programming Fundamentals Labs Page 11 of 110


}

//print sorted array elements in char array

cout<<"Sorted (Ascending Order) Array elements:"<<endl;

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

cout<<chst_name[i]<<"\t";

cout<<endl;

return 0;

Web Resources

http://en.wikipedia.org/wiki/Tic-tac-toe
http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/

CSC 102L Programming Fundamentals Labs Page 12 of 110

You might also like