Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 31st, 2005, 10:53 AM   #1
brandcolt
Newbie
 
Join Date: Jan 2005
Posts: 7
Rep Power: 0 brandcolt is on a distinguished road
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.
brandcolt is offline   Reply With Quote
Old Jan 31st, 2005, 10:57 AM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
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..
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Jan 31st, 2005, 11:03 AM   #3
brandcolt
Newbie
 
Join Date: Jan 2005
Posts: 7
Rep Power: 0 brandcolt is on a distinguished road
thanks for the quick reply, meanwhile anyone else can post any advice too
brandcolt is offline   Reply With Quote
Old Feb 1st, 2005, 10:50 AM   #4
brandcolt
Newbie
 
Join Date: Jan 2005
Posts: 7
Rep Power: 0 brandcolt is on a distinguished road
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
brandcolt is offline   Reply With Quote
Old Feb 1st, 2005, 11:07 AM   #5
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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).
Berto is offline   Reply With Quote
Old Feb 3rd, 2005, 10:44 AM   #6
brandcolt
Newbie
 
Join Date: Jan 2005
Posts: 7
Rep Power: 0 brandcolt is on a distinguished road
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.
brandcolt is offline   Reply With Quote
Old Feb 3rd, 2005, 11:43 AM   #7
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
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;
}
Eggbert is offline   Reply With Quote
Old Feb 3rd, 2005, 4:09 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
He's back!
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC