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]);
}
}
}
}
INSTRUCTIONS 1. Prompt the user for the number of participating units and expected peak demand hours...
Code in C++ Instructions Write a program which... Prompt the user to enter the number of values that they would like to insert into a binary search tree. Prompt the user to insert, one-at-a-time, a value into the binary search tree. Print the preorder, postorder, and inorder traversal on the tree. Binary Search Tree The methods preorder, postorder, inorder, preorderR, postorderR, and inorderR will all contains the necessary screen output statements to verify that the traversal is happening correctly (see...
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 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 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 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 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...
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...
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...
(Done in Eclipse Java) 1. Given an integer between 1—100 captured from a user, perform the following conditional actions: • If is odd, print Weird • If is even and in the inclusive range of 2 to 5, print Not Weird • If is even and in the inclusive range of 6 to 20, print Weird • If is even and greater than 20, print Not Weird Note: Validate that your algorithm works for all cases. 2. Read an integer...
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...