Object-Oriented Programming
Object-Oriented Programming (COM326)
Lab Week 5
Exercise 1: One dimensional array
Objective here is to manipulate a one-dimensional array. You go from element to
element by incrementing an index variable. Once you know the technique, you can
look/search for a certain element in the array. Applying this technique, you can find the
largest/smallest element in the array/vector.
void main()
{
int i,n;
float a[50],large,small;
cout<< “Size of the vector : ”;
cin<<n;
cout<< “\n”<<n;
cout<< “Enter elements of vector \n”;
for(i=0;i<n;i++)
cin>>a[i];
/* initialize value to small and large element */
large =a[0];
//small = a[0];
/* finding the smallest and largest elements */
for(i=0;i<n;i++)
{
if(a[i]>large)
large =a[i];
/*… For smallest element
if(a[i]<small)
small =a[i];
...*/
}
cout<< “Largest element of the vector is = \n” <<large;
//cout<< “Smallest element of the vector is = \n”<<small;
getch();
}
Find the largest and smallest element of the vector from this program.
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
Exercise 2: Two –dimensional arrays
Objective here is to learn the technique to initialize and display a twodimensional array.
void main()
{
int i,j, matrix[3][3];
cout<< “Enter values of the elements of the matrix \n”;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>matrix[i][j];
/* display the elements */
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
// You can put any conditions here we learnt in week 2, 3 & 4
// e.g. display upper/lower or diagonal elements
cout<< matrix[i][j];
}
cout<< “\n”;
}
}
A matrix (2-D array) A[3x3] is given below
1 2 3
1 4 7
A 4 5 6 B 2 5 8
7 8 9
3 6 9
Matrix A is to be transposed and stored into matrix B. Display the matrix B.
Note you will transpose the matrix without re-assigning the matrix. Find the trick!
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
Exercise 3: Pointers (Exercise moved to Week 6)
Objective here is to learn the technique to search an element in an array using pointer
technique. The elements of an array x={4,2,6,7,5,1,3} is stored in memory locations as
follows. ptr is a pointer pointing to the first element of the array. Find the smallest
element using pointer.
4
ptr
2
6
7
5
void main()
{
int small, i;
int *ptr;
int x[5];
cout<< “Enter elements of array ::\n”;
for(t=0;t<5;t++)
cin>>x[t];
ptr= x;
/* ptr =&x[0] */
small =*ptr;
ptr++;
/*The loop can be done in two ways:
for(i=0;i<5;i++) or
ptr=&x[0];
for( ;ptr>=&x[4];ptr++)
{……..} */
for(i=1;i<6;i++)
{
if(small>*ptr)
small= *ptr;
ptr++;
}
cout<< “The smallest number is ” << small;
getch();
}
Test your program for both types of loops.
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
Exercise 4: This exercise is for super-mouse group. Given below is a pattern
that looks like character 1. Display the character 1 and then shift the
character 1 one column to the right every iteration.
Tips: Use a function for the character. Call the function with a parameter that will shift
the character to the right.
{0,1,1,1,1,1,0,0,0,0,0,0,0},
{1,1,1,1,1,1,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0,0,0,0,0,0},
Write the programs in Exercise 1 and 2, compile, debug and run the program.
Once the program is running, show it to module coordinator/demonstrator for
records. Save the program in folder on your U drive. You will need the programs
for the lab submission in week 11.
Arrays
int array[] [] = {
{0,0,0,0,1,1,1,0,0,0,1,1,1},
{0,0,0,0,1,1,1,0,0,0,1,1,1},
{0,0,0,0,1,1,1,0,0,1,1,0,0},
{0,0,0,0,0,1,0,0,0,1,1,0,0},
{0,0,0,0,1,1,1,0,1,1,0,0,0},
{0,0,0,0,1,1,1,1,1,0,0,0,0},
{0,0,0,1,1,0,0,1,1,0,0,0,0},
{0,0,1,1,0,0,0,0,1,1,0,0,0},
};
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
DNA/RNA organized as Arrays
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
Human protein arrays
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
Fire works as arrays
http://www.scis.ulster.ac.uk/~siddique
© Copyright 2026 Paperzz