Thread: Help plz

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Bangladesh
    Posts
    7

    Post Help plz

    I am learning C for some days and I am facing many problems.I have asked for random sorting before and I got some solutions from here but by that time I came up with this code.
    I have been using this code and its ok.. but

    Code:
    int main()
    
    {
      
       printf ("Brazil	 vs	Argentina");
       int a,a1,a2,a3,a4,a5,a6,a7,a8,a9;
       time_t t;
       
       srand((unsigned) time(&t));
       for (int i=0;i<100;i++)
    	 {
    	 a= rand() % 11;
    	 if (a== 0)
    	 continue;
    	 else
    	 break;
    	 }
       a= rand() % 11;
    
       for (int i=0;i<100;i++)
    	 {
    	 a1= rand() % 11;
    	 if (a1 == 0 || a1==a)
    	 continue;
    	 else
    	 break;
    	 }
       for (int i=0;i<100;i++)
    	 {
    	 a2= rand()% 11;
    	 if (a2== 0 || a2==a || a2==a1 )
    	 continue;
    	 else
    	 break;
    	 }
    
       for (int i=0;i<100;i++)
    	 {
    	 a3= rand()% 11;
    	 if (a3== 0 ||a3==a || a3==a1 || a3==a2 )
    	 continue;
    	 else
    	 break;
    	 }
    
         for (int i=0;i<100;i++)
    	 {
    	 a4= rand()% 11;
    	 if (a4==0 || a4==a || a4==a1 || a4==a2 || a4==a3 )
    	 continue;
    	 else
    	 break;
    	 }
    
         for (int i=0;i<100;i++)
    	 {
    	 a5= rand()% 11;
    	 if (a5== 0 || a5==a || a5==a1 || a5==a2 || a5==a3 || a5==a4 )
    	 continue;
    	 else
    	 break;
    	 }
           for (int i=0;i<100;i++)
    	 {
    	 a6= rand()% 11;
    	 if (a6== 0 ||a6==a || a6==a1 || a6==a2 || a6==a3 || a6==a4 ||a6==a5)
    	 continue;
    	 else
    	 break;
    	 }
          for (int i=0;i<100;i++)
    	 {
    	 a7= rand()% 11;
    	 if (a7== 0 || a7==a || a7==a1 || a7==a2 || a7==a3 || a7==a4 || a7==a5 || a7==a6)
    	 continue;
    	 else
    	 break;
    	 }
        for (int i=0;i<100;i++)
    	 {
    	 a8= rand()% 11;
    	 if (a8 == 0 || a8==a || a8==a1 || a8==a2 || a8==a3 || a8==a4 || a8==a5 || a8==a6|| a8==a7 )
    	 continue;
    	 else
    	 break;
    	 }
    
         for (int i=0;i<100;i++)
    	 {
    	 a9= rand()% 11;
    	 if (a9==0 ||a9==a || a9==a1 || a9==a2 || a9==a3 || a9==a4 || a9==a5 || a9==a6|| a9==a7||a9==a8)
    	 continue;
    	 else
    	 break;
    	 }
       printf ("       \n%d		 vs 	     %d",a,a1);
       printf ("       \n%d		 vs	     %d",a2,a3);
       printf ("	   \n%d		 vs	     %d",a4,a5);
       printf ("	   \n%d		 vs	     %d",a6,a7);
       printf ("	   \n%d		 vs	     %d",a8,a9);
    
       getchar();
       getchar();
       return 0;
    }
    the output is like


    Brazil vs Argentina
    4 vs 10
    2 vs 8
    3 vs 9
    1 vs 5
    7 vs 6



    It is giving random numbers as I wanted. But what if I want names instead of those numbers?? How can I do that?
    like

    Jim vs Rock
    Tommy vs Chris
    X vs y


    . please help. And please explain how to do it. I want to understand it clearly.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Exorcist
    I am learning C for some days and I am facing many problems.I have asked for random sorting before and I got some solutions from here but by that time I came up with this code.
    I have been using this code and its ok.. but
    It would be good if you stated what the program was supposed to do.

    Quote Originally Posted by sufoode
    Solutions:
    I applaud Exorcist's aim to actually learn:
    Quote Originally Posted by Exorcist
    And please explain how to do it. I want to understand it clearly.
    As for you, refer to my reply to you in this thread.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sufoode View Post
    the code is self explanatory
    The code is self-explanatorily NOT EVEN VALID C CODE is what you mean...
    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

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Exorcist View Post
    But what if I want names instead of those numbers?? How can I do that?
    Make an array of strings:
    Code:
    char *strings[] = {"bob", "turquoise", "salad"};
    You can refer to this using subscripting, eg, strings[1] is "turquoise", strings[1][0] is "t".
    Now you can use random numbers to select a string.
    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

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    sufoode, I imagine youre just trying to help, which is what all of us here want to do because we enjoy helping others. However, if you just give an answer/solution and thats it--no explanation, comments in the code, etc--then you probably arent actually helping the person to learn. As I said, hopefully your intention is to help the person learn, but just handing them code "helps" them now (they can get marks for their assignment), though it doesnt "help" them later (during a test which is worth much more than an assignment).

    Also, its often a good idea to just give english/pseudocode or even short actual working code to explain things or to give an idea of what the solution might be, as it promotes their independent thinking and enforces them to actually work on the major details or fill in the minor details.

    So, again, just giving someone a solution shows that, I think, you (or whoever does this) isnt really interested in helping the person in the long run. If youre OK with that then thats fine, but you should read over the forum policies to see if that is allowed (I dont know if it is actually stated to allow/disallow this). If youre allowed to do this, then I imagine no one can prevent you from doing so (but can of course make suggestions to stop). If it isnt allowed, then you dont have any option.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nadroj View Post
    So, again, just giving someone a solution shows that, I think, you (or whoever does this) isnt really interested in helping the person
    Esp. when it is not even in the correct language, which demonstrates that you were in such a rush to post your solution you didn't even bother to read the question carefully.

    I could post two lines of perl to do this, but that is not going to help (or impress) anyone. If you want to test your problem solving abilities, try sphere:

    Sphere Online Judge (SPOJ)
    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

  7. #7
    Registered User
    Join Date
    Dec 2009
    Location
    Bangladesh
    Posts
    7
    Thanks to MK27 Its working fine and I understood how it works.

    And sufoode I did not understand this line

    char *names[] = {"jim", "rock", "tommy", "chris", "x", "y" };
    cout << names[rand() % 6] << endl;[/CODE]
    But thanx everyone for helping me

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Exorcist
    And sufoode I did not understand this line
    It has the same idea as what MK27 suggested, but is C++, not C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2009
    Location
    Bangladesh
    Posts
    7
    Thats why I dont know how it works. So making a string array is the only solution for my problem in C?

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    For randomizing a list of strings, stored in an array; then yes this is the easiest way. For the best O factor, then I still think that may be the preferred way as well, but those more advanced may say otherwise.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Exorcist
    So making a string array is the only solution for my problem in C?
    For what you want to do, yes, an array of pointers to the first characters of string literals is probably best. Instead of your rather long code, I would implement a shuffle, then pair the first half of the shuffled array with the next half. If you want to have a full round robin tournament, it would be even better to use a round robin pairing algorithm to generate a list of pairings, and then shuffle the array of "players" and thus assign them their pairing numbers.

    EDIT:
    sufoode, explain your code. Once again, this forum is not about spoonfeeding solutions. Look, if you are going to continue to do this, I'm going to ask for a change of rules to turn the common understanding into official policy, and then I'll delete your posts.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2009
    Location
    Bangladesh
    Posts
    7
    I understand what you mean by shuffle. But I couldn't do it. Actually the logic of shuffle isn't clear to me yet. In the previous thread of mine you told me to use swap. I did try but it didn't give me random numbers.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Exorcist View Post
    I understand what you mean by shuffle. But I couldn't do it. Actually the logic of shuffle isn't clear to me yet. In the previous thread of mine you told me to use swap. I did try but it didn't give me random numbers.
    What is it that you're trying to do, Exorcist? We can all chase our tails, and still, not help much. We need the full scoop from you: What do you want the program to do?

  14. #14
    Registered User
    Join Date
    Dec 2009
    Location
    Bangladesh
    Posts
    7
    I want it to give random numbers within 1 and 10.And it'll not give the same number twice. Laserlight told me to use shuffle on an array which contains the numbers from 1-10 . But I couldn't do the shuffling. I did it with a long code.

    My problem is solved. I just wanted to know how to shuffle an array.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    In general, when you want to generate non-repeating random numbers, you pick from an array of available values. Ensure the random factor has the proper limit so that indexing is not out-of-range.

    Then eliminate that picked element by moving the elements beyond that slot into its place... and reduce the size of the array by 1 (wherever you save that).

    Now repeat, choosing another random element from the reduced set.

    Repeat.

    If that’s what you were after in the first place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  3. plz help here, noob actually .D
    By BjoRn3n in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2005, 03:04 PM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM

Tags for this Thread