
Patrick B. answered 03/25/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STR_SIZE (25)
int Menu()
{
int iReturn=-1;
while ((iReturn<0) || (iReturn>3))
{
printf("********************************\n");
printf(" <1> SCRAMBLE THE WORDS \n");
printf(" <2> RANDOMLY SELECT WORDS \n");
printf(" <3> ORIGINAL WORD LIST \n");
printf("*********************************\n");
printf(" INPUT SELECTION OR ZER0 to QUIT :>");
scanf("%d",&iReturn);
}
return(iReturn);
}
void OutputWords(char ** A, int n, char * msgStr)
{
int iLoop;
if (msgStr!=NULL)
{
printf("*******************************************\n");
printf(msgStr); printf("\n");
}
printf("*********************************************\n");
for (iLoop=0; iLoop<n; iLoop++)
{
printf(">%s< \n",A[iLoop]);
}
}
Scramble(char ** A, int n)
{
char tempWord[STR_SIZE];
int iLoop;
int strLen;
int iRandomIndex;
for (iLoop=0; iLoop<n; iLoop++)
{
iRandomIndex = rand()%n;
strcpy(tempWord,A[iLoop]);
strcpy(A[iLoop],A[iRandomIndex]);
strcpy(A[iRandomIndex],tempWord);
}
}
Go()
{
char * _words[] = {(char*)"COMP10120",
(char*)"is",
(char*)"my",
(char*)"favorite",
(char*)"module",
(char*)"and",
(char*)"I",
(char*)"learn",
(char*)"lots",
(char*)"of",
(char*)"interesting",
(char*)"things"};
char * words[12];
char Words[12][STR_SIZE];
char * copyWords[12];
char CopyWords[12][STR_SIZE];
int iLoop=0;
for (iLoop=0; iLoop<12; iLoop++)
{
strcpy(Words[iLoop],_words[iLoop]);
strcpy(CopyWords[iLoop],_words[iLoop]);
words[iLoop] = Words[iLoop];
copyWords[iLoop]=CopyWords[iLoop];
}
OutputWords(words,12, (char*)" The original words are: ");
int menuReturn = -1;
while ((menuReturn= Menu()) != 0)
{
switch (menuReturn)
{
case 1:
{
Scramble(copyWords,12);
OutputWords(copyWords,12, (char*)"Scrambled Words");
break;
}
case 2:
{
int iRandomIndex = rand()%12;
printf(" Random word at index position %d is >%s< \n",iRandomIndex,words[iRandomIndex]);
break;
}
case 3:
{
OutputWords(words,12, (char*)" The original words are: \n");
break;
}
}//switch
} //while
}
int main()
{
Go();
}