Fix the errors in the following C code. #include <stdio.h> int nofjobs; int nofm
ID: 3883981 • Letter: F
Question
Fix the errors in the following C code.
#include <stdio.h>
int nofjobs;
int nofmachines;
struct s {
int mId,duration;
};
int main() {
struct s machines[nofmachines],jobs[nofjobs];
FILE *fptr;
fptr=fopen("file.txt","rb");
fread(jobs,sizeof(jobs),1,fptr);
fread(machines,sizeof(machines),1,fptr);
for (i=0;i<nofjobs;++i) {
printf("Machine ID: %d duration: %d",jobs[i].mId,jobs[i].);
}
fclose(fptr);
}
// s1 -> machine 1
//s2->machine 2 , a[i]-> job[i] ,time-> time consumed,jobs sorted in descending order
// allocated one by one to the machine which is free.
long long ans=INT_MAX;
sort(a,a+n);
reverse(a,a+n);
int i=2;
int s1=machines[0];
int s2=machines[1];
long long time=min(s1,s2);
s1-=time;
s2-=time;
while(i<n)
{
if(s1==0 && s2==0)
{
s1=a[i];
if(i+1<n) s2=a[i+1];
int c=min(s1,s2);
time+=c;
s1-=c;
s2-=c;
i+=2;
continue;
}
else
{
if(s1<s2) swap(s1,s2);
s2=a[i];
int c=min(s1,s2);
time+=c;
s1-=c;
s2-=c;
i++;
}
}
assert(s1*s2==0);
ans = min(ans,time+max(s1,s2));
Explanation / Answer
at 13 line i is undeclared first use i in function
at 14 line exxpected identifier before the')' token
at 21 line the error is'INT_MAX' is undeclared
here (not in a function)
long long ans=INT_MAX;
at 22 line the error: expected ')' before '+' token
sort(a,a+n);
at 23 line the error: expected ')' before '+' token
reverse(a,a+n);
at 25 line the error is 'machines' undeclared here (not in a function)
int s1=machines[0];
at 27 line the warning: implicit declaration of function 'min' [-Wimplicit-function-declaration]
long long time=min(s1,s2);
at 28 line the error is expected '=', ',', ';', 'asm' or '__attribute__' before
'-=' token
s1-=time;
at 29 error: expected '=', ',', ';', 'asm' or '__attribute__' before '-=' token
s2-=time;
at 30 error: expected identifier or '(' before 'while'
while(i<n)
at 54 error: expected declaration specifiers or '...' before 's1'
assert(s1*s2==0);
at 55 warning: data definition has no type or storage class
ans = min(ans,time+max(s1,s2));
at 55:1: error: conflicting types for 'ans'
at 55:11: note: previous definition of 'ans' was here
long long ans=INT_MAX;
at 55:20: warning: implicit declaration of function 'max' [-Wimplicit-function-declaration]
ans = min(ans,time+max(s1,s2));
at the line 55:7: error: initializer element is not constant
ans = min(ans,time+max(s1,s2));
at the end of the program close the program by using} paranthesis.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.