0% found this document useful (0 votes)
4 views5 pages

Assignment

The document contains programming tasks focused on 2D arrays in C++. It includes examples of declaring and initializing 2D arrays, finding the largest number in a matrix, counting even and odd numbers, and searching for a specific element within a matrix. Each task is accompanied by code snippets demonstrating the required functionality.

Uploaded by

a84480136
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)
4 views5 pages

Assignment

The document contains programming tasks focused on 2D arrays in C++. It includes examples of declaring and initializing 2D arrays, finding the largest number in a matrix, counting even and odd numbers, and searching for a specific element within a matrix. Each task is accompanied by code snippets demonstrating the required functionality.

Uploaded by

a84480136
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

Programming Fundamentals

Lab Task # 10

Submitted by:
M. Ashir Azeem
70186379

Submitted to:
Sir Babar

Department of Software Engineering


1. Declare and initialize a 2D arrays with different methods.

#include<iostream>
using namespace std;
int main(){
// DIRECT INITIALIZATION
int array[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int i,j;
int arr[2][3];

cout<<"Here is a 3 by 3 Matrix"<<endl;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
cout<<array[i][j]<<" ";
}
cout<<endl;
}
//USER INPUT INITIALIZATION

cout<<"Enter a 2 by 3 Matrix"<<endl;
for(i=0; i<2; i++){
for(j=0; j<3; j++){
cin>>arr[i][j];
}
cout<<endl;
}
for(i=0; i<2; i++){
for(j=0; j<3; j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;}
}
2. Write a program to input a 3×3 matrix and find the largest number present
in the matrix.

#include<iostream>
using namespace std;
int main(){
int i,j,large;
int arr[2][3];
// INITIALIZATION
cout<<"Enter a 2 by 3 Matrix"<<endl;
for(i=0; i<2; i++){
for(j=0; j<3; j++){
cin>>arr[i][j];
}
cout<<endl;
}
// ARRAY PRINTING
for(i=0; i<2; i++){
for(j=0; j<3; j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
// LARGE CHECK
large=arr[0][0];
for(i=0; i<2; i++){
for(j=0; j<3; j++){
if(arr[i][j]>large ){
large=arr[i][j];
}
}
}
cout<<large<<"is the largest nuber in 2D array"<<endl;}
3. Write a program to input a 2D array and count total even and odd
numbers separately.

#include<iostream>
using namespace std;
int main(){
int i,j,even=0,odd=0;
int arr[4][5];

// INITIALIZATION
cout<<"Enter a 4 by 5 Matrix"<<endl;
for(i=0; i<4; i++){
for(j=0; j<5; j++){
cin>>arr[i][j];
}
cout<<endl;}
for(i=0; i<4; i++){
for(j=0; j<5; j++){
if(arr[i][j]%2==0){
even++;
}else
odd++;

}
}
cout<<"Even numbers in matrix are: "<<even<<endl;
cout<<"Odd numbers in matrix are: "<<odd<<endl;
}
4. Write a program to input a matrix and search for a specific element
entered by the user. Display its row and column position if found.

#include<iostream>
using namespace std;
int main(){
int i,j,num;
int arr[2][3];
// INITIALIZATION
cout<<"Enter a 2 by 3 Matrix"<<endl;
for(i=0; i<2; i++){
for(j=0; j<3; j++){
cin>>arr[i][j];
}

cout<<"Enter a Number"<<endl;
cin>>num;
bool notpresent=1;
for(i=0; i<2; i++){
for(j=0; j<3; j++){
if(arr[i][j]==num) {
notpresent=0;
cout<<"Number found at position ROW# "<<i+1<<"and
COLUMN#"<<j+1<<endl;
break;}
}
}
if(notpresent=1);{
cout<<"Number not found"<<endl;
}
}

You might also like