![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2005
Posts: 7
Rep Power: 0
![]() |
C Programming-Randomize
Hello everyone, I am new to C and am trying to learn everything from some 7 year old book about programming. It's not going well...lol. Well anyway I have really wanted to make this program for my family and I'm stuck. Any help would be appreciated. Here's what I want the program to do:
You see each year my family draws names to decide on who gives gifts to who. I need a program that asks to enter all the names of the family (as the family keeps growing) and then randomizes the names. Then I need it to print out PersonA gives gift to PersonB and so forth. Every person must give and receive a gift. Here is what i have so far. It asks for 15 family members and stores them in a variable. #include <stdio.h> #include <conio.h> main() { char name[10]; char name1[10]; char name2[10]; char name3[10]; char name4[10]; char name5[10]; char name6[10]; char name7[10]; char name8[10]; char name9[10]; char name10[10]; char name11[10]; char name12[10]; char name13[10]; char name14[10]; //Gets all the people's names printf("Enter the 1st name.\n"); scanf("%s", name); printf("Oh ok the 1st name is %s\n", name); printf("\n\nPlease enter the 2nd name.\n"); scanf("%s", name1); printf("Oh ok the 2nd name is %s\n", name1); printf("\n\nPlease enter the 3rd name.\n"); scanf("%s", name2); printf("Oh ok the 3rd name is %s\n", name2); printf("\n\nPlease enter the 4th name.\n"); scanf("%s", name3); printf("Oh ok the 4th name is %s\n", name3); printf("\n\nPlease enter the 5th name.\n"); scanf("%s", name4); printf("Oh ok the 5th name is %s\n", name4); printf("\n\nPlease enter the 6th name.\n"); scanf("%s", name5); printf("Oh ok the 6th name is %s\n", name5); printf("\n\nPlease enter the 7th name.\n"); scanf("%s", name6); printf("Oh ok the 7th name is %s\n", name6); printf("\n\nPlease enter the 8th name.\n"); scanf("%s", name7); printf("Oh ok the 8th name is %s\n", name7); printf("\n\nPlease enter the 9th name.\n"); scanf("%s", name8); printf("Oh ok the 9th name is %s\n", name8); printf("\n\nPlease enter the 10th name.\n"); scanf("%s", name9); printf("Oh ok the 10th name is %s\n", name9); printf("\n\nPlease enter the 11th name.\n"); scanf("%s", name10); printf("Oh ok the 11th name is %s\n", name10); printf("\n\nPlease enter the 12th name.\n"); scanf("%s", name11); printf("Oh ok the 12th name is %s\n", name11); printf("\n\nPlease enter the 13th name.\n"); scanf("%s", name12); printf("Oh ok the 13th name is %s\n", name12); printf("\n\nPlease enter the 14th name.\n"); scanf("%s", name13); printf("Oh ok the 14th name is %s\n", name13); printf("\n\nPlease enter the 15th name.\n"); scanf("%s", name14); printf("Oh ok the 15th name is %s\n", name14); return 0; } Now I don't know how to randomize the character variables! Thanks in advance for any advice and help. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
take a look here
http://www.phim.unibe.ch/comp_doc/c_...C/MAN/rand.htm and i suggest skipping forward to the part where you learn about loops.. ![]() |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2005
Posts: 7
Rep Power: 0
![]() |
thanks for the quick reply, meanwhile anyone else can post any advice too
![]() |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jan 2005
Posts: 7
Rep Power: 0
![]() |
ok i read through that site and i found how to make random integers but not how to randomize character strings that are stored in variables. Any more help? I'm still stumped
![]() |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
i would store the names in an array, get the array size then you have, for instance 15 numbers, just randomise those numbers
1 gives 5 2 gives 15 7 gives 2 check that there are no duplicates/missing people, then you can call the names from teh array using array[2] gives array[15] Just look up arrays and things like that, but thats how i would do it :/. Or have two 15 number arrays 1 gives one recives, jumble them up and then check no giving self a present, and just print out giveArray[0] gives recvArray[0] giveArray[1] gives recvArray[1] giveArray[2] gives recvArray[2] (dont use this for syntax i am not to hot on c, i could do it in java though). |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jan 2005
Posts: 7
Rep Power: 0
![]() |
ok well does anyone know how to randomize the character arrays? I've looked everywhere and can't find it.
Last edited by brandcolt; Feb 3rd, 2005 at 10:52 AM. |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
There are several options when it comes to what you want. The most obvious is also the least efficient. That option is randomly shuffling the array by copying the strings with strcpy and the like. A better option is to maintain an array of pointers to the strings that you can easily shuffle, or an array of indices.
Here is an implementation for the option using an array of indices: #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N_NAMES 15
#define MAX_LEN 10
int main ( void )
{
char names[N_NAMES][MAX_LEN];
int index[N_NAMES];
int i, j;
for ( i = 0; i < N_NAMES; i++ ) {
printf ( "Enter name #%d: ", i + 1 );
fflush ( stdout );
if ( fgets ( names[i], MAX_LEN, stdin ) == NULL )
break;
index[i] = i;
}
srand ( (unsigned)time ( NULL ) );
/* Random shuffle */
for ( j = 0; j < i; j++ ) {
int r = rand() % ( i - j ) + j;
int save = index[j];
index[j] = index[r];
index[r] = save;
}
for ( j = 0; j < i; j++ )
printf ( "%s", names[index[j]] );
getchar();
return 0;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|