Thread: Small error in C implementing fCfS algo

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    15

    Small error in C implementing fCfS algo

    I m trying to implement fCfS Algo.
    The programm is not ful fledged complete.
    But the programm ends abruptly after the first input

    So pls can anyone help me whats wrong ?

    Code:
    #include <stdio.h>
    #include <conio.h>
    int nopr,art[50],jt[50],start,wt[50],i,j,k,l,m;
    void fcfs ()
    {
    	//int i,j,k,l,art[50],jt[50],start,wt[50];
    	printf ("\n Enter arrival Time in ascending order\n");
    	for (i=0;i<=nopr;i++)
    	{
    		scanf ("%d", art[i]);
    	}
    	printf ("\n Enter job Time\n");
    	for (j=0;j<=nopr;j++)
    	{
    		scanf ("%d", jt[j]);
    	}
    	for (k=1;k<=nopr;k++)
    	{
    		start=art[i]+jt[j];
    		wt[l]=start-art[i];
    	}
    	printf ("fCfS Algo");
    	printf ("The waiting Time for each process is :\n");
    	for (m=0;m<=nopr;m++)
    	{
    		printf ("%d",wt[l]);
    	}
    
    }
    void main ()
    {
    	//int nopr,art[50],jt[50],start,wt[50],i,j,k,l;
    	printf  ("\n\n------------- Scheduling Algorithm fCfS -------------\n\n");
    	printf ("Enter no of process \n");
    	scanf ("%d\n",&nopr);
    	fcfs ();
    }

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    What value do you read in in the first loop and where do you put it?

    HINT: TURN ON YOUR DAMN COMPILER WARNINGS.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Death to void main()! May it be blasted in the depths of hell!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    15
    Quote Originally Posted by ledow View Post
    What value do you read in in the first loop and where do you put it?

    HINT: TURN ON YOUR DAMN COMPILER WARNINGS.


    i Dont know how to turn on the compiler warning.
    i m using MinGW Compiler.

    What value are u talking about.
    See this is the image where the error occursSmall error in C implementing fCfS algo-fcfs-png

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Addresses need to be passed to scanf(), not values. You need to add ampersands. Without those ampersands, your scanf() calls in fcfs() are writing to random areas of memory.

    Also a loop of the form for (i = 0; i <= nopr; i++) loops nopr+1 times.

    And claudiu is right: main() returns int, not void.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    See the bit where you typed in "gcc fCfs.C -o fCfS"? Put a -Wall into it:

    Code:
    gcc -Wall -Werror fCfs.C -o fCfS
    would be my recommendation.

    Then fix everything it tells you about.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    15
    Quote Originally Posted by grumpy View Post
    Addresses need to be passed to scanf(), not values. You need to add ampersands. Without those ampersands, your scanf() calls in fcfs() are writing to random areas of memory.

    Also a loop of the form for (i = 0; i <= nopr; i++) loops nopr+1 times.

    And claudiu is right: main() returns int, not void.


    yea i know i need to add ampersands in scanf.
    But do i need to do taht in arrays too ?
    'coz i have learnt that in arrays it directly points to addres so no need to add ampersands.

    & so can u pls tell me whats the correct loop ?

    & i have corrected that 'coz my compiler always gives error with void in main to change to int().
    Can u pls explain me that part, i dont seem to understand it

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    15
    Quote Originally Posted by ledow View Post
    See the bit where you typed in "gcc fCfs.C -o fCfS"? Put a -Wall into it:

    Code:
    gcc -Wall -Werror fCfs.C -o fCfS
    would be my recommendation.

    Then fix everything it tells you about.


    hey thanks man
    But now its telling me this :Small error in C implementing fCfS algo-dharmil014-jpg

    int & int are same so why error ?

  9. #9
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    That's not what it says at all. It says that "int" is not the same as "int *" (a pointer to an int, which an asterisk in it!).

    And that's PRECISELY the cause of your problems.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Cross-posted here, here, and here.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    15
    Thanks People Just by adding an ampersand "&", it solved the issue.
    But in arrays it directly points to the address, so there is no need to add an ampersand, RIGHT ???
    So why this error ?

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    15
    Quote Originally Posted by grumpy View Post
    Also a loop of the form for (i = 0; i <= nopr; i++) loops nopr+1 times.
    Hey thanks.
    Figured it out.
    Removed teh equal sign.

  13. #13
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by dharmil007 View Post
    Thanks People Just by adding an ampersand "&", it solved the issue.
    But in arrays it directly points to the address, so there is no need to add an ampersand, RIGHT ???
    So why this error ?
    I advise you to read up on arrays better, because you're getting confused. Assuming:

    Code:
    int array[10]
    Then "array" is equivalent to &array[0], yes. But array[0] is "the contents of array box 0", if you like. The square brackets ([]) are reaching into memory for you and retrieving the contents of the array. You *could* do it yourself (e.g. array + some offset) but the [] do the job for you. You CAN have arrays of pointers, too, but this ISN'T one. It's an array of int's. So the 0th element of the array is an int. When you say "array[0]", it's going to be an int. If you want a pointer to that int, the most convenient way is to use "&array[0]".

    However, the array as a whole COULD be referred to using a pointer (&array[0]) which is sometimes "shortcut" to something simpler (array) for convenience. Pointers and arrays are linked (all arrays are stored in contiguous memory and referred to by a pointer) but not in the way you think.

    Don't spend your C programming life just putting an * or & where you think things should be pointers or where you get compiler errors. Think about what they mean and what you're actually storing and referring to. Otherwise, the first time you run into something complex, you'll mess it up (e.g. I wrote a qsort comparator function lately that puts NULL pointers before others - if you don't THINK about what all the *'s and &'s mean, you mess it up. As it was, I got it to work first-time by thinking about what I wanted - and even commented the code on that fact, I was so shocked!).

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    15
    Quote Originally Posted by ledow View Post
    I advise you to read up on arrays better, because you're getting confused. Assuming:

    Code:
    int array[10]
    Then "array" is equivalent to &array[0], yes. But array[0] is "the contents of array box 0", if you like. The square brackets ([]) are reaching into memory for you and retrieving the contents of the array. You *could* do it yourself (e.g. array + some offset) but the [] do the job for you. You CAN have arrays of pointers, too, but this ISN'T one. It's an array of int's. So the 0th element of the array is an int. When you say "array[0]", it's going to be an int. If you want a pointer to that int, the most convenient way is to use "&array[0]".

    However, the array as a whole COULD be referred to using a pointer (&array[0]) which is sometimes "shortcut" to something simpler (array) for convenience. Pointers and arrays are linked (all arrays are stored in contiguous memory and referred to by a pointer) but not in the way you think.

    Don't spend your C programming life just putting an * or & where you think things should be pointers or where you get compiler errors. Think about what they mean and what you're actually storing and referring to. Otherwise, the first time you run into something complex, you'll mess it up (e.g. I wrote a qsort comparator function lately that puts NULL pointers before others - if you don't THINK about what all the *'s and &'s mean, you mess it up. As it was, I got it to work first-time by thinking about what I wanted - and even commented the code on that fact, I was so shocked!).


    Thanks man
    to get much more insight in C, what do u think what should i refer too ?
    & where would i get some practice programms ?

    i have ANSI C by Ritchie, what do u think any other refernces ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-23-2010, 12:23 AM
  2. Small Error it seems.
    By zerkz in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2009, 03:11 PM
  3. Help implementing file modifcation algo.
    By enli in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 05:00 AM
  4. error in small code
    By asd010 in forum C Programming
    Replies: 15
    Last Post: 10-11-2006, 06:08 AM
  5. small error need help
    By doodman in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2004, 07:23 PM