0% found this document useful (0 votes)
33 views

Solution:: Week 10 May - 14 May-2021

The document describes a programming problem to write a function that displays a diagonal matrix. It specifies that the function should take row and column arguments from the user, check if they are equal, and if so, call the displayDiagonal function to print the matrix with the row value on the diagonal and zeros elsewhere. It provides an example output and the solution code, which defines the displayDiagonal function to use nested for loops to print the row value when the row and column counters are equal, and zero otherwise.

Uploaded by

Ayesha Noor
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)
33 views

Solution:: Week 10 May - 14 May-2021

The document describes a programming problem to write a function that displays a diagonal matrix. It specifies that the function should take row and column arguments from the user, check if they are equal, and if so, call the displayDiagonal function to print the matrix with the row value on the diagonal and zeros elsewhere. It provides an example output and the solution code, which defines the displayDiagonal function to use nested for loops to print the row value when the row and column counters are equal, and zero otherwise.

Uploaded by

Ayesha Noor
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/ 3

Lab # 3

Week = 10 May – 14 May-2021

Problem Statement:

Write a program in which you have to define a function displayDiagnol which will have two integer arguments
named rows and cols. In the main function, take the values of rows and columns from the users. If the number
of rows is same as numbers of columns then call the displayDiagnol function else show a message on screen
that number of rows and columns is not same.

The following logic will be implemented inside the displayDiagnol function:

The function will take the value of rows and cols which are passed as argument and print the output in matrix
form. To print the values in the matrix form, nested loops should be used. For each loop, you have to use a
counter variable as counter. When the value of counters for each loop equals, then it prints the value of row at
that location and prints hard coded zero at any other location.

Example if the user enters rows and cols as 3, then the output should be like this

100

020

003

Example: when rows and columns are not same.

Example: when rows and columns are same.

Solution:
#include <iostream>
using namespace std;

void displayDiagonal(int,int); // function declaration


int main(){

int rows, columns;

rows = 0;

columns = 0;

cout<< "Enter the number of rows:";

cin>> rows;

cout<< "Enter the number of columns:";

cin>> columns;

if(rows == columns)

displayDiagonal(rows,columns); // call function

else

cout<< "Wrong input! Num of rows should be equal to num of columns";

return 0;

// function definition

void displayDiagonal(int rows, int columns){

for (int i = 1; i<=rows; i++) {

for (int j = 1; j<=columns; j++){

if(i==j)

cout<<i<< " ";


else

cout<< 0 << " ";

cout<< "\n";

You might also like