Question
C Coding Puzzle: Please finish the following shell code and post a screenshot of working output. This project addresses efficient manipulation of data structures and
C Coding Puzzle: Please finish the following shell code and post a screenshot of working output.
This project addresses efficient manipulation of data structures and pattern matching. The task is a classic IQ test where one is asked to match an eight color pattern to a copy that may be flipped (mirrored) and rotated. The reference puzzle is the square in the center of the board. The eight candidate puzzles wrap around the edge of the board clockwise beginning in the upper left hand corner. The color codes are as follows (0 = red, 1 = yellow, 2 = green, and 3 = blue). The reference pattern in the example below (the center 3 x 3 pattern) is yellow, blue, red, yellow, blue, red, yellow, red. The matched candidate is the pattern mark with an X to the left of the reference (pattern 7). Note that it is flipped and rotated. Exactly one of the eight candidates will match the reference pattern; it may be flipped (horizontally or vertically), rotated, or both. The reference and candidate color codes are each packed into the lower 16-bits of an unsigned integer (2 bits per element). The Reference and Candidates arrays shown below show the 2 bit values of each element, and the table shows the values of the corresponding packed unsigned int.
Part1-1: In this part, you must write a C program to locate the matching puzzle in the grid. Use the shell code P1-1-shell.c as a starting point. Two testfiles (test1.txt and test3.txt) containing sample puzzles are included. You may create additional puzzles using the dump command in Misasim. In order for your solution to be properly received and graded, there are a few requirements. 1. The file must be named P1-1.c. 2. The program should report the answer using the following print statement: printf(The matching pattern is at position [0-7] %d , Position);
Here's a link to the shell code and test files:
https://we.tl/t-P1rSNXMAC0
Please fill in the following code:
/* Match Puzzle
This program finds the reference pattern in the candidates which might be rotated or flipped.
Your name: Date: */
#include
int main(int argc, char *argv[]) { int Reference; int Candidates[8]; int NumCandidates; int Position = 0; //temporary initial value int Load_Reference(char *); int Load_Mem(char *, int *);
if (argc != 2) { printf("usage: ./P1-1 testfile "); exit(1); } Reference = Load_Reference(argv[1]); if (Reference == 0) { printf("Invalid Reference pattern. "); exit(1); } NumCandidates = Load_Mem(argv[1], Candidates); if (NumCandidates != 8) { printf("testfiles must contain 8 candidates "); exit(1); }
/* Your program goes here */
printf("The matching pattern is at position [0-7] %d ", Position); exit(0); }
/* This routine reads in one integer value from the first line of a named file in the local directory (each line of the file is in the form Addr: integer). The integer is returned. */
int Load_Reference(char *InputFileName) { int Reference, NumVals, Addr, Value; FILE *FP;
FP = fopen(InputFileName, "r"); if (FP == NULL) { printf("%s could not be opened; check the filename ", InputFileName); return 0; } else { NumVals = fscanf(FP, "%d: %d", &Addr, &Value); if (NumVals == 2) Reference = Value; else { printf("test file must contain Addr: Value pairs on each line. "); Reference = 0; } fclose(FP); return Reference; } }
/* This routine loads in up to 8 newline delimited integers from a named file in the local directory. The values are placed in the passed integer array. The number of input integers is returned. */
int Load_Mem(char *InputFileName, int IntArray[]) { int N, Addr, Value, NumVals; FILE *FP;
FP = fopen(InputFileName, "r"); if (FP == NULL) { printf("%s could not be opened; check the filename ", InputFileName); return 0; } else { fscanf(FP, "%d: %d", &Addr, &Value); // ignore first line for (N=0; N Reference ,3, 0, 1, 3, 0, 1, 01 2 Candidates 2, 3, 2, 3, 1, 2, 3, 11, 3, 0, 2. 0, 3, 1, 2. 11. 2, 0, 3, 2, 1, 3, 0, 11. a,0 3, 1, 2, 0, 2, 11 2, 0, 3, 1, 3,0, 3. 11. [0, 3, 1, 0, , 0, 3, 111 6 4 reference | cand. O | cand. 1 | cand. 2 | cand. 3 | cand. 4 | cand. 5 | cand. 6 | cand. 7 4941 31214 26403 51378 19890 25203 33863 29554 28956 Reference ,3, 0, 1, 3, 0, 1, 01 2 Candidates 2, 3, 2, 3, 1, 2, 3, 11, 3, 0, 2. 0, 3, 1, 2. 11. 2, 0, 3, 2, 1, 3, 0, 11. a,0 3, 1, 2, 0, 2, 11 2, 0, 3, 1, 3,0, 3. 11. [0, 3, 1, 0, , 0, 3, 111 6 4 reference | cand. O | cand. 1 | cand. 2 | cand. 3 | cand. 4 | cand. 5 | cand. 6 | cand. 7 4941 31214 26403 51378 19890 25203 33863 29554 28956
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started