0% found this document useful (0 votes)
17 views4 pages

Lab Manual 08-10

This lab manual provides an introduction to using two-dimensional arrays in C++ programming, including syntax, data entry methods using nested loops, and initialization examples. It includes sample code snippets for declaring and accessing arrays, as well as a series of lab tasks that require students to implement various C++ programs involving 2D arrays and other concepts. The tasks cover a range of topics such as string manipulation, temperature storage, and structure usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Lab Manual 08-10

This lab manual provides an introduction to using two-dimensional arrays in C++ programming, including syntax, data entry methods using nested loops, and initialization examples. It includes sample code snippets for declaring and accessing arrays, as well as a series of lab tasks that require students to implement various C++ programs involving 2D arrays and other concepts. The tasks cover a range of topics such as string manipulation, temperature storage, and structure usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1

LAB MANUAL 8-10

2D ARRAY

Lab Objectives:
At the end of this lab students will know about

 How to use 2d arrays in C++ programs

SYNTAX OF TWO-DIMENSIONAL ARRAY:-

(Data type) (Name of array) [Number of rows] [Number of columns];

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.

ACCESSING EACH LOCATION OF TWO DIMENSIONAL ARRAYS:-

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:-

int matrix [2][2];


matrix [0] [0] = 43;
matrix [0] [1] = 44;
matrix [1] [0] = 56;
matrix [1] [1] = 72;
2

HOW TO ENTER DATA IN A TWO DIMENSIONAL ARRAYS:-

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()

int matrix [5] [5];


for (int m1=0 ; m1<5 ; m1++)
{
for (int m2=0 ; m2<5 ; m2++)
{
matrix [m1] [m2] = 5 ;
}
}
getch();
}

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++.

HOW TO INITIALIZE TWO-DIMENSIONAL INTEGER ARRAY:-

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.

EXAMPLE OF A TWO DIMENSIONAL INTEGER ARRAY:-

int mat [3][3]= {

{ 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.

#include<iostream> // For displaying elements of


//a matrix on a screen //
using namespace std;
for (int m1=0 ; m1<2 ; m1++)
main()
{
{
for (int m2=0 ; m2<3 ; m2++)
int matrix [2][3];
cout<<"Your Entered Integer
// For taking integer inputs are :";
//in a matrix //
cout<<matrix [m1][m2];
for (int m1=0 ; m1<2 ; m1++)
cout<<endl;
{
}}
for (int m2=0 ; m2<3 ; m2++)
{ cout<<"Enter Integer :";
cin>>matrix [m1][m2];
cout<<endl;}}
4

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.

You might also like