Question

computer science

CSCI 3000 Homework 4

In this assignment, you will implement a simple version of Computer Lab administration system in C++. Your program will monitor computers in 4 computer labs and will allow users to log in and log out. Each computer lab has different number of computers.

·      Lab 1 has 10 computers

·      Lab 2 has 6 computers

·      Lab 3 has 3 computers

·      Lab 4 has 12 computers

Here is a sample state of the system:

Lab Array

Some of the computers are free (no one logged in), and some others are occupied by students.

In the implementation, you will define a Lab array (an array of string pointers) in the stack memory. This pointer array can be local to the main function or it can be a global. And each pointer in the array will point to a string array that is dynamically allocated from the heap memory. (Size of each array is given above)

Initially, all computers in all labs will be set to “Free” state. Then you will provide a menu to the user such as

1)    Print the current status of computers in all labs

2)    Log in a new student to a computer in a lab

a.     Ask user name, the lab number, and the computer number. If it is available, log that student in.

3)    Log out a student from his/her computer in a lab

a.     Ask user the lab number and the computer number. If the computer is occupied, log that student out.

4)    Exit

5)    (Bonus Option +20 Points) Add N new computers to a lab.

Note: Here you will ask user the lab number and the how many new computers (N) to add. Then you will add those computers to the lab without losing the current content (logged in students).

