Question

I need to modify below code to have output like: Example: RBGY [ You have 1...

I need to modify below code to have output like: Example: RBGY

[ You have 1 white flag and you have 1 red flag]

Example: CCGG

[ You have 0 white flag and you have 1 red flag]

==================================

#include <stdio.h>
#include <string.h> 
#include <time.h>
#include <stdlib.h>

#define SIZE_STRING 4
#define SIZE_STRING_LONG 15


int main(int argc, char *argv[]){

    if(argc != 2) {

        printf("Usage: ./main <scComputer>");
        exit(0);
    }

    char szUser[SIZE_STRING_LONG];
    char *szComputer = argv[1];
    int counter=0;
    int colour=0;
    int position=0; 
    char alreadyChecked[SIZE_STRING];


    printf("

 You have 8 changes to get it right. 
");           

    printf("

 Computer choses %s", szComputer); //check if the program works allright

    do{
        counter++;
        position=0;
        colour=0;
        memset(alreadyChecked, 0, sizeof(alreadyChecked));
        printf("
 ===================================================
");
        printf("
 Chance %d", counter);
        printf("

 Please enter your 4 colours choice");
        printf("
 (Please write capital letters without space  ");         
        scanf("%s",szUser);     

        if(strlen(szUser) != SIZE_STRING)
        {
            printf("

 Sorry you choose a wrong option.");
            counter--;
        } else {

                if(strcmp(szUser,szComputer))
                {
                    printf("
 Wrong choice. Try again...");                    

                    for (int u = 0; u < SIZE_STRING; u++)
                    {
                        if (szUser[u] == szComputer[u])
                        {
                            position++;                             
                        }
                    }

                    if (position > 0 && position < 2)
                    {
                        printf("

 You have %d red flag", position);
                    }

                    if (position > 1 )
                    {
                        printf("

 You have %d red flags", position);
                    }

                    for (int u = 0; u < SIZE_STRING; u++)
                    {

                        if (szUser[u] == szComputer[u])
                        {        
                            continue;
                        }

                            for (int c = 0; c < SIZE_STRING; c++)
                            {
                                if (!alreadyChecked[c] && szUser[u] == szComputer[c])
                                {
                                    colour++;
                                    alreadyChecked[c] = 1;

                                    break; 
                                }
                            }
                    }       



                    if (colour > 0 && colour < 2)
                    {
                        printf("

 You have %d white flag", colour);
                    }

                    if (colour > 1 )
                    {
                        printf("

 You have %d white flags", colour);
                    }

                    if (colour == 0 && position == 0)
                    {
                        printf("

 0 white flags and 0 red flags");
                    }
                }
            }

    }while(strcmp(szUser,szComputer) && counter <=7);

    if (strcmp(szUser,szComputer))
    {
        printf("

 Sorry, you run out of chances...");
    }   

    if(!strcmp(szUser,szComputer))
        printf("

 Right combination !
");

    return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#define SIZE_STRING 4
#define SIZE_STRING_LONG 15


int main(int argc, char *argv[]){

if(argc != 2) {

printf("Usage: ./main <scComputer>");
exit(0);
}

char szUser[SIZE_STRING_LONG];
char *szComputer = argv[1];
int counter=0;
int colour=0;
int position=0;
char alreadyChecked[SIZE_STRING];
   int checkedarr1[4];
   int checkedarr2[4];

  
     
printf("\nYou have 8 changes to get it right. ");   
printf("\n===================================================\n");
printf("\nComputer chooses %s", szComputer); //to check if the program works allright

do{
counter++;
position=0;
colour=0;
memset(alreadyChecked, 0, sizeof(alreadyChecked));
memset(checkedarr1, 0, sizeof(checkedarr1));
memset(checkedarr1, 0, sizeof(checkedarr2));
printf("\n===================================================\n");
printf("\nPlease enter your 4 colours choice");
printf("\n(Please write capital letters without space ");   
       printf("\nChance %d :", counter);
scanf("%s",szUser);   

if(strlen(szUser) != SIZE_STRING)
{
printf("\nSorry you choose a wrong option.");
counter--;
} else {

if(strcmp(szUser,szComputer))
{
printf("\nWrong choice. Try again...");

for (int u = 0; u < SIZE_STRING; u++)
{
if (szUser[u] == szComputer[u])
{
position++;
                           checkedarr1[u]=1;   
                           checkedarr2[u]=1;
}
}


for (int i = 0; i < SIZE_STRING; i++)
{
   if(checkedarr1[i]!=1)
   {
                       for (int j = 0; j < SIZE_STRING; j++)
        {
                           if(checkedarr2[j]!=1)           
                           {
                               if(szUser[i] == szComputer[j])
                               {
                                   colour++;
                                   checkedarr2[j]=1;
                                   break;
                               }
                           }
        }
                       }
                   }
                   printf("\n[ You have %d white flag and you have %d red flag]",colour,position);
}
}

}while(strcmp(szUser,szComputer) && counter <=7);

if (strcmp(szUser,szComputer))
{
printf("\nSorry, you run out of chances...");
}   

if(!strcmp(szUser,szComputer))
printf("\nRight combination !");

return 0;
}

Add a comment
Know the answer?
Add Answer to:
I need to modify below code to have output like: Example: RBGY [ You have 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

    PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

    #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function  

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer...

    T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...

  • -I need to write a program in C to store a list of names (the last...

    -I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){     int i = 0;     while (i < size){         int j = i+1;         while (j < size){...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT