Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this C++ program. The code provided works in everyway but I need the code changed to where it will randomly generate seats

Need help with this C++ program. The code provided works in everyway but I need the code changed to where it will randomly generate seats numbers for the classroom instead of having the seat numbers entered through the keyboard.

Program requirements:

Create a program that will assign a random number to each seat in a classroom. The program should work for a classroom of any size, so your solution must use a 2-dimensional dynamic array. The main program should ask the user for the size of the classroom (number of rows along with number of seats per row) and the largest maximum number to be generated. Numbers generated will be in the range [1..MAX].

The program should continue asking for user input until valid data has been entered. For example, if the user entered 3 rows by 5 students per row and a maximum value of 10, it wouldnt be possible to generate unique numbers for each student. When the data is valid, allocate the space for your 2-D dynamic array.

The program needs to call four functions below the main:

  • One to generate the data and store it in the array. Be sure you dont repeat any numbers
  • Display the data in a tabular format
  • Display the smallest, largest, and mean value
  • Free up the memory that was allocated to the array

Code:

#include #include using namespace std; void getData(int **seats,int rows,int noOfSeats,int MAX); void display(int **seats,int rows,int noOfSeats); void displayMinMaxAvg(int **seats,int rows,int noOfSeats); bool isValidNumber(int **seats,int MAX,int rows,int noOfSeats,int number); int main() { int rows,noOfSeats; int MAX; cout<<"Enter number of rows :"; cin>>rows; cout<<"Enter number of seats :"; cin>>noOfSeats; while(true) { cout<<"Enter Maximum Number :"; cin>>MAX; if(MAX { cout<<"Invalid! Must be greater than or equal to "< } else break; }

int** seats = new int*[rows]; for (int i = 0; i < rows; ++i) seats[i] = new int[noOfSeats]; for(int i=0;i { for(int j=0;j { seats[i][j]=0; } } getData(seats,rows,noOfSeats,MAX); display(seats,rows,noOfSeats); displayMinMaxAvg(seats,rows,noOfSeats); return 0; }

void getData(int **seats,int rows,int noOfSeats,int MAX) { int number; for(int i=0;i { for(int j=0;j { while(true) { cout<<"Enter Number in Seat ["< cin>>number; if(number<1 || number>MAX) { cout<<"Invalid! Must be a number between 1-"<< MAX < continue; } else break; }

bool b=isValidNumber(seats,MAX,rows,noOfSeats,number); if(b) { seats[i][j]=number; j++; } else { cout<<"Invalid!"< } } } }

bool isValidNumber(int **seats,int MAX,int rows,int noOfSeats,int number) { for(int i=0;i { for(int j=0;j { if(seats[i][j]==number) return false; } } return true; } void display(int **seats,int rows,int noOfSeats) { for(int i=0;i { for(int j=0;j { cout< } cout< } }

void displayMinMaxAvg(int **seats,int rows,int noOfSeats) { int min,max; double sum=0,avg=0; min=seats[0][0]; max=seats[0][0]; for(int i=0;i { for(int j=0;j { if(min>seats[i][j]) min=seats[i][j]; if(max max=seats[i][j]; sum+=seats[i][j]; } } avg=sum/(rows*noOfSeats); cout<<"Minimum Number :"<< min < cout<<"Maximum Number :"<< max < cout<<"Mean :"<< avg < }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual C# And Databases

Authors: Philip Conrod, Lou Tylee

16th Edition

1951077083, 978-1951077082

More Books

Students also viewed these Databases questions

Question

What is one of the skills required for independent learning?Explain

Answered: 1 week ago