Lab Manual 08-10
Lab Manual 08-10
2D ARRAY
Lab Objectives:
At the end of this lab students will know about
For example:-
Int matrix [7] [7];
When we type the above statement the compiler will generate a 2-D array of a matrix which
consists of 7 rows and 7 columns.
In order to store values in a C++ two dimensional arrays the programmer have to specified the
number of row and the number of column of a matrix. To access each individual location of a
matrix to store the values the user have to provide exact number of row and number of column.
For Example:-
Nested loop is used to enter data in 2-D arrays. It depends upon the programmer which loop he
wants to use it could be While loop or it could be a For loop. The outer loop acts as the number
of rows of a matrix and the inner loop acts as the number of columns of a matrix.
For Example:-
#include<iostream>
using namespace std;
main()
The above portion of a code uses nested For loop to assigns 5 to all the locations of a matrix of
a two dimensional array in C++.
The 2-D arrays which are declared by the keyword int and able to store integer values are called
two dimensional integer arrays. The process of assigning values during declaration is called
3
initialization. These Arrays can be initialized by putting the curly braces around each row
separating by a comma also each element of a matrix should be separated by a comma.
{ 3,6,8 },
{ 5,4,7 },
{ 2,4,7 }
};
As you can see the above array is declared by a keyword int therefore it is a 2-D integer array.
Now consider a following example of a complete program which will elaborate the working.
Explanation:-
In a main function first of all the two dimensional integer array will be declared by the name of
a matrix, then the integer elements will be stored in an array with the help of a nested For loop.
Then again with the help of another nested loop the program will print the elements stored in
the matrix on a computer screen.
Lab Tasks
Question # 1
Write a C++ program which stores names of five cities and print the names of only those cities which
start from K.
Perform the above given task using C style strings.
Question #2
Write a C++ program to find the duplicate values in a 2d array.
Question # 3
Write a C++ program to move all negative elements of an array of integers to the end of the array
without changing the order of positive element and negative element.
Question # 4
Write a C++ Program to store temperature of two different cities for a week and display it. Find the
city with hottest temperature.
Perform the above given task using arrays.
Question # 5
Write a C++ program that read the entries of an array of 10 integers from a user. Compute x as the
average of the 10 entries and then compute the average of those entries that are greater than x. Print this
final average.
Question # 6
Write a C++ code to count the number of a’s in a string
Question # 7
Write a program in C++ to read a sentence and replace lowercase characters by uppercase and vice-
versa
Question # 8
Write a program to add two numbers using function prototype.
Question # 9
Write a C++ program to remove characters in String except Alphabets
Question # 10
Write a C++ program to find sum of elements in a given array
Question # 11
Write a C++ program to declare a structure name book having members as book_name and book_price,
Ask user to enter record of 2 books and print the record of the book having greater price
Question # 12
Add two complex numbers using structure.
Question # 13
Write a C++ program to traverse an array using pointers.