CS201 (P) ProblemStatement
CS201 (P) ProblemStatement
CS201 (P) ProblemStatement
Topics Covered:
● Arrays
● Functions
Problem Statement:
Write a program in which you have to declare an integer array of size 10 and initializes it with
numbers of your choice. Find the maximum and minimum number from the array and output the
numbers on the screen.
For finding the maximum and minimum numbers from the array you need to declare two
functions findMax and findMin which accept an array and size of array (an int variable) as
arguments and find the max min numbers, and return those values.
#include <iostream>
//functions declaration
int main() {
//Array initialization
int number[10] = {
21,25,89,83,67,81,52,100,147,10
};
//Displaying minimum and maximum number
return 0;
int min = 0;
min = array[0];//Storing the value of the first element of array in 'min' variable
if(min > array[i])//Testing if the value of 'min' variable is greater than the current
element of array
int max = 0;
max = array[0];//Storing the value of the first element of array in 'max' variable
Lab # 3
Topics Covered:
Functions
Repetition Structure (Loop)
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 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
Solution:
#include <iostream>
int main(){
rows = 0;
columns = 0;
cin>> rows;
cin>> columns;
if(rows == columns)//conditional check for square matrix
else
return 0;
// function definition
if(i==j)//diagonal check
else
cout<< "\n";
}
Lab # 2
Topics Covered:
“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;
⮚ “Prompt the user to enter the age of each student” this requires cin>> statement.
For example:
cin>> age;
⮚ Average can be calculated by doing addition of 10 values and dividing sum with 10.
Solution:
#include<iostream>
main() {
cin>>age;
Lab # 1
Topics Covered:
● Variables
● Data Types
● Arithmetic Operators
● Precedence of Operators
Problem Statement:
“Calculate the average age of a class of ten students. Prompt the user to enter the age of each
student.”
\\ Case Sensitive
⮚ “Prompt the user to enter the age of each student” this requires cin>> statement.
For example:
cin >> age1;
cin >> age2;
cin >> age3;
⮚ 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;
Solution:
#include<iostream>
int main(){
cin>>age1;
cin>>age2;
cin>>age3;
cin>>age4;
cin>>age5;
cin>>age6;
cin>>age7;
cin>>age8;
cin>>age9;
cin>>age10;
Alternative Solution
#include<iostream>
main() {
// declaring variable age to take input
int age=0;
// using for loop to take input of each students and adding them
for (int i = 1;i<=10;i++){
cin>>age;
TotalAge += age;