CP LAB #06 Assignment: Course: BS (CS) Class: 1-A .: 02-134212-041

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CSL-113: Computer Programming Lab

Name : jawad ahmed khan


Semester : Spring-2021.bscs
Lab Engineer : Tooba zahid

CP LAB #06 Assignment

Name: Jawad Ahmed Khan


Course: BS (CS)
Class: 1-A
Enrollment no. : 02-134212-041
CSL-113: Computer Programming Lab

Name : jawadahmed khan


Semester : Spring-2021.bscs
Lab Engineer : TOOBA ZAHID

LAB TASK#1:

Write a program to create a 2D array of size 3x3. The program takes input
for each cell in the array and then calculates and displays the sum of each
row.

For example:
123
456
789
 Sum of first row is 6
 Sum of second row is 15
 Sum of third row would is 24

PROGRAM:
#include <cstdlib>

#include <iostream>

#define size 100


CSL-113: Computer Programming Lab

Name : jawad ahmed khan


using namespace std; Semester : Spring-2021.bscs
Lab Engineer : Tooba zahid

//Our function to find and display the sum of each row

void rowSum(int arr[size][size] ,int m,int n){

int sum;

//finding column sum

for(int i = 0; i < n; i++){

sum = 0;

for(int j = 0; j < m; j++){

sum = sum + arr[i][j]; //adding each element of row

cout<<"sum of "<<"row "<<i+1<<" is: " <<sum<<endl;

}
CSL-113: Computer Programming Lab

Name : jawadahmed khan


int main(int argc, char** argv) { Semester : Spring-2021.bscs
Lab Engineer : TOOBA ZAHID

int arr[size][size], m ,n;

cout<<"Enter the size of row: ";

cin>>m;

cout<<"\nEnter the size of column: ";

cin>>n;

cout<<"\nInput the matrix: "<<endl;

//initializing the matrix

for(int i = 0; i < m; i++)

for(int j=0;j<n;j++)

cin>>arr[i][j];

}
CSL-113: Computer Programming Lab

Name : jawad ahmed khan


} Semester : Spring-2021.bscs
Lab Engineer : Tooba zahid

cout<<"\nSum of each row: \n"<<endl;

rowSum(arr, m, n);

return 0;

OUTPUT:
CSL-113: Computer Programming Lab

Name : jawadahmed khan


Semester : Spring-2021.bscs
Lab Engineer : TOOBA ZAHID

LAB TASK#2::

Write a program that takes a 3x3 matrix as input and asks for
a number entered and prints out its position in the matrix. It
displays not found if the number is not in the matrix.

For example:
123
456
789
Input Example 1:
Enter a number to find: 4
Found! Position: [1] [0]

Input Example 2:
Enter a number to find: 0
Not Found
CSL-113: Computer Programming Lab

Name : jawad ahmed khan


Semester : Spring-2021.bscs
Lab Engineer : Tooba zahid

PROGRAM:

#include <iostream>
using namespace std;
main() {
int narr[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "enter numbers for matrix: ";
cin >> narr[i][j];
}
}
bool foind = false;
int num;
cout << "enter number you want to search: ";
cin >> num;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (num == narr[i][j])
{

cout << "character" << num << " is found at position " << i <<
"x" << j << " in the matrix" << endl;
CSL-113: Computer Programming Lab

Name : jawadahmed khan


foind = true; Semester : Spring-2021.bscs
} Lab Engineer : TOOBA ZAHID

if (foind == false)
{

cout << "the number you want to search is not found in the
matrix " << endl;

system("pause");

}
CSL-113: Computer Programming Lab

Name : jawad ahmed khan


Semester : Spring-2021.bscs
Lab Engineer : Tooba zahid

OUTPUT:

IF NUMBER IS NOT PRESENT IN MATRIX


CSL-113: Computer Programming Lab

Name : jawadahmed khan


Semester : Spring-2021.bscs
Lab Engineer : TOOBA ZAHID
CSL-113: Computer Programming Lab

Name : jawad ahmed khan


Semester : Spring-2021.bscs
Lab Engineer : Tooba zahid

LAB TASK#3:

Write a program which calculates the transpose of a


3x3 matrix.

Example:

123
456
789
Transpose:
147
258
369

PROGRAM:

#include <iostream>

using namespace std;


CSL-113: Computer Programming Lab

Name : jawadahmed khan


Semester : Spring-2021.bscs
Lab Engineer : TOOBA ZAHID
int main() {

int a[10][10], transpose[10][10], row, column, i, j;

cout << "Enter rows and columns of matrix: ";

cin >> row >> column;

cout << "\nEnter elements of matrix: " << endl;

// Storing matrix elements

for (int i = 0; i < row; ++i) {

for (int j = 0; j < column; ++j) {

cout << "Enter element a" << i + 1 << j + 1 << ": ";

cin >> a[i][j];

// Printing the a matrix

cout << "\nEntered Matrix: " << endl;

for (int i = 0; i < row; ++i) {


CSL-113: Computer Programming Lab

Name : jawad ahmed khan


for (int j = 0; j < column; Semester : Spring-2021.bscs ++j)
{ Lab Engineer : Tooba zahid

cout << " " << a[i][j];

if (j == column - 1)

cout << endl << endl;

// Computing transpose of the matrix

for (int i = 0; i < row; ++i)

for (int j = 0; j < column; ++j) {

transpose[j][i] = a[i][j];

// Printing the transpose

cout << "\nTranspose of Matrix: " << endl;

for (int i = 0; i < column; ++i)

for (int j = 0; j < row; ++j) {

cout << " " << transpose[i][j];

if (j == row - 1)
CSL-113: Computer Programming Lab

Name : jawadahmed khan


cout << endl << endl; Semester : Spring-2021.bscs
Lab Engineer : TOOBA ZAHID
}

return 0;

}
CSL-113: Computer Programming Lab

Name : jawad ahmed khan


Semester : Spring-2021.bscs
Lab Engineer : Tooba zahid

OUTPUT:
CSL-113: Computer Programming Lab

Name : jawadahmed khan


Semester : Spring-2021.bscs
Lab Engineer : TOOBA ZAHID

THE END

You might also like