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

CS201 Problem Statment

The document describes a lab assignment on multi-dimensional arrays. It asks students to write a program that takes a 4x4 integer array as input from the user, displays the original array and its transpose by swapping rows and columns. The solution program defines functions to read the array, display it, and transpose it by swapping elements. It uses a 4x4 integer array, nested for loops to input values and display the array, and a temporary variable to swap elements for transposition.

Uploaded by

m.h ss
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

CS201 Problem Statment

The document describes a lab assignment on multi-dimensional arrays. It asks students to write a program that takes a 4x4 integer array as input from the user, displays the original array and its transpose by swapping rows and columns. The solution program defines functions to read the array, display it, and transpose it by swapping elements. It uses a 4x4 integer array, nested for loops to input values and display the array, and a temporary variable to swap elements for transposition.

Uploaded by

m.h ss
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lab # 6

Week = 5th Dec -2022 – 9th Dec -2022

Topics Covered:

 Multi-dimensional Arrays

Problem Statement:

Write a program in which you need to declare a multidimensional integer type array of size 4*4. In this
program:

 You should take input values from the users and store it in 4*4 matrix.
 Display this matrix on the screen.
 Also, display the transpose of this matrix by converting rows into cols.

Note: Use different functions for above three point’s functionalities.

Solution:

#include <iostream>

using namespace std;

const int arraySize = 4; // intializing constant variable with value 4

void readMatrix(int arr[][arraySize]); // prototype of a function that takes input values from the user
and will store it into the array.

void displayMatrix(int a[][arraySize]); // prototype/signature of the function to display array values

void transposeMatrix(int a[][arraySize]); // protype of a function that will change rows into columns and
columns into rows.

