Question

In: Computer Science

For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined...

For C++ Use arrays and or vectors, no classes.

Visualize and consider 100 lockers all lined up horizontally in a row
Each locker is numbered from 1 to 100 in sequential order

Every locker can be fully closed (open state = 0.00)
Every locker can be fully opened (open = 1.00)
Every locker can be partially open with any possible value between 0.00 and 1.00 inclusive on both ends
A locker cannot ever be more closed than fully closed (open cannot be less than 0.00)
A locker cannot ever be more open than fully opened (open cannot be greater than 1.00)

All 100 lockers start in the fully closed state
100 students will be walking by the lockers and opening/closing them in the following manner:

The 1st student will OPEN EVERY LOCKER       1/2 of the way (ADD 0.50 TO LOCKERS 1,2,3,4...)

The 2nd student will CLOSE EVERY SECOND LOCKER 1/3 of the way (SUBTRACT 0.333333333333 FROM LOCKERS 2,4,6,8...)

The 3rd student will OPEN EVERY THIRD LOCKER   1/4 of the way (ADD 0.25 TO LOCKERS 3,6,9,12...)

The 4th student will CLOSE EVERY FOURTH LOCKER 1/5 of the way (SUBTRACT 0.20 FROM LOCKERS 4,8,12,16...)

The 5th student will OPEN EVERY FIFTH LOCKER   1/6 of the way (ADD 0.166666666666 TO LOCKERS 5,10,15,20...)

The 6th student will CLOSE EVERY SIXTH LOCKER 1/7 of the way (SUBTRACT 0.142857142857 FROM LOCKERS 6,12,18,24...)

The 99th student will OPEN EVERY 99TH LOCKER   1/100 of the way (ADD 0.01 TO LOCKER 99)

The 100th student will CLOSE EVERY 100TH LOCKER 1/101 of the way (SUBTRACT 0.009900990099 FROM LOCKER 100)

NOTE: Remember that the locker open state must always stay within 0.00 <= value <= 1.00

1) Develop C++ code that will generate the open state values for all 100 lockers

2) Also develop C++ code that will output answers to the following questions:

Which lockers are left fully closed (open state == 0.00)?

Which lockers are left fully open (open state == 1.00)?

Which locker is the one opened the least and what is its value (open state closest to 0.00)?

Which locker is the one closed the least and what is its value (open state closest to 1.00)?

Solutions

Expert Solution

#include<iostream>
using namespace std;

double perform(int n) //method to calculate how much open/close to perform
{
   double value = (double)1.0/(n+1);
   if(n%2 == 0) //if # of person is even, locker should be closed
   value = value * (-1); //so multiply -1
   return value;
}

int main()
{
   double locker[100],howmuch;
   int i,j;
   for(i=0;i<100;i++)
   locker[i] = 0.0; //initialising value of locker
   for(i=1;i<=100;i++)
   {
       howmuch = perform(i); //calculate how much to open/close
       for(j=(i-1);j<100;j=j+i)
       {
           locker[j] = locker[j] + howmuch; //open close the locker
           if(locker[j]<0) //see if value<0 or >1
           locker[j] = 0;
           else if(locker[j]>1)
           locker[j] = 1;
       }
   }
   //un-comment the next part to display status of every locker
   /*
   for(i=1;i<=100;i++)
   {
       cout << i << "\t" << locker[i-1] << "\n";
   }
   */

   double min=1,max=0; //set minimum and maximum
   int openpos,closepos;
   cout << "\nFully closed lockers:\t";
   for(i=1;i<=100;i++)
   {
       if(locker[i-1]==0) //if locker is fully closed, print
       cout << i << ",";
   }
   cout << "\nFully open lockers:\t";
   for(i=1;i<=100;i++)
   {
       if(locker[i-1]==1) //if locker is fully open, print
       cout << i << ",";
   }
   for(i=1;i<=100;i++)
   {
       if(locker[i-1]>0 && locker[i-1]<min) //if locker is more closed than min but not 0, make this min
       {
           openpos = i;
           min = locker[i-1];
       }
       if(locker[i-1]<1 && locker[i-1]>max) //if locker is more open than max but not 1, make this max
       {
           closepos = i;
           max = locker[i-1];
       }
   }
   cout << "\n\nLocker " << openpos << " is least opened with value " << min;
   cout << "\nLocker " << closepos << " is least closed with value " << max;
}

Sample execution:

NOTE: Un-comment the specified part to get status of every locker.

THANK YOU!! PLEASE VOTE


Related Solutions

You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files 1. WriteaclassGradeBookcontainingthefollowing: Private attributes: - courseName: a string representing the name of the course. - nbOfStudents: an integer representing the number of students enrolled in the course. The number of students is greater than or equal to 5. - grades: a double dimensional array of integers representing the grades of Test1, Test2 and Final of every student. It should be a dynamic array. Public...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if we want! Choice either vectors or arrays either one is fine We will implement a classic Inheritance hierarchy. A simple console based interface is all that is needed. Build your classes first, each in their own .h and .cpp files, then test them with the simple main method provided below. Phase 1 : Here is the following set of classes you will implement and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following...
Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following is a partial implementation of Set class. You are required to enhance and implement the following missing functions from the implementation: A) UNION B) reset C) intersection D) difference A SAMPLE driver program : int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2); Set s4 = s1.intersection(s2); Set s5 = s1.difference(s2); s3.print("s3"); s4.print("s4");...
Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers...
Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers to the classic implementation of strings in the C programming language. A C-string is a sequence of characters terminated by the null character, '\0'. Since C is part of C++, C-string implementations are valid in C++ as well as C. Recall that an array of characters is the underlying data structure for storing C-strings. For example, this definition creates such an array. char myString[100];...
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION...
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION WHICH ALREADY EXISTS ON CHEG. DO NO USE ARRAY LIST Scenario: A dog shelter would like a simple system to keep track of all the dogs that pass through the facility. The system must record for each dog: dogId (int) - must be unique name (string) age (double) - cannot be less than 0 or more than 25 breed (string) sex (char) – m...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT