Lab Statement W4
Lab Statement W4
Lab Statement W4
Problem Statement:
“Calculate the average age of a class of ten students. Prompt the user to enter the age of each
student.”
“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 ;
Solution:
#include<iostream>
main() {
cin>>age1;
cout<<"please enter the age of student 2: ";
cin>>age2;
cin>>age3;
cin>>age4;
cin>>age5;
cin>>age6;
cin>>age7;
cin>>age8;
cin>>age9;
TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 + age9 + age10;
AverageAge = TotalAge/10;
}
Lab CS-201 Week # 2
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
#include <iostream>
using namespace std;
int main(){
rows = 0;
columns = 0;
cin>> rows;
cin>> columns;
if(rows == columns)
else
return 0;
// function definition
void displayDiagonal(int rows, int columns){
if(i==j)
else
cout<< "\n";
}
Lab CS-201 Week # 3
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.
Solution:
#include <iostream>
using namespace std;
int main() {
int number[10] = {
21,25,89,83,67,81,52,100,147,10
};
return 0;
}
int findMin(int array[],int size){
int min = 0;
min = array[0];
min = array[i];
}
return min;
int max = 0;
max = array[0];
max = array[i];
return max;
}
Lab CS-201 Week # 4
Problem Statement:
Note: For comparing both these arrays, the size should be same.
Solution:
#include <iostream>
#include <cstring>
// function definition
int check = 1;
int i = 0;
while(i<size){
if(arr1[i] != arr2[i]){
check = 0;
break;
}
i++;
if(check == 1)
else
int main() {
cin.getline(firstArray, sizeof(firstArray));
cin.getline(secondArray, sizeof(secondArray));
if(strlen(firstArray) == strlen(secondArray)){
else {
cout<< "Size of both names are not same" <<endl;
system("pause");
----------------------------
Week # 5
Write a program in which you need to declare an integer type matrix of size 4*4. In this
program:
1. You should take input values from the users and store it in 4*4 matrix.
2. Display this matrix on the screen.
3. Also, Display the transpose of this matrix by converting rows into cols.
#include <iostream>
main(){
int a[arraySize][arraySize];
readMatrix(a);
cout << "\n\n" << "The original matrix is: " << '\n';
displayMatrix(a);
transposeMatrix(a);
//Display the transposed matrix
cout << "\n\n" << "The transposed matrix is: " << '\n';
displayMatrix(a);
system("pause");
cout << "\n" << "Enter " << row << ", " << col << " element: ";
}
}
int temp;
a[row][col] = a[col][row];
}
Lab CS-201
Week # 6
#include <iostream>
struct MyStruct{
int i;
float f;
ms4 = {0,0.0};
MyStruct ms3;
return ms3;
}
main(){
cin>> ms1.i;
cin>> ms1.f;
cin>> ms2.i;
cin>> ms2.f;