Question

INSTRUCTIONS 1. Prompt the user for the number of participating units and expected peak demand hours 2. Create two arrays: OTHINK: Think about this for a minute. This requires repeated action to shut down the number of units needed to meet demand, srun: Enter the number of units: Enter the number of hours: In hour 1 we need to shut down 2 units: Unit 0 is OFF with creditsISIVI3230 In-class lab Module - Working with Arrays Spring 2020 FOCUS TOPICS • Arrays: storing multiple values of the same ty

0 0
Add a comment Improve this question Transcribed image text
Answer #0

The answer will not be the same always because it is generating random integer. Do not panic if you don't get the same answer

#include <iostream>
using namespace std;

int main() {
   //no of participating units participating p
   //no of expected peak hours t
  
   int p,t;
   cin>>p>>t;
   cout<<"Enter the number of units:"<<endl;
   cout<<p<<endl;
   cout<<"Enter the number of hours:"<<endl;
   cout<<t<<endl;
   string Unitstatus[p];
   int Unitcredit[p];
   for(int i=0;i<p;i++)
   {
        Unitstatus[i]="ON";
        Unitcredit[i]=0;
   }
   //for every demand hour
   for(int i=1;i<=t;i++)
   {
        //generate random number between 0 and p
        int num = (rand() %(p - 0 + 1)) + 0;
        cout<<"In hour "<<i<<" we need to shut down "<<num<<" units:"<<endl;
        for(int j=0;j<num;j++)
        {   if(Unitstatus[j]=="OFF")
                Unitstatus[j]="ON";
            else
                Unitstatus[j]="OFF";
            Unitcredit[j]+=1;       
            cout<<"Unit "<<j<<" is "<<Unitstatus[j]<<" with credits: "<<Unitcredit[j]<<endl;
        }
        for(int j=num;j<p;j++)
        {
             cout<<"Unit "<<j<<" is "<<Unitstatus[j]<<" with credits: "<<Unitcredit[j]<<endl;
        }
      
   }
   return 0;
}

This code is in c++ and totally working

Here is java code:

/*package whatever //do not write package name here */

import java.io.*;
import java.util.Scanner;
import java.util.Random;

class GFG {
   public static void main (String[] args) {
       int p,t;
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter the number of units:");
       p = sc.nextInt();
       System.out.println(p);
       System.out.println("Enter the number of hours:");
       t = sc.nextInt();
       System.out.println(t);
       String[] Unitstatus= new String[p];
       int[] Unitcredit= new int[p];
       for(int i=0;i<p;i++)
       {
            Unitstatus[i]="ON";
            Unitcredit[i]=0;
       }
       //for every demand hour
     
   for(int i=1;i<=t;i++)
   {
        //generate random number between 0 and p
        Random rand = new Random();
        int num = rand.nextInt(p+1);
        System.out.println("In hour "+i+" we need to shut down "+num+" units:");
        for(int j=0;j<num;j++)
        {   if(Unitstatus[j]=="OFF")
                Unitstatus[j]="ON";
            else
                Unitstatus[j]="OFF";
            Unitcredit[j]+=1;     
            System.out.println("Unit "+j+" is "+Unitstatus[j]+" with credits: "+Unitcredit[j]);
        }
        for(int j=num;j<p;j++)
        {
             System.out.println("Unit "+j+" is "+Unitstatus[j]+" with credits: "+Unitcredit[j]);
        }
    
   }
      
   }
}

Add a comment
Know the answer?
Add Answer to:
INSTRUCTIONS 1. Prompt the user for the number of participating units and expected peak demand hours...
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
  • ​Declare an array with 1000 elements of type int Then in a loop generate 1000 random numbers and assign one to each element in the array The random numbers should be between 1 and 50 Then, prompt the user to enter a number, store this number in a local

    Rules to follow are:Declare an array with 1000 elements of type intThen in a loop generate 1000 random numbers and assign one to each element in the arrayThe random numbers should be between 1 and 50do not use rand or srand, use code is providedThen, prompt the user to enter a number, store this number in a local variable, the number should be safety checked using the GetInteger() functionThen, iterate through the array and determine how many times the user's...

  • Need a basic program in C using the instructions above. Thanks. (1) Prompt the user to...

    Need a basic program in C using the instructions above. Thanks. (1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in an array of doubles. Output the array's numbers on one line, each number followed by one space. (2 pts) Ex: Enter weight 1: 236 Enter weight 2: 89.5 Enter weight 3: 142 Enter weight 4: 166.3 Enter weight 5: 93 You entered 236.000000 89.500000 142.000000 166.300000 93.000000 (2) Also output the total weight,...

  • Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the...

    Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...

  • You will create a program with two methods, the main() method of course and another method...

    You will create a program with two methods, the main() method of course and another method called printArray(). The main method will define an array of 5 elements. A loop will be used to prompt the user to enter 5 numeric values which will go into the 5 array elements. The main() method will then call the printArray() method passing the array by reference, and the array length by value. The printArray() method will then print the contents of the...

  • write a java program that does the following Part one Use a For loop to compute...

    write a java program that does the following Part one Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic (Nested loops: Indent text) so that the...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • How many employees will be needed during demand of 610 units in period 5 if no...

    How many employees will be needed during demand of 610 units in period 5 if no overtime production is to be scheduled oogle Chrome ent/1/OPME%20Study%20Guide%20SP5%20-%202019.pdf 2. Case Study A company has the following demand forecast next year, expressed in six bimonthly(2-month) periods. Period قم قم | Forecast Demand (Standard Units of Work) 400 380 470 530 610 500 alo The following costing data have been obtained: • Each employee works 160 regular working hours per month Each unit requires 20...

  • Assignment 5 will be way different. It will be more like what you will receive in...

    Assignment 5 will be way different. It will be more like what you will receive in a programming shop. By that I mean that some things are built for you and others you will need to create or finish. P.S. part of your grade will include: Did you add in the required files? Did you rename your project? does your linear searches work? Did you put your code in the correct modules? did you change modules that you weren't supposed...

  • Has to be written in Python 3 GPA calculator: Assume the user has a bunch of...

    Has to be written in Python 3 GPA calculator: Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the grade and number of units for each course. It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work. We will use a while loop and after getting the data of one...

  • Instructions We're going to create a program which will allow the user to add values to a list, p...

    Instructions We're going to create a program which will allow the user to add values to a list, print the values, and print the sum of all the values. Part of this program will be a simple user interface. 1. In the Program class, add a static list of doubles: private statie List<double> _values new List<double>) this is the list of doubles well be working with in the program. 2. Add ReadInteger (string prompt) , and ReadDouble(string prompt) to the...

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