What is wrong with the following program?

This is a discussion on What is wrong with the following program? within the C Programming forums, part of the General Programming Boards category; I am unable to understand why the program doesnt compile? [insert] Code: #include <stdio.h> #include <stdlib.h> int main(int argc , ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    What is wrong with the following program?

    I am unable to understand why the program doesnt compile?

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc , char * argv[])
    {
    	int n, i, j;
    	//int []a;
    	//n = atoi(argv[1]);
    
    	int a[10];
    
    	for(i = 0;i<10;i++)
    	{
    		int temp = random(10);
    		printf("\n%d", temp);
    		a[i] = temp;
    	}
    
    	for(i = 0; i<10 ; i++){
    		for(j = 0; j<10 ;j++){
    			if(a[j] > a[j+1]){
    				int t = a[j];
    				a[j] = a[j+1];
    				a[j+1] = t;
    			}
    		}
    	}
    
    	printf("\n Array in ascending order");
    	for(i = 0; i< 10;i++){
    		printf("\n %d", a[i]);
    	}
    	return 0;
    
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    roann - please post the error messages!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    What is : random(10) ?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Sorry for that

    The error message is

    [insert]
    Code:
    1>sort.obj : error LNK2019: unresolved external symbol _random referenced in function _main
    1>C:\Documents and Settings\ROHAN\My Documents\Visual Studio 2008\Projects\classc2\Debug\classc2.exe : fatal error LNK1120: 1 unresolved externals
    1>Build log was saved at "file://c:\Documents and Settings\ROHAN\My Documents\Visual Studio 2008\Projects\classc2\classc2\Debug\BuildLog.htm"
    1>classc2 - 2 error(s), 2 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by rdrast View Post
    What is : random(10) ?
    This is how i saw in one of the links to generate the random numbers.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    rand(10)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    This is how i saw in one of the links to generate the random numbers.
    There's nothing like random. It's rand() which produces random numbers. Your sorting function looks somewhat strange. Why you've used 'i' for loop if you're not using i as an index?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Oh thanks i got the point but then this link told me to use it in the form of

    random(10);

    http://www.phanderson.com/C/random.html

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Code:
    for(i = 0; i<10 ; i++){
    		for(j = 0; j<10 ;j++){
    			if(a[j] > a[j+1]){
    				int t = a[j];
    				a[j] = a[j+1];
    				a[j+1] = t;
    When j=9, then the comparison a[9]>a[10] will be made and you know that it's out of bound.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by BEN10 View Post
    There's nothing like random. It's rand() which produces random numbers. Your sorting function looks somewhat strange. Why you've used 'i' for loop if you're not using i as an index?
    Oh yes i got the point for using the random. The thing is that its a bubble sort algorithm so i have to iterate two times.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by roaan View Post
    Oh thanks i got the point but then this link told me to use it in the form of

    random(10);

    C Programming - Random Numbers
    Any time you see a webpage that says how TurboC does things, you shouldn't actually read any farther.

    (As an aside, *nix has a random(), but not a randomize(), as well.)

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The prototype for rand() is:

    rand(void);

    Meaning you cannot provide a meaningful argument. rand() returns a number from 0 to RAND_MAX. So you can do this for 0-9:

    Code:
    int maxfactor = RAND_MAX/10;
    int val = rand()/maxfactor;
    or use "rand()%10" which will also work but probably is very slightly weighted against 9.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by MK27 View Post
    The prototype for rand() is:

    rand(void);

    Meaning you cannot provide a meaningful argument. rand() returns a number from 0 to RAND_MAX. So you can do this for 0-9:

    Code:
    int maxfactor = RAND_MAX/10;
    int val = rand()/maxfactor;
    or use "rand()%10" which will also work but probably is very slightly weighted against 9.
    Okay i am using rand()%10 but then i realized you say its slightly weighted against 9. So what does that imply -> occurence of 9 would be less/ more as compared to other numbers.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    Okay i am using rand()%10 but then i realized you say its slightly weighted against 9. So what does that imply -> occurence of 9 would be less/ more as compared to other numbers.
    Well, say RAND_MAX is 17 (obviously it is much more than that, but it is still not a nice even number). Using rand()%10, we could get: 0, 1, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7. Ie, getting 8 or 9 is half as likely as 0-7.

    But since RAND_MAX is actually in the billions, the ratio will not be "half as likely". It will be more like 0-7 occur one hundred million and one times whereas 8 and 9 only occur one hundred million times.

    So not much need to worry, and anyway I don't think there is any option which precludes this potential imbalance, it is just as applicable with the /maxfactor method, except that will only bias the last number. At least I think so, it's not totally clear to me without a paper and pen. But you get the point?
    Last edited by MK27; 08-27-2009 at 12:41 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by MK27
    anyway I don't think there is any option which precludes this potential imbalance
    One way is to reduce the range, perhaps by discarding numbers at the end if they are generated, such that the number of integers in the range is perfectly divisble by 10.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 01:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 04:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21