Water Solution keep getting error message "did not find 12\n" - c

#include <cs50.h>
#include <stdio.h>
int main(void) {
printf("How many minutes do you shower for?\n");
int minutes = get_int();
int bottles = minutes * (192/16);
printf("Minutes: %i\n",minutes);
printf("Bottles: %i\n",bottles);
printf("%i minute equals %i bottles.\n", minutes, bottles);
do {
minutes = get_int();
}
while (minutes < 0);
}
This may be a simple problem, but I am very stuck right now and would appreciate the help!
When I use check50 to check the code this message keeps popping up:
:) water.c exists.
:) water.c compiles.
:) 1 minute equals 12 bottles.
:) 2 minute equals 24 bottles.
:( 5 minute equals 60 bottles.
timed out while waiting for program to exit
:( 10 minute equals 120 bottles.
timed out while waiting for program to exit
:) rejects "foo" minutes
:) rejects "" minutes
:) rejects "123abc" minutes

Related

Round-Robin Scheduling Algorithm in OS

I have been trying to understand how the round robin concept and how the algorithm works. I have tried running this code in ubuntu, and i'm unable to get the answer that i wanted.
So based on the Round Robin Scheduling Algorithm; Let's say there are 3 processes. Where Processor 1 - Burst Time is 24, Processor 2 - Burst time is 3 and Processor 3 - Burst time is 3. and the Time Quantum is 3.
Based on this information, the waiting time for P1 is 6, P2 is 4 and P3 is 7. So the Turn Around Time is P1 is 30, P2 is 7 and P3 is 10.
Average Turn Around time is 15.6667 and The average waiting time is 5.667
Based on the code below, if i run it, it would return me; for waiting time - P1 is 6, P2 is 3 and P3 is 6, Turn around time P1 is 30, P2 is 6, P3 is 9.
And the Average Turn Around time is 15.0000 and The average waiting time is 5.000
I'm unable to figure out the error. Can any one help me with this problem and provide an explanation to error and solution?
#include<stdio.h>
#include<curses.h>
int main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clear();
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
{
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
getch();
}
The code is returning the right answer.
All the processes arrived at time 0.
P1 - 0-3 21 units remaining
P2 - 3-6 0 units remaining
P3 - 6-9 0 units remaining
P1 - 9-30 0 units remaining
P1 waited 6 units, P2 waited 3 units and P3 waited 6 units.
Note that Waiting time is the amount of time a process is waiting without being executed after given to the scheduler. Turnaround time is the total time a process took to complete its execution (Waiting time + Burst time).
Avg waiting time: (6+3+6) / 3 = 15
Average Turnaround time = (30+6+9) / 3 = 15

How can I print values in C at intervals of 1 sec?

I am trying to build a traffic light simulator which requires that I print green for the first 8 seconds at 1 second intervals, yellow for the next 4 seconds at 1 second intervals and red for the last 8 seconds at 1 second intervals. How can I use time.h to implement this in C?
This is my attempt, but I only get an output that prints green nonstop at intervals which are not 1 second long.
// Traffic light simul`ator
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t start, end;
double elapsed;
time(&start); /* start the timer */
do {
time(&end);
elapsed = difftime(end, start);
if (elapsed )
{
printf("green");
}
} while(elapsed < 9);
}
The following seems to work as intended:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t start, end;
double elapsed, prev_elapsed = 0.0;
time(&start); /* start the timer */
do
{
time(&end);
elapsed = difftime(end, start);
if (elapsed >= prev_elapsed+1.0)
{
printf("green\n");
prev_elapsed = elapsed;
}
} while(elapsed < 9.0);
}
Here we keep track of the 'current' elapsed time, and the time when the previous one-second tick was noted. When this 'current' elapsed differs from the "previous" elapsed we know that one second (or, more likely, slightly more) has gone by, and we print "green".
Your code will just print "green" as fast as it can because if(elapsed) will always happen because the condition is always True. You can use difftime but will need to re-work your code a bit (I think it is adding maybe 2-3 lines). Is there a reason you can't just use sleep? It seems to be a simpler solution: print "green", call sleep(1), repeat 7 more times, move on to printing "yellow", and so on. Hope this helps!
Edit: if(elapsed) is True as long as the value elapsed is not equal to 0, and just because computers don't act instantaneously there will always be a non-zero time difference returned by difftime.
Okay, so I figured out what is going on here.
difftime() returns double. That means, fractional seconds can be returned. If they are (which they probably will be), then that means you have some value in elapsed, which means it will not evaluate to false--even though 1 full second has not yet passed.
If you want elapsed to be greater than 1, then just check for it:
if (elapsed >= 1)
{
printf("green");
}
That should do the trick.

I want to make a timer in C

So what I wanted to make is a timer that won't do anything for 25 minutes and then play an mp3 file as an alarm. Only solution I've come across is using the sleep() function, but I don't know if it'll cause any problems due to running for 25 minutes (I see it being used for 3 seconds not 25 * 60). I don't if using it for this long is too taxing on the system or inefficient.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
size_t sessions;
puts("Enter number of sessions to work: ");
scanf("%u" , &sessions);
int work_time = 25*60; //25 minutes to seconds
int break_time = 5 *60; //5 minutes to seconds
for(int i = 1 ; i <= sessions ; i++)
{
sleep(work_time);
printf("Have a 5 minute break...You deserve it :)\n");
sleep(break_time);
puts("Break is over, let's get shit done\n\n");
}
//next line isn't probably the best way to do it but it works for now
system("start wmplayer C:\\Users\\me\\Desktop\\myuser\\english\\fartingnoise.mp3");
return 0;
}
Here's my code so far, my main question is about the sleep function but any criticism of my code is welcome.
No, there is no problem with sleeping for 25 minutes.
In fact, using sleep is about the most efficient way to wait for 25 minutes.

Invalid operands to binary & (have 'float *' and 'float')

I can't figure out what I need to do to get my code to work, I've tried a few things but the same error keeps occurring. I'm not sure how else to proceed, the main issue of the code appears to be converting the hours and minutes into only hours. I know this is a very basic question, but I'm a beginner and I can't seem to get a solution.
// freezer.c
// Estimates the temperature in a freezer given the elapsed time since a power failure.
#include <stdio.h>
#include <math.h>
int main(void) {
float dec_time, // input - time in hours and minutes.
temperature, // output - temperature in degrees celsius
hours, // input for dec_time
minutes; // input for dec_time
/* Get the time in hours and minutes */
printf("hours and minutes since power failure: ");
scanf("%lf%lf", &hours &minutes);
/* Convert the time in hours and minutes into only hours in real number */
dec_time = hours + (minutes / 60.0);
// Using time via an equation to estimate the temperature
temperature = ((4 * dec_time * dec_time) / (dec_time + 2)) - 20;
// Display the temperature in degrees celsius
printf("Temperature in freezer %9.2f.\n", temperature);
return 0;
}
Any explanation anyone can give to give me insight on this would be greatly appreciated.
Edit: when I added the comma to the scanf() statement in the code on my computer, the primary compilation error in the title was resolved. I also changed the %lf to %f, but now when I key in a single digit to a.out, e.g. 3, the program does not compute until I key in q!.
Change scanf("%lf%lf", &hours &minutes) to scanf("%f%f", &hours, &minutes). No 'l', #melpomene add comma #Anton Malyshev.
Also recommend to check result to see if it is 2. (2 fields successfully scanned).
if (2 != scanf("%f%f", &hours, &minutes)) {
puts("Input error");
exit(1);
}
scanf("%lf%lf", &hours &minutes);
^ comma needed
You missed a comma ,.
Re-write as follows-
scanf("%f%f",&hours,&minutes); // make sure you use only %f and not %lf
You missed comma, that's how it should be: scanf("%lf%lf", &hours, &minutes)

C programming: I want subtract a weight per second

I am new in stackoverflow and i am sorry if i make mistakes.
I am starter in C language and i have one project what it needs to subtact a weight per second.For example
Weight: 50kg
Subtract per second 4%
i found this code
while(waitFor(1))
{
*weight=(*weight)-(*weight)*0.4;
}
void waitFor(int secs)
{
int retTime;
retTime = time(0) + secs; // Get finishing time.
while (time(0) < retTime); // Loop until it arrives.
}
But i dont want to wait x seconds to finish. I want faster solution. Any ideas
**Note: I want to know how much seconds need to make weight 0
Sleep command it is not working in my computer.
**
For cleaning and disinfecting of the pool dropped into the water of a chemical
a solid body. This solid body upon contact with water
immediately begins to dissolve, losing weight equal to 4% by mass per second.
If the dissolution rate of the chemical remains constant, to implement program
will accept the weight of the solid body in grams and displays after how
time will completely dissolve. The time is displayed in a "hours: minutes: seconds".
For example, if the dissolution time is 3,740 seconds, displaying 01: 02: 20.
To calculate the time you need to implement a function which accepts
gram and return the three time parameters ie
hours, minutes and seconds. Note that the time printed on the main
function.
you can use sleep(int) function in loop it will wait suspend the process up to the integer value.
while((1) && weight > 0)
{
sleep(1);
*weight=(*weight)-(*weight)*0.4;
}
it will wait for 1 seconds and subtraction will made, it will run continuously
Edit:
To find out the number of seconds required for the weight to reach 0:
unsigned seconds = 0;
while(*weight != 0){
*weight -= *weight * 0.04
seconds++;
//in case you have the patience to attend:
sleep(1000); //in milliseconds => 1 second
}
Please note that weight is considered to be a pointer to an integer type.

Resources