1. ## 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

2. 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.

Originally Posted by sufoode
Solutions:
I applaud Exorcist's aim to actually learn:
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.

3. Originally Posted by sufoode
the code is self explanatory
The code is self-explanatorily NOT EVEN VALID C CODE is what you mean...

4. Originally Posted by Exorcist
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.

5. 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.

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)

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. 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.

9. Thats why I dont know how it works. So making a string array is the only solution for my problem in C?

10. 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. 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.

12. 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. Originally Posted by Exorcist
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. 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. 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.