CS201P Problem Statement
- Type
- Quiz
- Course
- CS201P Introduction to Programming (Practical)
- Size
- PDF 509 KB
- Updated
- 23 Oct 2026
You get 3 free files a day on the website. Add the Chrome extension for 30 a day.
Preview text
Text from pages 1, 2, 4 of 9. Download the file for all 9 pages.
Lab # 3
Week = 1-May -- 5-May-2023
Functions
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
Example: when rows and columns are not same.
Example: when rows and columns are same.
Solution:
void displayDiagonal(int,int); // function declaration
int main(){
int rows, columns;//variable declaration and initialization
rows = 0;
columns = 0;
cout<< " Enter the number of rows:";//Taking number of rows as input
cin>> rows;
cout<< " Enter the number of columns:";//Taking columns of rows as input
cin>> columns;
if(rows == columns)//conditional check for square matrix
displayDiagonal(rows,columns); // call function
else
cout<< " Wrong input! Num of rows should be equal to num of columns"; “Calculate the average age of a class of ten students using while loop. Prompt the user to enter the age of each student.”
- We need 10 values of variable age of int type to calculate the average age.
int age;
cin>> age;
- Average can be calculated by doing addition of 10 values and dividing sum with 10.
Solution:
main() {
int age=0;
// declaring ageCounter variable to check the number of iterations int ageCounter=0;
Related CS201P files
All CS201P files →- CS201P Final Term MCQs by Miss Mehwish Final term PDF 1.1 MB
- CS201P Quiz 3 Solution by Lucky Quiz PDF 681 KB
- CS201P Quiz 2023 by Danish Hanif Quiz PDF 409 KB
- CS201P Quiz 9 Updated Aug 2022 by Attiq Kundi Quiz PDF 659 KB
- CS201P Quiz 7 Solution 2023 Quiz PDF 273 KB
- CS201P Lab 9-10 Quiz Quiz PDF 607 KB
Get more out of VUBuddy, for free
- Unlimited downloads
- The explanation behind every MCQ answer
- Study offline on slow data
- 30 downloads a day instead of 3
- Sign in and buy Pro Semester here
One Pro Semester membership works in both the extension and the app.