main(){

// Declaring a multi-dimensional array

int a[arraySize][arraySize];

// Taking input from user

readMatrix(a); // Call by value of a function to store input values in the array.


// Display the matrix

cout << "\n\n" << "The original matrix is: " << '\n';

displayMatrix(a); // Calling a function that is written to display array values from a multi-
dimensional array.

//Taking Transpose of a matrix

transposeMatrix(a); //Calling a function that will return swapped value at the end.

//Display the transposed matrix

cout << "\n\n" << "The transposed matrix is: " << '\n';

displayMatrix(a); // Calling a function that is written to display array values from a multi-
dimensional array. This time it will show swapped values.

// Defining a readMatrix() function, in which we are using nested for loop to build a multi-dimensional
matrix.

void readMatrix(int arr[arraySize][arraySize]){

int row, col;

for (row = 0; row < arraySize; row ++){

for(col=0; col < arraySize; col ++){

cout << "\n" << "Enter " << row << ", " << col << " element: ";

cin >> arr[row][col];

cout << '\n';

// Defining displayMatrix() function, that will traverse through the whole array and will display array
elements.

void displayMatrix(int a[][arraySize]){


int row, col;

for (row = 0; row < arraySize; row ++){

for(col = 0; col < arraySize; col ++){

cout << a[row][col] << '\t';

cout << '\n';

// A function that is swapping whole rows into columns and columns into arrays.

void transposeMatrix(int a[][arraySize]){

int row, col;

int temp;

for (row = 0; row < arraySize; row ++){

for (col = row; col < arraySize; col ++){

/* Interchange the values here using the swapping mechanism */

temp = a[row][col]; // Save the original value in the temp variable

a[row][col] = a[col][row];

a[col][row] = temp; //Take out the original value

}//end of lab=6
Lab # 5

Week = 28th Nov – 02nd Dec -2022

Topics Covered:

 Array Manipulation
 Pointers

Problem Statement:

1. Write a program that will take two strings as input from the user in the form of
character arrays namely as “firstArray” and “secondArray” respectively.
2. Both arrays along with the size will be passed to the function compareString.
3. compareString function will use pointer to receive arrays in function and then
start comparing both arrays using while loop.
4. If all the characters of both these arrays are same then the message “Both strings
are same” should be displayed on the screen.

Note: For comparing both these arrays, the size should be same.

#include <iostream> // header file for input/output streams

#include <cstring> // header file for manipulating strings

using namespace std;

//function definition

void compareString(char *array1, char *array2, int array_size){

//Initializing variables

int flag = 1;

int i = 0;

//Start of while loop

while(i<array_size){

if(array1[i] != array2[i]) //Comparing each character of arrays


{

flag = 0; //Set Variable value to 0

break; // Terminate from loop

i++; //Incrementing variable value by 1

if(flag == 1) //Check whether variable value is 1

cout<< "Both strings are same!" <<endl;

else

cout<< "Both strings are not same!" <<endl;

int main() {

//Declaring character array of size 20

char firstArray[20];

char secondArray[20];

cout<< "Enter the First Name: ";

cin.getline(firstArray, sizeof(firstArray)); //First String Input from user

cout<< "Enter the Second Name: ";

cin.getline(secondArray, sizeof(secondArray));//Second String Input from user

if(strlen(firstArray) == strlen(secondArray)) //Comparing size of both arrays

{
int array_size = strlen(firstArray); //Getting the size of the string

compareString(firstArray, secondArray, array_size); //Calling compareString


function

else {

cout<< "Size of both arrays are not same" <<endl;

}
Lab # 4

Week = 21-Nov -- 25-Nov-2022

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.

Solution:

#include <iostream>

using namespace std;

//functions declaration

int findMin(int [],int);

int findMax(int [],int);

int main() {

const int SIZE = 10; //size of array

//Array initialization

int number[10] = {

21,25,89,83,67,81,52,100,147,10

};

//Displaying minimum and maximum number

cout<< "Maximum number in the array is :" <<findMax(number, SIZE) <<endl;


cout<< "Minimum number in the array is :" <<findMin(number, SIZE) <<endl;

return 0;

//Definition of findMin function

int findMin(int array[],int size){

int min = 0;

min = array[0];//Storing the value of the first element of array in 'min' variable

for (int i = 0; i<size; i++){ //loop for traversing array

if(min > array[i])//Testing if the value of 'min' variable is greater than the current
element of array

min = array[i];//Storing the value of current element of array in 'min'


variable

return min; //returning the minimum value of the array

//Definition of findMax function

int findMax(int array[],int size){

int max = 0;

max = array[0];//Storing the value of the first element of array in 'max' variable

for (int i = 0; i<size; i++){//loop for traversing array

if(max < array[i]) //Testing if the value of 'max' variable is less than the current
element of array

max = array[i];//Storing the value of current element of array in 'max' variable


}

return max; //returning the maximum value of the array

Lab # 3

Week = 14-Nov -- 18-Nov-2022

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 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;//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";

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)//diagonal check

cout<<i<< " "; //displaying element

else

cout<< 0 << " ";

cout<< "\n";

Lab # 2
Week = 07-Nov -- 11-Nov-2022

Topics Covered:

 Repetition Structure (Loop)

Problem Statement: While Loop

“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.

TotalAge = TotalAge + age1;

AverageAge = TotalAge / 10;

Solution:

#include<iostream>

using namespace std;

main() {

// declaring variable age to take input


int age=0;

// declaring variables to calculate totalAge and averageAge


int TotalAge = 0, AverageAge = 0;

// declaring ageCounter variable to check the number of iterations


int ageCounter=0;
// comparing the value of ageCounter

while (ageCounter <10)

cout<<"please enter the age of student "<<++ageCounter<<" :\t";

cin>>age;

//calculate totalAge by adding age of all students

TotalAge = TotalAge + age;

cout<< "Total Age of 10 students = "<<TotalAge<<endl;


// calculate AverageAge by dividing the totalAge with number of students
AverageAge = TotalAge/10;

//Display the result (average age)


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

}
Lab # 1

Week = 07-Nov -- 11-Nov-2022

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.”

 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;

int main(){

// declaring 10 integer variables to take input of students age


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

// declaring variables to calculate totalAge and averageAge


int TotalAge = 0, AverageAge = 0;
// taking input of each student age
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;

//calculate totalAge by adding age of all students


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

// calculate AverageAge by dividing the totalAge with number of students


AverageAge = TotalAge/10;
//Display the result (average age)

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

Alternative Solution

#include<iostream>

using namespace std;

main() {
// declaring variable age to take input
int age=0;

// declaring variables to calculate totalAge and averageAge


int TotalAge = 0, AverageAge = 0;

// using for loop to take input of each students and adding them
for (int i = 1;i<=10;i++){

cout<<"please enter the age of student "<<i<<" :\t";

cin>>age;

TotalAge += age;

// calculate AverageAge by dividing the totalAge with number of students


AverageAge = TotalAge/10;

//Display the result (average age)

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

You might also like