150 pts.
 Round Robin
Can anyone check what the mistake is:

#include #include void main() { int st[10],bt[10],wt[10],tat[10],n,tq; int i,count=0,swt=0,stat=0,temp,sq=0; float awt=0.0,atat=0.0; clrscr(); cout<<"Enter number of processes:"; cin>>"%d",&n; cout<<"Enter burst time for sequences:"; for(i=0;i>"%d",&bt[i]; st[i]=bt[i]; } cout<<"Enter time quantum:"; cin>>"%d",&tq; while(1) { for(i=0,count=0;itq) st[i]=st[i]-tq; else if(st[i]>=0) { temp=st[i]; st[i]=0; } sq=sq+temp; tat[i]=sq; } if(n==count) break; } for(i=0;i



Software/Hardware used:
ASKED: December 19, 2008  2:14 PM
UPDATED: June 11, 2013  3:16 PM

Answer Wiki:
It would help if you described the mistake in a bit more detail -- what's the problem you're having, specifically? With not-too-descriptive variable names like "st", it's not very easy to see at a glance what this code is even trying to do. That said, I can see a few fairly glaring problems. First is that you never check the value of n after input, but you use it for array indexes. What happens if I enter "20" as n? Now you'll be reading and writing bt[19], which is out of the array's bounds and is thus random memory. Writing to random memory is never a good thing and can crash your program or, worse, provide a backdoor for hackers to spread malware. Also, remember that array values (like everything else in C) are not automatically initialized. Consider tat[]. There are indexes of tat[i] that you never set value to (if st[i]==0, then tat[i] never gets assigned), so those will have random data. Another bug is in your if/else statement. C uses only curly brackets for control -- indentation doesn't matter. So in
<pre>else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;</pre>
Only that first "if" statement is under the else. The next two statements are always hit. What you want is:
<pre>else
{
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}</pre>
You should also throw brackets around your other "if" statements; it's good habit to put in brackets even for one-liners, because otherwise you're setting yourself up for a mistake like the one in this "else" block. Same with the for loops, etc. By the way, your "while" is a bit backwards. Instead of "while(1) { .... if(n==count) break; }" you should consider using the more standard "do { ... } while (n==count);" There are also a few times when you do things like "stat = stat+tat[i];" This can be abbreviated to "stat += tat[i];" Your way won't cause bugs, it's just not as concise. And finally, as a formatting note, your couts never have newlines, so all that output will be on one line. You can get newlines with something like: <pre> cout<<"Process_no Burst time Wait time Turn around time" << endl;</pre> Btw, does this code compile? cout and cin are part of the std namespace, which you never bring in. If the program isn't compiling because it can't find cout and cin, type at the top (right after the includes) "use namespace std;" HTH
Last Wiki Answer Submitted:  June 11, 2013  3:17 pm  by  Michael Tidmarsh   14,060 pts.
All Answer Wiki Contributors:  Michael Tidmarsh   14,060 pts. , YuvalShavit   905 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

- cin and cout won’t cause problems, since <iostream.h> was included.
- if you want to use the std namespace, it should be “using namespace std” (not “use”)
- clrscr won’t compile (include stdlib.h and use system(“cls”) instead).
- remove the format modifiers in cin calls (i.e. use “cin >> n;” instead of “cin>>”%d”,&n;”) as they will rise memory exceptions
- you cannot use format modifiers in cout as you would do with printf
- Describe what your problem is, so more an better help can be provided.

 63,580 pts.