(Tip: You will allocate a new array space (with N more elements) from the heap, then move the content of the old array to the new array. Finally, you will send the address of the new array to corresponding lab pointer. All new elements (computers) will be set to “Free” state.

 

 

Example Case for Menu option 2:

If the current state of the labs as given in first figure, and if a new student “Kim” wants to login computer 3 of Lab 3, you will update the corresponding element in the correct array by writing students name. The new state of the labs will be

Another example would be logging out a student from the computers. In that case, the student name will be replaced with the word “Free” in the array.

 

On the top of your source code, please add your name and number, Course ID, HW number and the date using a comment block. Please see the example below,

/*

Your Name – Your number

CSCI 3000, HW-X

(Due Date)

*/

Please do not copy your code from someone else’s program. The instructor may use a code comparison program that automatically checks the similarity between different programs. And do not hesitate to contact me if you are having trouble with the homework. I will be more than happy t


0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
 
// function declaration
int menu();
void printLabs(string *labs[], int computerInLabs[], int numLabs);
 
int main() {
 
const int numLabs = 4; // number of labs in the computer
string *labs[4]; // create an array of string pointers of size 4
int computerInLabs[] = {10,6,3,12}; // specify the current number of computers in each of the 4 labs
int choice, labNumber, computerNumber,i;
string name;
 
// allocate the computers in the labs and initialize all to Free state
for(int i=0;i<numLabs;i++)
{
labs[i] = new string[computerInLabs[i]];
for(int j=0;j<computerInLabs[i];j++)
labs[i][j] = "Free";
}
 
// loop that continues till the user exits
do
{
// input of choice
choice = menu();
 
// perform the operation based on the choice
switch(choice)
{
case 1: // display the status of lab computers
printLabs(labs,computerInLabs,numLabs);
break;
 
case 2: // log in a new student to a computer in a lab
// input the name of the student
cout<<"Enter name : ";
cin>>name;
// input the lab number
cout<<"Enter the lab number(1-"<<numLabs<<") : ";
cin>>labNumber;
// validate the labNumber, if invalid re-prompt until valid
while(labNumber < 1 || labNumber > numLabs)
{
cout<<"Invalid lab number"<<endl;
cout<<"Enter the lab number(1-"<<numLabs<<") : ";
cin>>labNumber;
}
// input the computer number
cout<<"Enter the PC number(1-"<<computerInLabs[labNumber-1]<<") : ";
cin>>computerNumber;
// validate computer number for the given lab number, if invalid re-prompt until valid
while(computerNumber < 1 || computerNumber > computerInLabs[labNumber-1])
{
cout<<"Invalid computer number"<<endl;
cout<<"Enter the PC number(1-"<<computerInLabs[labNumber-1]<<") : ";
cin>>computerNumber;
}
// validate if labNumber in computerNumber in free or not
if(labs[labNumber-1][computerNumber-1].compare("Free") == 0)
{
// if free allocate it to the student
labs[labNumber-1][computerNumber-1] = name;
cout<<"Computer number : "<<computerNumber<<" in lab : "<<labNumber<<" allocated to "<<name<<endl;
}
else // not free
cout<<"Computer number : "<<computerNumber<<" in lab : "<<labNumber<<" not free"<<endl;
break;
 
case 3: // log out a student from a computer in the lab
// input the lab number
cout<<"Enter the lab number(1-"<<numLabs<<") : ";
cin>>labNumber;
// validate the labNumber, if invalid re-prompt until valid
while(labNumber < 1 || labNumber > numLabs)
{
cout<<"Invalid lab number"<<endl;
cout<<"Enter the lab number(1-"<<numLabs<<") : ";
cin>>labNumber;
}
// input the computer number
cout<<"Enter the PC number(1-"<<computerInLabs[labNumber-1]<<") : ";
cin>>computerNumber;
// validate computer number for the given lab number, if invalid re-prompt until valid
while(computerNumber < 1 || computerNumber > computerInLabs[labNumber-1])
{
cout<<"Invalid computer number"<<endl;
cout<<"Enter the PC number(1-"<<computerInLabs[labNumber-1]<<") : ";
cin>>computerNumber;
}
// validate if a ny student if allocated in the given computer for the given lab
if(labs[labNumber-1][computerNumber-1].compare("Free") != 0)
{
// if allocated, remove it and set it to free
name = labs[labNumber-1][computerNumber-1];
labs[labNumber-1][computerNumber-1] = "Free";
cout<<name<<" is logged out of computer number : "<<computerNumber<<" in lab number : "<<labNumber<<endl;
}else // computer is free
cout<<"Computer number : "<<computerNumber<<" in lab : "<<labNumber<<" is not allocated"<<endl;
break;
 
case 4: // add new computers to a lab
{
// input the lab number
cout<<"Enter lab number : ";
cin>>labNumber;
// validate the labNumber, if invalid re-prompt until valid
while(labNumber < 1 || labNumber > numLabs)
{
cout<<"Invalid lab number"<<endl;
cout<<"Enter the lab number(1-"<<numLabs<<") : ";
cin>>labNumber;
}
 
int N;
// input the number of computers to add
cout<<"Enter the number of computers to add : ";
cin>>N;
// validate number of computers, if invalid, re-prompt until valid
while(N <= 0)
{
cout<<"Invalid number of computers to add."<<endl;
cout<<"Enter the number of computers to add : ";
cin>>N;
}
// create a new lab with added number of computers
string *newLab = new string[computerInLabs[labNumber-1]+N];
// loop to copy the data from old lab
for(i=0;i<computerInLabs[labNumber-1];i++)
newLab[i] = labs[labNumber-1][i];
computerInLabs[labNumber-1] += N; // add N to array storing the number of computers for labs
// set the new computers to Free state
for(;i<computerInLabs[labNumber-1];i++)
{
newLab[i] = "Free";
}
 
delete labs[labNumber-1]; // delete the old lab
labs[labNumber-1] = newLab; // set old lab to new lab
break;
}
case 5: //exit
break;
}
}while(choice != 5);
 
// delete the memory allocated
for(int i=0;i<numLabs;i++)
delete labs[i];
return 0;
}
 
// function to input and return the user choice
int menu()
{
int choice;
// disply the menu
cout<<"\n1. Print the current status of computers in all labs"<<endl;
cout<<"2. Log in a new student to a computer in the lab"<<endl;
cout<<"3. Log out a student from his/her computer in a lab"<<endl;
cout<<"4. Add more computers in a lab"<<endl;
cout<<"5. Exit"<<endl;
cout<<"Enter a choice(1-5) : ";
// input of choice
cin>>choice;
// validate choice and re-prompt until valid
while(choice < 1 || choice > 5)
{
cout<<"Invalid choice."<<endl;
cout<<"Enter a choice(1-5) : ";
cin>>choice;
}
 
return choice;
}
 
// function to print the details of all computers in all labs
void printLabs(string *labs[], int computerInLabs[], int numLabs)
{
cout<<"Details of labs : "<<endl;
// loop over labs
for(int i=0;i<numLabs;i++)
{
cout<<"Lab-"<<(i+1)<<" : "<<endl;
// loop over each computer in the lab
for(int j=0;j<computerInLabs[i];j++)
{
cout<<left<<setw(5)<<"PC"<<(j+1)<<" : "<<left<<setw(15)<<labs[i][j]<<endl;
}
cout<<endl;
}
}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
computer science
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment, you will implement a simple version of Computer Lab administration system in C++....

    In this assignment, you will implement a simple version of Computer Lab administration system in C++. Your program will monitor computers in 4 computer labs and will allow users to log in and log out. Each computer lab has different number of computers. •Lab 1 has 10 computers •Lab 2 has 6 computers •Lab 3 has 3 computers •Lab 4 has 12 computers Here is an example state of the system: Lab Array Some of the computers are free (no...

  • Please post the code in c++! Computer Labs Write a Computer Labs program to store the...

    Please post the code in c++! Computer Labs Write a Computer Labs program to store the list of user IDs for each computer station using a linked list. Problem Description: You run four computer labs. Each lab contains computer stations that are numbered as shown in the table below: Lab Number Computer station Numbers 1-5 2 1-6 1-4 4 1-3 Each user has a unique five-digit ID number. Whenever a user logs on, the User's ID, labnumber, and the computer...

  • Write the following program in C++ You run four computer labs, Each lab contains computer stations...

    Write the following program in C++ You run four computer labs, Each lab contains computer stations that are numbered as shown in the table below: Lab Number Computer Station Numbers 1 1 - 5 2 1 - 6 3 1 - 4 4 1 - 3 Each user has a unique five-digit ID number. Whenever a user logs on, the user’s ID, lab number, and the computer station number are transmitted to your system. For example, if user 49193 logs...

  • JAVA Programming Assignment: You run four computer labs. Each lab contains computer stations that are numbered...

    JAVA Programming Assignment: You run four computer labs. Each lab contains computer stations that are numbered as shown in the table below:- Lab Number   Computer Station Numbers 1   1-5 2   1-6 3   1-4 4   1-3 Each user has a unique five-digit ID Number. Whenever a user logs on, the user's ID, Lab Number, and the computer station are transmitted to your station. For example, if user 49193 logs onto station 2 in Lab 3, your system receives (49193, 2, 3)...

  • Principles of Computer Science

    Question First, you need to design, code in Java, test and document a base class, Student. The Student class will have the following information, and all of these should be defined as Private: A. Title of the student (eg Mr, Miss, Ms, Mrs etc) B. A first name (given name) C. A last name (family name/surname) D. Student number (ID) – an integer number (of type long) E. A date of birth (in day/month/year format – three ints) - (Do NOT use the Date class from...

  • Java Computer Labs Assignment! The code NEEDS to be commented properly! Every class file should have a header comment that includes your name and assignment number and briefly documents what the prog...

    Java Computer Labs Assignment! The code NEEDS to be commented properly! Every class file should have a header comment that includes your name and assignment number and briefly documents what the program does! If there are known deficiencies with your program such as known problems or incomplete features, these should be clearly listed in the header comment! Every method should have a method header comment that documents what the method does, what its parameters are, and what it returns! You...

  • C++ program that could be used to track, by lab, who is logged into which computer

    C++ program that could be used to track, by lab, who is logged into which computer 1. |15 points| Suppose you run four computer labs. Each lab contains computer stations that are numbered as shown in the table below Lab Number |Computer Station Numbers 1-5 2 1-4 1-3 Each user has a unique five-digit ID number. Whenever a user logs on, the user's ID, lab number, and the computer station number are transmitted to your system. For instance, if user...

  • Introduction: In this lab, you will write a MIPS program to read in (up to) 50...

    Introduction: In this lab, you will write a MIPS program to read in (up to) 50 integer values from the user, store them in an array, print out the amay, one number per line, reverse the elements in the array and finally print out the elements in the just-reversed) array. Feel free to do this lab and all assembly programming labs) in Windows. You must use MARS Getting started: l. In MARS, create a new assembly file with the name...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT