Can I use nested do while loop in C for this? - c

I am tasked with creating a simple program for a train schedule. I need to have a user input how many trains will be running and how many times the trains will run for the day. For each run I need to tell 1 train at a time to go and the others to standby until they have all traveled. I'm trying to use a nested do while loop to achieve this but not having much luck. Is this possible or am I going about it wrong from the start?
If the user inputs 2 trains will run 2 times my output will read:
Train 1 go. All other trains standby.
Train 2 go. All other trains standby.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int numTrains = 0;
int timeBlocks = 0;
int numRuns = 1;
int timeBlocksran = 0;
printf("How many trains will be running today?\n");
scanf("%d", &numTrains);
printf("How many times will the trains run today?\n");
scanf("%d", &timeBlocks);
printf("OK. There will be %d trains running %d times today. Let's get them started. All Aboard.\n", numTrains, timeBlocks);
do
{
timeBlocksran++;
printf("Time Block %d\n", timeBlocksran);
do
{
printf("Train %d go. All other trains standby.\n", numRuns);
numRuns++;
} while (numRuns <= numTrains);
} while (timeBlocksran <= timeBlocks);
return 0;
}
If the user inputs 2 trains will run 2 times I want my output to read:
TIME BLOCK 1
Train 1 go. All other trains standby.
Train 2 go. All other trains standby.
TIME BLOCK 2
Train 1 go. All other trains standby.
Train 2 go. All other trains standby.

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

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.

while loop for scanning user input table into array

I am a super unintuitive beginner programming student working on a homework assignment. The program is to take an input table of hockey teams and game stats, calculate "points", and then output the table organized in a different way, with additional columns for points and games played.
So far, I haven't made it past scanning the input. With my while loop, I'm trying to first determine if the string is a conference name or a team name and then scan and store the subsequent table values accordingly. I'm just trying to print the same table back out at this point, but when i copy/paste the input input table which was given, I get no output, and when I manually type it in, it comes out suuper weird.
The input table looks something like this:
Conference1_Name 3 (teams in conference)
Team_1_Name 31 15 2 2 (wins, losses, ot losses, shootout losses)
Team_2_Name 24 21 1 0
Team_3_Name 27 19 0 2
Conference2_Name 4
Team_4_Name 30 15 1 1
...
aaand this is my code so far...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main(void){
int n=0, N=25, nE=0, nW=0, i, w[n], l[n], otl[n], sol[n], gp[n], pts[n];
char input[30], team[n][30];
printf("Enter conference data\n");
setvbuf(stdout,NULL,_IONBF,0);
while(n<=N){
scanf("%s", input);
if (0==strcmp(input, "Eastern_Conference")) {
scanf("%d", &nE);
continue; //does not count eastern conference as a team, but stores number of teams, and does not increment n
}
if (0==strcmp(input, "Western_Conference")) {
scanf("%d", &nW);
continue; //does not count western conference as a team, but stores number of teams, and does not increment n
}
if (0==strcmp(input, "EOD")) break;
//originally i had a statement like: if (nE>0 && nW>0){N=nE+nW;) so that the loop condition n<N will break the loop on it's own but thought maybe EOD would simplify it and fix my problem//
strcpy(team[n], input);
scanf("%d%d%d%d", &w[n], &l[n], &otl[n], &sol[n]);
gp[n]=w[n]+l[n];
pts[n]=(2*w[n])+otl[n]+sol[n];
n++;
} //so we have input the data and calculated points
printf("\n%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "WHL", "GP", "W", "L", "OTL", "SL", "PTS");
for(i=0; i<n; i++){
printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\n", team[i], gp[i], w[i], l[i], otl[i], sol[i], pts[i]);
}
return EXIT_SUCCESS;
}

Get program to return an error message and return to start of program if value entered is outside range

I'm a complete beginner with C and am currently trying to write a program where the user can enter results from football league games and calculate the teams' scores after each game.
There are 6 teams in the league and the user is required to choose a team at the start of the program in order to input a score for that team. What I would like to do is have the program return an error message if the user enters a value that is not between 1 and 6.
I've tried two different approaches but I'm not sure if either are in the right direction. One approach is shown below for the home team.
#include <stdio.h>
int main(void)
{
int h=0; /*Home team number*/
int a=0; /*Away team number*/
int hgoals=0; /*Goals scored by home team*/
int agoals=0; /*Goals scored by away team*/
/*User inputs home team number*/
printf("Home team number: ");
scanf_s("%d",&h);
/*Returns error message if home team number is not between 1 & 6*/
if(1<!h<!6){
printf("Please enter a number between 1 & 6\n");
}
return 0;
}
The other idea I had was to use an if statement where if the number entered is between 1 & 6 then nothing would happen, and use else to print the error message if the number is not between 1 & 6, but I'm not sure how to make an if statement that does nothing. I'm also thinking that I would have to put the entire program inside a loop to get it to restart if the number is not between 1 & 6.
Any help is appreciated!
Change the condition in the if statement :
if(h < 1 || h > 6)
printf("Please enter a number between 1 and 6\n");

Really weird, debug of the program works OK but when i run it i get a weird result

i'm trying to create a 'game' in C programming which throws 2 dices for the user, throws 2 dices for the PC, and whoever gets the bigger sum wins,
when i debug it in visual studio i see good results both in the variable values and the console window, but when i run it without debugging the user and the PC both always get the same value for their dices (user gets 2 and 2, and PC gets 2 and 2, for example).
Can anyone solve it? i looked at it for the last 3 hours and i just can't find what's the problem.
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int throwDice();
int diceSum();
int main()
{
int res1;
res1=diceSum();
if(res1==0)
printf("It is a tie!\n");
if(res1==1)
printf("You Won!\n");
if(res1==-1)
printf("You Lost\n");
}
int throwDice()
{
int i;
srand((unsigned)time(NULL));
i = (rand()%(6-1)) + 1;
return i;
}
int diceSum()
{
int j,a=0,b,c=0,d=0;
int array[4];
for(j=1;j<=2;j++)
{
array[c]=throwDice();
a=a+array[c];
c++;
}
for(b=1;b<=2;b++)
{
array[c]=throwDice();
d=d+array[c];
c++;
}
printf("You got %d and %d.\nYour opponent got %d and %d.\n",array[0],array[1],array[2],array[3]);
if(a==d)
return 0;
if(a>d)
return 1;
else
return -1;
}
Everytime you call throwDice, you are re-initializing your random number generator with the current time.
The accuracy of time is only 1-second, so in a single run of this program, the time doesn't change, so you get the same results.
You are supposed to call srand ONLY ONCE, near the start of your program.
From the documentation
"Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand."
The random number generator doesn't just pull random numbers out of nowhere. When you seed it you're giving it a good number to start with, and from there it can generate a bunch of random numbers.
The problem is, it will give you a sequence of random numbers, but that sequence will be the same if you give it the same seed twice in a row. That's why it's important that the value you seed it with is relatively random. And hey, the time is pretty random, what are the chances two people will end up running the program at exactly the same time?
What you're doing, is seeding the generator before every call to rand, instead of seeding it at the beginning and letting the random number generator do its job. Since time only returns the time in seconds (see man page), you'll get the same number every time the loop runs until the start of the next second.

Resources