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

Lab Exercise

The document describes a lab problem to calculate the average age of 10 students by prompting the user to enter each student's age. It then provides the C++ code to define variables, input the ages, calculate the total and average age, and output the result. The second part of the document describes a lab problem to write a function that displays a diagonal matrix given the number of rows and columns. It provides an example output and explains the logic to check if rows equals columns before calling the function, which uses nested loops to print the row value when the counters match and 0 otherwise.

Uploaded by

M NOOR MALIK
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Lab Exercise

The document describes a lab problem to calculate the average age of 10 students by prompting the user to enter each student's age. It then provides the C++ code to define variables, input the ages, calculate the total and average age, and output the result. The second part of the document describes a lab problem to write a function that displays a diagonal matrix given the number of rows and columns. It provides an example output and explains the logic to check if rows equals columns before calling the function, which uses nested loops to print the row value when the counters match and 0 otherwise.

Uploaded by

M NOOR MALIK
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Labs CS-201

Week # 2

Lab 1

Problem Statement:

“Calculate the average age of a class of ten students. Prompt the user to enter the age of each
student.”

 We need 10 variables of int type to calculate the average age.


int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10;

 “Prompt the user to enter the age of each student” this requires cin>> statement.
For example:
cin>> age1;

 Average can be calculated by doing addition of 10 variables and dividing sum with 10.

TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 +age9 + age10 ;

AverageAge = TotalAge / 10;

Solution:

#include<iostream>

using namespace std;

main() {

int age1 = 0, age2 = 0, age3 = 0, age4 = 0, age5 = 0, age6 = 0, age7 = 0, age8 = 0,


age9 = 0, age10 = 0;

int TotalAge = 0, AverageAge = 0;

cout<<"please enter the age of student 1: ";


cin>>age1;

cout<<"please enter the age of student 2: ";

cin>>age2;

cout<<"please enter the age of student 3: ";

cin>>age3;

cout<<"please enter the age of student 4: ";

cin>>age4;

cout<<"please enter the age of student 5: ";

cin>>age5;

cout<<"please enter the age of student 6: ";

cin>>age6;

cout<<"please enter the age of student 7: ";

cin>>age7;

cout<<"please enter the age of student 8: ";

cin>>age8;

cout<<"please enter the age of student 9: ";

cin>>age9;
cout<<"please enter the age of student 10: ";

cin>>age10;

TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 + age9 + age10;

AverageAge = TotalAge/10;

//Display the result (average age)

cout<<"Average of students is: "<<AverageAge;

}
Week # 3

Lab2

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.


#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