# SOLVED CS201 Assignment 2 Solution and Discssion

• Assignment No. 2
Semester: Fall 2019
CS201 – Introduction to Programming Total Marks: 20

Due Date:
02-12-2019

Instructions
It should be clear that your assignment will not get any credit if:

o Assignment is submitted after due date.
o Submitted assignment does not open or file is corrupt.
o Assignment is copied (From internet/students).

Software allowed to develop Assignment

• Dev C++

Objectives:
In this assignment, the students will learn:
• How to fill / populate a two-dimensional array using random numbers.
• How to write user defined functions and pass an array to them as parameter.
• How to implement switch statement to process specific options.
• How to compare elements of array.
• How to find transpose of an array.

Assignment Submission Instructions
You are required to submit only .cpp file on the assignments interface of CS201 at VU-LMS. Assignment submitted in any other format will not be accepted and will be graded zero marks.

An array of arrays is known as two-Dimensional array. In C++ it is also known as matrix. A matrix can be represented as a table of rows and columns. Like single dimensional arrays, we can store same type of data in it. Elements stored in array can be accessed using array index. Index starts from . Means the index of row and column starts from 0. Following is an example of a two-dimensional array declaration and initialization having 3 rows and 3 columns.

``````int mularray = {         //Declaration and initialization of a 2D Array
{1, 2, 3},
{5, 7, 9},
{0, 2, 4}
};
// The value of mularray  is 1
// The value of mularray  is 2
// The value of mularray  is 9
// The value of mularray  is 2
``````

Problem Statement
Write an option-based program in C++ which should show following four options to the user at the start of application:

1. Press 1 to populate a two-dimensional array with integers from 1 to 100.
2. Press 2 to display the array elements.
3. Press 3 to display the largest element present in the array along with its row and column index.
4. Press 4 to find and show the transpose of the array.
Instructions to write C++ program:

 Use random number function to populate the array i.e. rand( ). Range should be 1 to 100.
Hint: use srand( ) function before rand( ) to seed the random number. In loop, rand( ) may generate same numbers so you should use srand(time(0)) before rand( ) to generate different random numbers. To use time(0) you may need to include time.h header file.
 Switch statement will be implemented to perform multiple conditions and to perform different actions based on the conditions. i.e. Option 1, 2, 3 and 4.
 Write user defined functions to perform tasks given in options. Following function names should be used for consistency.

To populate the array poulateArray( );
To display all elements of Array showElements( );
To show largest element in the array showLargestElement( );
To show transpose of the array transposeArray( );

Screenshots for Guidance:

1 If the user choses any option other than “1” at the start when the array is empty, the user should get a message like “Sorry the array is empty, first populate it by pressing 1 to perform this task ". See the following sample output: 2 Upon pressing 1, a message like “Array has been populated successfully!" should be displayed. See the following sample output: 3 Array elements should be displayed when the user presses 2. See the following screenshot for it: 4 Largest number along with its row and column number can be found by pressing 3. Similarly, if the user presses 4 transpose of the array should be displayed. See the sample outputs for both options: Good Luck!

Lectures Covered: This assignment covers Lecture # 11-15.
Deadline: The deadline to submit your assignment solution is 02-12-2019. Your assignment must be submitted within the due date through VU-LMS. No assignment will be accepted through email after the due date.

• Ideas Solution Code

``````#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

const int rows = 10;
const int cols = 10;

int selectOption(){
int choice = 0;

cout<<"Press 1 to populate a two-dimensional array with integers from 1 to 100\n";
cout<<"Press 2 to display the array elements\n";
cout<<"Press 3 to display the largest element present in the array along with its row and column index\n";
cout<<"Press 4 to find and show the transpose of the array\n";
cout<<"Press 5 To Exit\n";

cout<<"\nPlease select an option, use numbers from 1 to 5: ";

do {
cin>>choice;
if(choice >= 1 && choice <= 5){
break;
}
else {
cout<<"\nChoice should be between 1 and 5\n";
cout<<"Invalid choice, please select again: ";
}
} while(true);
cout<<"___________________________________________________\n";
return choice;
} // end selectOption function

void populateArray(int data[rows][cols]){

srand(time(0));
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){

data[i][j] = rand() % 100 + 1;
}
}
cout<<"Array populated sucessfully\n";
} // end of poulateArray function

void showElements(int data[rows][cols]){

for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){

cout<<data[i][j]<<"\t";
}
cout<<endl;
}
} // end of showElements function

