Okay, now, before scrambling, I decided to let the user input the names of each character of the swapping with this code:
But, no matter what I try, I can't get the names in the array. Is there a specific command to do that, or am I just doing it wrong?Code:main (void) { int N; char n1 [10],n2 [10],n3 [10],n4 [10],n5 [10],n6 [10],n7 [10],n8 [10],n9 [10]; printf ("Give me the 1st name\n"); scanf ("%s", n1); printf ("Give me the 2nd name\n"); scanf ("%s", n2); printf ("Give me the 3rd name\n"); scanf ("%s", n3); printf ("Give me the 4th name\n"); scanf ("%s", n4); printf ("Give me the 5th name\n"); scanf ("%s", n5); printf ("Give me the 6th name\n"); scanf ("%s", n6); printf ("Give me the 7th name\n"); scanf ("%s", n7); printf ("Give me the 8th name\n"); scanf ("%s", n8); printf ("Give me the 9th name\n"); scanf ("%s", n9); char Names [9] [15] = {&n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9}; printf ("Give me a number from 1-9\n"); scanf ("%d", &N); printf ("%s", Names[N-1]); system ("PAUSE"); }
P.S.- the "void" in main (void) isn't really necesary according to our compiler and our teacher, that's why I didn't put it there before.
P.P.S.- Thanks for the scrambling algorithms, I'll tell you how it goes when I try them.
Isn't this really a variation on Tower of Hanoi - Wikipedia, the free encyclopedia
I don't follow. In the Tower of Hanoi, you repeat the order of two of the disks, many times.
create the array first...then input your data directly into the array..
Code:char Names[10][20]; for(int i = 0; i < 10; i++) scanf("%19s",Names[i]);That's right the void parameter isn't absolutely necessary (arguably, it is more secure)... but the integer return value is! The operating system uses that returned value to determine if you code is returning an error or not. Moreover; some parent programs and batch files also use the return values for various programmatic reasons. Your code may work perfectly but it will be incorrectly interfaced with the operating system.P.S.- the "void" in main (void) isn't really necesary according to our compiler and our teacher, that's why I didn't put it there before.
More than once I've watched batch files crash because some idiot let his program return garbage instead of paying attention to details.
Last edited by CommonTater; 12-02-2011 at 12:19 PM.
It's a run errands day today. Likely you'll have it finished before I get back. Sounds like a fun one to ponder though.
Okay, so far, I've come up with this (and since my partner is pretty much only doing what I tell him to do, my brains are about to explode by this point). I've Input the 9 names into an string array and copied the original array so that I can compare who's mind is in who's body after the scramble. I figured I could try to create a pointer to each of the positions in the array, and then scramble them, but I've got no idea how to do it. Is there a command for it? 'Cause all i can find is malloc, calloc and realloc, and they're no use (or I'm using them wrong, also a distinct posibility xD). Anyway, do you have any ideas how to do it?
Code:#include<stdio.h> #include<string.h> main (void) { int N, i; char Names [9] [15]; //Input Names printf("Give me 9 Names\n"); for (i = 0; i < 9; i++) { scanf ("%s", Names[i]); } //Copy Original Array char names [9] [15]; strcpy (names, Names) //Scramble Array (Names) //Return Scrambled Array int j; for (j = 0; i < 9; j++) { printf ("%s's mind is in %s's body", names[j], Names[j]); } //Unscramble, step by step, the array
I would not suggest trying to compare two string arrays... that's a lot of coding.
Take a look at my suggestion in message 13 for a starting point... if you want to print names instead of numbers, take the suggestion in message 14 and just use the names array to print with...
And you are still not setting up your main() function correctly... If your teacher is telling you that you don't need to return an integer value from main, he's wrong... use... int main (void) or int main() don't count on your compiler to correct it for you.
Like this...
Code:// tested #include <stdio.h> #include <time.h> #include <stdlib.h> void swap(int x, int y, int* array) { int temp; temp = array[x]; array[x] = array[y]; array[y] = temp; } // Entry int main (void) { int array[9]; char Names[9][16] = {"Zoidberg", "Fry", "Leela", "Profesor", "Bender", "Amy", "Hermes", "Washbucket", "E. Nikolai"}; int i,x,y; srand(time(NULL)); // initialize for (i = 0; i < 9; i++) array[i] = i; // start problem do { x = rand() %9; y = rand() %9; } while( x == y ); swap(x,y,array); // correct names printf("Players : \n"); for (i = 0; i < 9; i++) printf("%s ", Names[i]); printf("\n\nTo start: \n"); // initial status for (i = 0; i < 9; i++) printf("%s is in %s body\n", Names[array[i]], Names[i]); printf("\n\n"); getchar(); return 0; }
Last edited by CommonTater; 12-02-2011 at 01:47 PM.
Okay, scratch what I said before. I just had an idea, but I wanted to run it through you before I started trying it out.
What I thought was this, more or less:
Would something like that (well done of course) work??Code:#include<stdio.h> #include<string.h> int main (void) { int N, i; char Body [9] [15]; //Input Names printf("Give me 9 Names\n"); for (i = 0; i < 9; i++) { scanf ("%s", Body[i]); } //Copy Original Array char Mind [9] [15]; strcpy (Mind, Body) //Scramble Array (Mind) //RANDOM NUMBER GENERATOR (random number is x) (9 times) int k, count=0 for (k=0; k<9; k++) Mind [x]= Body [count] count = count+1
Are you reading any of my suggestions?
No it won't work... Go back to the show... the problem begins with 1 initial swap... you would swap all 9 of them.
Yes, I do read your suggestions, I just forgot, and was looking for a way to delete the comment, sorry. Now I'm trying to do pretty much the same thing by changing a bit the card shuffle code you gave me before. I'll tell you how it goes in a bit. Thanks again.
Here's a thought... copy/paste the code in message 25 and try it out... I compiled it here and tested it... I know it works.
Okay, so I tried with the shuffle deck code, and it pretty much works, but when it's going to return the scrambled array, it stops working. Can you see anything wrong with the code?
Code:#include<stdio.h> #include<string.h> #include<time.h> #include<stdlib.h> #define RANDS 9 int main (void) { int N, i; char Body [9] [15]; //Input Names printf("Give me 9 Names\n"); for (i = 0; i < 9; i++) { scanf ("%s", Body[i]); } //Scramble Array (Names) int Mind[RANDS]; int item = 0; int temp; int idx = 0; srand(time(NULL)); for(idx = 0; idx < RANDS; idx++) Mind[idx] = idx + 1; for (idx = RANDS - 1; idx > 0; idx--) { item = rand() % idx; temp = Mind[idx]; Mind[idx] = Mind[item]; Mind[item] = temp; } //Return Scrambled Array int j; for (j = 0; j < 9; j++) { printf ("%s's mind is in %s's body\n", Mind[j], Body[j]); } //Unscramble, step by step, the array //Return Names }