I have created a C Program to simulate the Non-Preemptive Shortest Job First Algorithm but it has bugs with certain inputs. The shortest job first algorithm program takes in inputs for the arrival and burst times of the required number of processes and arranges the processes in 2 phases. The first phase involves arranging the program by arrival times and the 2nd phase arranges them by burst times given that their arrival times are lower than the time for the previous process to complete. This is all then compiled in the end and shown.
#include <stdio.h>
// n - total processes
// p - process no. array
// bt - burst time array
// at - arrival time array
// wt - the time taken for the process to start from it's arrival time array
// tat - time spent by process in cpu array
int i, n, j, m, min, sum = 0, x = 1, btTally = 0, p[20], bt[20], at[20], wt[20], tat[20], ta = 0;
float tatTally = 0, wtTally = 0;
//function grabs arrival and burst times of each process and stores it in its respective array
void getInput(){
printf("\nEnter the total number of processes: ");
scanf("%d", & n);
// For Loop for user to input info about the processes
for (i = 0; i < n; i++) {
p[i] = i + 1;
printf("\nEnter the arrival time of process %d: ", p[i]);
scanf(" %d", & at[i]);
printf("\nEnter the burst time of process %d: ", p[i]);
scanf(" %d", & bt[i]);
}
}
//Function arranges processes according to their arrival times
void arrangePhase1(){
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (at[j] > at[i]) {
m = p[j];
p[j] = p[i];
p[i] = m;
m = at[j];
at[j] = at[i];
at[i] = m;
m = bt[j];
bt[j] = bt[i];
bt[i] = m;
}
}
}
}
//Function arranges the processes according to Burst time
void arrangePhase2(){
for (i = 0; i < n; i++) {
btTally = btTally + bt[i];
min = bt[x];
for (j = x; j < n; j++) {
if (bt[j] < min && btTally >= at[j]) {
m = p[x];
p[x] = p[j];
p[j] = m;
m = at[x];
at[x] = at[j];
at[j] = m;
m = bt[x];
bt[x] = bt[j];
bt[j] = m;
}
}
x++;
}
}
//Function calculates the tallies of turnaround time and waiting time
void calcTallies(){
for (i = 0; i < n; i++) {
ta = ta + bt[i];
tat[i] = ta - at[i];
tatTally = tatTally + tat[i];
}
wt[0] = 0;
for (i = 1; i < n; i++) {
sum = sum + bt[i - 1];
wt[i] = sum - at[i];
wtTally = wtTally + wt[i];
}
}
//Function displays all of the information about the algorithm running
void showFinal(){
printf("\nProcess\t Arrival Time\t Burst Time\t Waiting Time\t Turnaround Time");
for (i = 0; i < n; i++) {
printf("\n p%d\t %d\t\t %d\t\t %d\t\t %d", p[i], at[i], bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time: %.2f", (wtTally / n));
printf("\nAverage Turn Around Time: %.2f", (tatTally / n));
}
int main() {
getInput();
arrangePhase1();
arrangePhase2();
arrangePhase2();
calcTallies();
showFinal();
return 0;
}
These are the expected results:
These are the results I get with my programme:
Any help would really be appreciated. Thanks!
I ... rearrange your program a bit ...
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int p, at, bt, wt, tat;
} Job;
#define maxN (20)
Job jobs[maxN ];
int n, btTally = 0, sum = 0, ta = 0;
float tatTally = 0, wtTally = 0;
#define TestSize (5)
Job Test[TestSize] = {
{ 1, 2, 1, 0, 0 },
{ 2, 1, 5, 0, 0 },
{ 3, 4, 1, 0, 0 },
{ 4, 0, 6, 0, 0 },
{ 5, 2, 3, 0, 0 }
};
void getInput(){
int i;
printf("\nEnter the total number of processes: ");
scanf("%d", & n);
if (n == 0) {
for (i = 0; i < TestSize; ++i)
jobs[i] = Test[i];
n = TestSize;
return;
}
// For Loop for user to input info about the processes
for (i = 0; i < n; i++) {
jobs[i].p = i + 1;
printf("\nEnter the arrival time of process %d: ", jobs[i].p);
scanf(" %d", & jobs[i].at);
printf("\nEnter the burst time of process %d: ", jobs[i].p);
scanf(" %d", & jobs[i].bt);
}
}
int compareAT (const void * a, const void * b) {
Job *jobA = (Job *)a;
Job *jobB = (Job *)b;
return ( jobA->at - jobB->at );
}
void arrangePhase1(){
qsort (jobs, n, sizeof(Job), compareAT);
}
void Swap(int i, int j) {
Job m = jobs[i];
jobs[i] = jobs[j];
jobs[j] = m;
}
void calcTallies(){
int i;
for (i = 0; i < n; i++) {
ta = ta + jobs[i].bt;
jobs[i].tat = ta - jobs[i].at;
tatTally = tatTally + jobs[i].tat;
}
jobs[0].wt = 0;
for (i = 1; i < n; i++) {
sum = sum + jobs[i - 1].bt;
jobs[i].wt = sum - jobs[i].at;
wtTally = wtTally + jobs[i].wt;
}
}
void showFinal(){
int i;
printf("\nProcess\t Arrival Time\t Burst Time\t Waiting Time\t Turnaround Time");
for (i = 0; i < n; i++) {
printf("\n p%d\t %d\t\t %d\t\t %d\t\t %d", jobs[i].p, jobs[i].at, jobs[i].bt, jobs[i].wt, jobs[i].tat);
}
printf("\nAverage Waiting Time: %.2f", (wtTally / n));
printf("\nAverage Turn Around Time: %.2f", (tatTally / n));
}
void arrangePhase2() {
int i, j, min, x = 1;
for (i = 0; i < n; i++) {
btTally = btTally + jobs[i].bt;
min = jobs[x].bt;
for (j = x; j < n; j++) {
if (jobs[j].bt < min && btTally >= jobs[j].at) {
Swap(x, j);
min = jobs[x].bt;
showFinal();
}
}
x++;
}
}
int main() {
getInput();
arrangePhase1();
showFinal();
arrangePhase2();
// arrangePhase2();
calcTallies();
showFinal();
return 0;
}
And left some test and prints in it.
The main problem is that the min is not updated. Try to add the update to your original program and see if it works. Next is to move the int i,j; to the functions, does it work now? (it wont work without this if you just add the showFinal(); test to your code).
Now the code I made is not the one true truth, it is just an example from someone who hasn't programmed C in decades.
Short code review.
Using globals are bad in C, you easily gets a problem with your loops if you use globals for loop control, here i,j is reused a lot.
You could use structs more, at least for the 3 main values, makes some of the sorting a bit easier. In either case having a Swap function makes the code easier to check and understand.
void SwapInt(int *i, int *j) {
int m = *i;
*i = *j;
*j = m;
}
SwapInt(&at[i], &at[j]); // example of calling swapping
The 2nd call to arrangePhase2(); doesn't do anything.
Related
I'm currently working on an essay, in which I compare sorting times of certain algorithms. I've written an optimized version of bubble sort, which checks if there is a swap, if not, it stops.
void bubble_sort(int* tab, int n) {
int zamiana, x, i;
do {
zamiana = 0;
for (int counter = 0, i = 1; i < n; ++counter, ++i) {
if (tab[counter] > tab[i]) {
x = tab[counter];
tab[counter] = tab[i];
tab[i] = x;
zamiana = 1;
}
}
} while (zamiana != 0);
}
I have found that it takes almost 0s to sort array sorted in ascending order, now I'm testing it on an array sorted in descending order, and times are almost the same as for ascending order. Is it normal?
Code tested:
#include <time.h>
#include <stdio.h>
void quickSort(int* tab, int lewy, int prawy) {
int x, y = lewy - 1, z = prawy + 1, pivot = tab[(lewy + prawy) / 2];
while (1) {
while (pivot < tab[++y]);
while (pivot > tab[--z]);
if (y <= z) {
x = tab[y];
tab[y] = tab[z];
tab[z] = x;
}
else {
break;
}
}
if (z > lewy) {
quickSort(tab, lewy, z);
}
if (y < prawy) {
quickSort(tab, y, prawy);
}
}
void bubble_sort(int* tab, int n) {
int zamiana, x, i;
do {
zamiana = 0;
for (int counter = 0, i = 1; i < n; ++counter, ++i) {
if (tab[counter] > tab[i]) {
x = tab[counter];
tab[counter] = tab[i];
tab[i] = x;
zamiana = 1;
}
}
} while (zamiana != 0);
}
int main (){
int* tab;
int n;
srand(time(NULL));
scanf("%d", &n); //user input array size
tab = (int*)malloc(n * sizeof(int*));
for (int counter = 0; counter < n; ++counter) {
tab[counter] = (rand() % 200) - 100; //<-100;100>
}
quickSort(tab, 0, n); //sorting array to get descending order
clock_t start = clock();
bubble_sort(tab, n); //sorting array
clock_t end = clock();
float seconds = (float)(end - start) / CLOCKS_PER_SEC;
printf("Time elapsed: %f", seconds);
}
I have to write a C program that multiplies numbers from 1 to N.
N is scanned. Before multiplication, I have to increase each number by 2.
For example: N = 3 => (1+2)(2+2)(3+2) = 60
I have to only use while loop and print and scan function.
Example program:
Enter the value of N: 4
The result of multiplication: 360
This is my code and I am not sure what is wrong with this. Please help.
#include <stdio.h>
int N;
int count=1, ii, result;
printf("Enter the value of N:");
scanf("%d", &N);
while (count<=N)
{
count ii = count + 2;
ii = ii * ii ; //three
count++;
}
result = ii;
printf("The result of multiplication: %d", result);
return 0;
}
If you're looking for that series as a sum:
const int N = 3;
int c = 1;
for (int i = 1; i <= N; ++i) {
c *= (i + 2);
}
Or in a more C-esque form:
const int N = 3;
int c = 1;
for (int i = 0; i < N; ++i) {
c *= (i + 1 + 2);
}
int main()
{
int N;
int count=1, ii = 1, result;
printf("Enter the value of N:");
scanf("%d", &N);
while (count<=N)
{
ii = ii * ( count + 2 };
count++;
}
result = ii;
printf("The result of multiplication: %d", result);
return 0;
}
This is the given condition and I have problem with setting time interval
Find prime numbers from 2 in 5 minutes:
include <time.h>
end= start = (unsigned)time(NULL);
while((end-start)<300)
find primes
print the prime number and how many primes are there(frequency)
end = (unsigned)time(NULL);
print total execution time
And this is the code I did.
#include <stdio.h>
#include <time.h>
int main()
{
time_t end, start;
end = start = (unsigned)time(NULL);
int i, j;
int freq = 0;
int count = 0;
while ((end-start) < 300) {
for (i = 2;; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
count = 1;
break;
}
}
if (!count) {
++freq;
}
count = 0;
}
return i, freq;
printf("Prime number: %d, frequency: %d\n", i, freq);
}
end = (unsigned)time(NULL);
printf("total time : %d seconds\n", end);
return 0;
}
I checked the finding prime part and the frequency part was fine when I put this way`
}
count = 0;
printf("Prime number: %d, frequency: %d\n", i, freq);
}
return i, freq;`
but there's no result even after the 5 minutes
Your while cycle ends when according to the value of end and start 5 minutes passed. They are initialized with the same value.
end is never updated inside the loop, so this will be an infinite cycle. You update end after the loop, but that is too late. You need to update it at each step, like:
while ((end-start) < 300) {
for (i = 2;(end-start) < 300; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
count = 1;
break;
}
}
if (!count) {
++freq;
}
count = 0;
end = (unsigned)time(NULL); printf("Prime number: %d, frequency: %d\n", i, freq);
}
//Don't return at this point
//return i, freq;
}
there's a mistake in my code but i can't find where. it must calculate the averages after reading inputs.
it reads all the code and gets all the inputs but when it comes to calculating the average it does nothing. help please
#include <stdlib.h>
#include <stdio.h>
int main() {
int students, modules, m, n, first_student, last_student, l = 0;
float student[100][20], high = 0, low = 20, average[students], average_mark[students];
printf("Please enter the number of students:\n");
scanf("%d", &students);
printf("Please enter the number of modules:\n");
scanf("%d", &modules);
for (m = 0; m < students; ++m) {
for (n = 0; n < modules; n++) {
printf("Please enter the mark of module %d for student number %d,\n", n + 1, m + 1);
scanf("%f", &student[m][n]);
}
}
for (m = 0; m < students; ++m) {
average[m] = 0;
}
for (n = 0; n < modules; n++) {
average[m] += student[m][n];
average_mark[m] = average[m] / modules;
}
printf("student average\n");
for (m = 0; m < students; ++m) {
printf("%d %f\n", m + 1, average_mark[m]);
}
for (m = 0; m < students; ++m) {
if (average_mark[m] < low) {
average_mark[m] = low;
}
else
if (average_mark[m] == low) {
last_student = m + 1;
}
if (average_mark[m] > high) {
average_mark[m] = high;
}
else
if (average_mark[m] == high) {
first_student = m + 1;
}
}
printf("The student who had the highest mark is %d : %f\n", first_student, high);
printf("The student who had the lowest mark is %d : %f\n", last_student, low);
for (m = 0; m < students; ++m) {
if (average_mark[m] == 10 || average_mark[m] > 10) {
l++;
}
}
printf("the number of students having a mark that equals or exceeds the average is %d\n", l);
return 0;
}
There are some other problems in the code but as a start, you may take a peek below.
edit: The next (2nd) problem noted & corrected. I'm leaving you to find & correct the last (3rd) problem.
// either these values need to be predefined
// or you'll need to dynamically allocate
// arrays using malloc
int MAX_STUDENTS = 100;
int MAX_MODULES = 20;
int main() {
int students, modules, m, n, first_student, last_student, l = 0;
float student[MAX_STUDENTS][MAX_MODULES], high = 0, low = 20, average[MAX_STUDENTS], average_mark[MAX_MODULES];
...
...
...
// the 2nd & 3rd for loops needs to be nested as below
for (m = 0; m < students; ++m) { // this was 2nd
average[m] = 0;
for (n = 0; n < modules; n++) { // this was 3rd
average[m] += student[m][n];
}
// you need to move this line out of 3rd loop
average_mark[m] = average[m] / modules;
}
...
...
...
// this is how you find the lowest & highest ranking students
for (m = 0; m < students; ++m) {
if (average_mark[m] < low) {
low = average_mark[m];
last_student = m;
}
if (average_mark[m] > high) {
high = average_mark[m];
first_student = m;
}
}
printf("The student who had the highest mark is %d : %f\n", first_student + 1, high);
printf("The student who had the lowest mark is %d : %f\n", last_student + 1, low);
...
...
...
}
we are trying to achieve round robin algorithm using linked list.
But My logic has some errors.
When I try to run it for 3 process then the first process values are wrong and sometimes right for the other below processes.
Please Help me.
I tried Searching for logics
Code Link: https://pastebin.com/FkbtUEaQ
#include<stdio.h>
struct process
{
char na[20];
int at, bt, ft, tat, rem;
//float ntat;
} Q[5], temp;
void roundRobin()
{
int rr[20], q, x, k;
int f, r, n, i, j, tt = 0, qt, t, flag, wt = 0;
float awt = 0, antat = 0, atat = 0;
printf("Enter the no. of jobs:");
scanf("%d", &n);
for (r = 0; r < n; r++)// aceppting arrival; and burst time
{
printf("Enter process name,arrival time and burst time:\n");
scanf("%s%d%d", Q[r].na, &Q[r].at, &Q[r].bt);
}
printf("Enter quantum:\n");
scanf("%d", &qt);
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (Q[i].at < Q[j].at) {
temp = Q[i];
Q[i] = Q[j];
Q[j] = temp;
}
}
}
for (i = 0; i < n; i++)
{
Q[i].rem = Q[i].bt;
Q[i].ft = 0;
}
tt = 0;
q = 0;
rr[q] = 0;
do
{
for (j = 0; j < n; j++)
if (tt >= Q[j].at)
{
x = 0;
for (k = 0; k <= q; k++)
if (rr[k] == j)
x++;
if (x == 0)
{
q++;
rr[q] = j;
}
}
if (q == 0)
i = 0;
if (Q[i].rem == 0)
i++;
if (i > q)
i = (i - 1) % q;
if (i <= q)
{
if (Q[i].rem > 0)
{
if (Q[i].rem < qt)
{
tt += Q[i].rem;
Q[i].rem = 0;
} else
{
tt += qt;
Q[i].rem -= qt;
}
Q[i].ft = tt;
}
i++;
}
flag = 0;
for (j = 0; j < n; j++)
if (Q[j].rem > 0)
flag++;
} while (flag != 0);
printf("\n\n\t\tROUND ROBIN ALGORITHM");
printf("\n***************************");
printf("\nprocesses Arrival time burst time finish time tat wt ntat");
for (f = 0; f < n; f++) {
wt = Q[f].ft - Q[f].bt - Q[f].at;
Q[f].tat = Q[f].ft - Q[f].at;
Q[f].ntat = (float) Q[f].tat / Q[f].bt;
antat += Q[f].ntat;
atat += Q[f].tat;
awt += wt;
printf("\n\t%s\t%d\t%d\t%d\t%d\t%d %f", Q[f].na, Q[f].at, Q[f].bt,
Q[f].ft, Q[f].tat, wt, Q[f].ntat);
}
antat /= n;
atat /= n;
awt /= n;
printf("\nAverage tat is %f", atat);
printf("\nAverage normalised tat is %f", antat);
printf("\n average waiting time is %f", awt);
}
void main()
{
roundRobin();
getch();
clrscr();
}
The First Process Gives Wrong Values
processes | ArrivalTime | BurstTime | FinishTime | Tat | WaitTime
a 0 10 60 60 50
b 0 20 30 30 10
c 0 30 50 50 20
In the do while loop you use i to index the rr array with valid indexes 0 to q as well as the Q array with valid indexes 0 to n − 1, of which only the former is correct. So, you have to change every occurrence of Q[i] in this loop to Q[rr[i]].
After that, still the order of the statements
if (Q[rr[i]].rem == 0)
i++;
if (i > q)
i = (i - 1) % q;
is wrong - the test for Q[rr[i]].rem == 0 is to be done each time a new i is chosen, e. g.:
while (Q[rr[i %= q+1]].rem == 0) i++;