void showLargestElement(int data[rows][cols]){

int largest = 1, row =0, col = 0;

for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){

if(data[i][j] > largest){
largest = data[i][j];
row = i;
col = j;
}
}
}
cout<<"Largest element is "<<largest<<" which is at row = "<<row+1<<" or index = "<<row<<" and column "<<col+1<<" or index "<<col<<endl;

} // end of showLargestElement function

void transposeArray(int data[rows][cols]){

for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
cout<<data[j][i]<<'\t';
}
cout<<endl;
}
} // end of transposeArray function

main(){

int choice = 0,  data[rows][cols] = {0};

do{
choice  = selectOption();

switch(choice){
case 1:
cout<<endl;
populateArray(data);
cout<<endl;
break;

case 2:
if(data == 0){
cout<<"\nSorry the array is empty, first populate it by pressing 1 to perform this task"<<endl<<endl<<endl;
continue;
}
cout<<endl;
showElements(data);
cout<<endl;
break;

case 3:
if(data == 0){
cout<<"\nSorry the array is empty, first populate it by pressing 1 to perform this task"<<endl<<endl<<endl;
continue;
}
cout<<endl;
showLargestElement(data);
cout<<endl;
break;

case 4:
if(data == 0){
cout<<"\nSorry the array is empty, first populate it by pressing 1 to perform this task"<<endl<<endl<<endl;
continue;
}
cout<<endl;
transposeArray(data);
cout<<endl;
break;
}
}while(choice != 5);  // end of do-while loop
} // end of main function

``````

• Ideas Solution Code

``````#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

const int rows = 10;
const int cols = 10;

int selectOption(){
int choice = 0;

cout<<"Press 1 to populate a two-dimensional array with integers from 1 to 100\n";
cout<<"Press 2 to display the array elements\n";
cout<<"Press 3 to display the largest element present in the array along with its row and column index\n";
cout<<"Press 4 to find and show the transpose of the array\n";
cout<<"Press 5 To Exit\n";

cout<<"\nPlease select an option, use numbers from 1 to 5: ";

do {
cin>>choice;
if(choice >= 1 && choice <= 5){
break;
}
else {
cout<<"\nChoice should be between 1 and 5\n";
cout<<"Invalid choice, please select again: ";
}
} while(true);
cout<<"___________________________________________________\n";
return choice;
} // end selectOption function

void populateArray(int data[rows][cols]){

srand(time(0));
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){

data[i][j] = rand() % 100 + 1;
}
}
cout<<"Array populated sucessfully\n";
} // end of poulateArray function

void showElements(int data[rows][cols]){

for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){

cout<<data[i][j]<<"\t";
}
cout<<endl;
}
} // end of showElements function

void showLargestElement(int data[rows][cols]){

int largest = 1, row =0, col = 0;

for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){

if(data[i][j] > largest){
largest = data[i][j];
row = i;
col = j;
}
}
}
cout<<"Largest element is "<<largest<<" which is at row = "<<row+1<<" or index = "<<row<<" and column "<<col+1<<" or index "<<col<<endl;

} // end of showLargestElement function

void transposeArray(int data[rows][cols]){

for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
cout<<data[j][i]<<'\t';
}
cout<<endl;
}
} // end of transposeArray function

main(){

int choice = 0,  data[rows][cols] = {0};

do{
choice  = selectOption();

switch(choice){
case 1:
cout<<endl;
populateArray(data);
cout<<endl;
break;

case 2:
if(data == 0){
cout<<"\nSorry the array is empty, first populate it by pressing 1 to perform this task"<<endl<<endl<<endl;
continue;
}
cout<<endl;
showElements(data);
cout<<endl;
break;

case 3:
if(data == 0){
cout<<"\nSorry the array is empty, first populate it by pressing 1 to perform this task"<<endl<<endl<<endl;
continue;
}
cout<<endl;
showLargestElement(data);
cout<<endl;
break;

case 4:
if(data == 0){
cout<<"\nSorry the array is empty, first populate it by pressing 1 to perform this task"<<endl<<endl<<endl;
continue;
}
cout<<endl;
transposeArray(data);
cout<<endl;
break;
}
}while(choice != 5);  // end of do-while loop
} // end of main function

``````  5

5

5

1

4

1

1
• ## SOC101 Assignment 2 Solution and Discussion SOC101 - Introduction to Sociology • soc101 assignment 2 solution discussion fall 2019 • • zareen

2  | |