+ 1

How to print characters in array in random order???

Hello, so I'm kind of stuck. I am writing a C code which allows a user to enter their student number, then choose whether they want their student number written in the forwards direction, backwards or in random order. I am unsure how to randomize the characters entered into the array so I can then print them out in random order. Wonder if anyone can help? Here is how far I got: #include <stdio.h> #include <stdlib.h> #include<string.h> int main(void) { char fbr; int i; char name[9]; // char string as an array of characters printf("Please enter student number: "); //prompt user to enter student number scanf("%s", name); //accept input from user getchar(); // required to avoid return key being recognised as a character printf("\nYour student number has been entered. Please type f to print in forward direction, b to print backwards or r for random: \n\n"); scanf("%c", &fbr); if (fbr == 'f') //Print student number in the Forward Direction for(i=0; i<9; i++) { printf("%c",name[i]); } else if (fbr == 'b') // Print student number in the Reverse Direction for(i=8; i>=0; i--) { printf("%c",name[i]); } else if (fbr == 'r') { // Print student number Randomly } else { printf("\n\n hmmmmm \U0001F914 looks like you have entered an invalid option, please restart program and try again;"); } return 0; } \\end of code Thanks

26th Oct 2020, 10:43 AM
Arthur Jones
Arthur Jones - avatar
7 Answers
+ 2
Use rand() can generate a random number. srand(time(NULL)); //seed based on time. #include <time.h> printf("%d", rand()%9); And then you can access a random index of characters. But you will need a way to avoid duplicate index access.
26th Oct 2020, 11:07 AM
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż - avatar
26th Oct 2020, 11:07 AM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 1
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void swap(int *a, int *b) //swap function use to help randomise array { int temp = *a; *a = *b; *b = temp; } void randomise(int arr[], int n) //randomise function to shuffle the array for random option in program { srand(time(NULL)); //seed based on time int i; for(i = n-1; i > 0; i--) { int j = rand() % (i+1); //rand() used to generate a random number swap(&arr[i], &arr[j]); } } int main(void) { int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8 }; //array of integers to index the 8 char of student number int n = sizeof(arr)/ sizeof(arr[0]); char fbr; int i; char name[9]; // char string as an array of characters printf("Please enter student number: "); //prompt user to enter student number scanf("%s", name); //accept input from user getchar(); // required to avoid return key being recognised as a character printf("\nYour student number has been entered. Please type f to print in forward direction, b to print backwards or r for random: \n\n"); scanf("%c", &fbr); if (fbr == 'f') //Print student number in the Forward Direction for(i=0; i<9; i++) { printf("%c",name[i]); } else if (fbr == 'b') // Print student number in the Reverse Direction for(i=8; i>=0; i--) { printf("%c",name[i]); } else if (fbr == 'r') { // Print student number Randomly randomise (arr, n); for(i=0; i<n; i++) // print the array products using the randomised indexes { int index = arr[i]; printf("%c",name[index]); } } else // give error if neither 'f', 'b' or 'r' are chosen as options { printf("\n\n hmmmmm \U0001F914 looks like you have entered an invalid option, please restart program and try again;
26th Oct 2020, 12:28 PM
Arthur Jones
Arthur Jones - avatar
0
Thanks guys, got it figured out 👍
26th Oct 2020, 12:28 PM
Arthur Jones
Arthur Jones - avatar
0
Thanks for those points Martin. So if I wanted the code to ignore spaces and keep reading until it collects all the required characters I would have to get rid of scanf altogether and use something along the lines of fgets() instead? Link to code: https://code.sololearn.com/cyS8Kr8P9qne
26th Oct 2020, 2:48 PM
Arthur Jones
Arthur Jones - avatar