Question

Design and implement a C version of the color match program. As a starting point, use...

Design and implement a C version of the color match program. As a starting point, use the file
HW2-1-shell.c. The program should employ a reasonable algorithm that compares all
pairings of colors in the palette exactly once. A color should not be compared to itself. Nor should
two colors be compared to each other more than once. Your C program should print out the total
component color difference for the closest pair using the print string provided in the shell code.
Name the file HW2-1.C and upload it to Canvas by the scheduled due date.
The shell program HW2-1-shell.c includes a reader function Load_Mem() that loads the
values from a text file. You should use gcc under Ubuntu to develop your program. Sample test
files (test119.txt, test30.txt, test111.txt) are provided – the number in the
name of each file indicates the correct answer for the min component difference. You should
compile and run your program using the Linux command lines:
> gcc HW2-1.c –g –Wall –o HW2-1
> ./HW2-1 test119.txt
You can create additional test files using MiSaSiM to run HW2-2-shell.asm, go to the end
of the trace, and use the “Dump” memory menu button to save the memory to a text file with the
correct answer (the value in $13) in the name of the file.


2. Design and implement a MIPS version of the color match program. A description of the MIPS
library routines you need is given below. Use the file HW2-2-shell.asm as a starting point.
The results should be returned by your program in $10 (minimum total component difference),
$11 (memory address of closest color A), and $12 (memory address of closest color B). You must
use these register conventions or the automated grader will score your program incorrectly.
Memory should only be used for input to your program. Your program must return to the
operating system via the jr instruction. Programs that include infinite loops or produce
simulator warnings or errors will receive zero credit. Name the file HW2-2.asm and upload it to
Canvas by the scheduled due date.

HW2-1-shell.c.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* Color Matcher

This program finds the two closest colors in an array of packed RGB values,
based on the total component difference (computed as a sum of absolute
difference.) It prints the total component difference of the two closest colors.

Date:
Your Name:
*/

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

int main(int argc, char *argv[]) {
/* you may change and add to these declarations and initializations */
unsigned   Pixels[8];
int   NumPixels, MinDelta=-55; // temporary initial value
int Load_Mem(char *, unsigned *);

if (argc != 2) {
printf("usage: ./HW2-1 valuefile ");
exit(1);
}
NumPixels = Load_Mem(argv[1], Pixels);
if (NumPixels != 8) {
printf("valuefiles must contain 8 entries ");
exit(1);
}

/* Your code goes here */
  
printf("The two closest colors have a total component difference of %d ", MinDelta);
exit(0);
}

/* This routine loads in up to 8 newline delimited unsigned integers from
a named file in the local directory. The values are placed in the
passed unsigned integer array. The number of input integers is returned. */

int Load_Mem(char *InputFileName, unsigned PixelArray[]) {
int   N, Addr, NumVals;
unsigned Value;
FILE   *FP;
  
FP = fopen(InputFileName, "r");
if (FP == NULL) {
printf("%s could not be opened; check the filename ", InputFileName);
return 0;
} else {
for (N=0; N < 8; N++) {
NumVals = fscanf(FP, "%d: %d", &Addr, &Value);
if (NumVals == 2)
   PixelArray[N] = Value;
else
   break;
}
fclose(FP);
return N;
}
}

--------------------------------------------------------------------------------------------------------------------------

HW2-2-shell.asm

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# Color Matcher
#
# Your Name:
#
# Date:
#
# This program finds the two closest colors in a eight color palette.
#
# required output register usage:
# $10: minimum total component difference
# $11 and $12: memory addresses of the two closest colors

.data
Array: .alloc 8 # allocate static space for packed color data

.text
ColorMatch: addi $1, $0, Array # set memory base
swi 500 # create color palette and update memory

######################################################
# Temporary: the following 3 instructions demo use of swi 581.
# Be sure to replace them.
addi $10, $0, 48 # guess min component difference
addi $11, $1, 12 # guess an address
addi $12, $1, 4 # guess an address
######################################################

swi 581 # report answer (in $10, $11, $12)
jr $31 # return to caller

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------email to lexirobins@live.com if possible

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

The algorithm is simply:

1.Select first color from palette

2.Select second color from palette, from remaining colors

(we start from the rest of the colors so that we don't count the same pair twice i.e. [i,j] and [j,i])

3.calculate absolute value difference of the 2 colors

4.if the current difference is less than the minimum value, update the minimum value with the current difference

Below is the complete code:

#include <stdio.h>

#include <stdlib.h>

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

unsigned Pixels[8];

int NumPixels, MinDelta=-55; // temporary initial value

int Load_Mem(char *, unsigned *);

if (argc != 2) {

printf("usage: ./HW2-1 valuefile ");

exit(1);

}

NumPixels = Load_Mem(argv[1], Pixels);

if (NumPixels != 8) {

printf("valuefiles must contain 8 entries ");

exit(1);

}

int i,j,n=NumPixels;

MinDelta=99999;

for(i=0;i<n;i++)

{

for(j=i+1;j<n;j++)

{

int color_diff=(Pixels[i]-Pixels[j]);

if(color_diff<0)

color_diff=0-color_diff;

if(color_diff<MinDelta)

MinDelta=color_diff;

}

}

printf("The two closest colors have a total component difference of %d ", MinDelta);

exit(0);

}

int Load_Mem(char *InputFileName, unsigned PixelArray[]) {

int N, Addr, NumVals;

unsigned Value;

FILE *FP;

FP = fopen(InputFileName, "r");

if (FP == NULL) {

printf("%s could not be opened; check the filename ", InputFileName);

return 0;

} else {

for (N=0; N < 8; N++) {

NumVals = fscanf(FP, "%d: %d", &Addr, &Value);

if (NumVals == 2)

PixelArray[N] = Value;

else

break;

}

fclose(FP);

return N;

}

}

Add a comment
Know the answer?
Add Answer to:
Design and implement a C version of the color match program. As a starting point, use...
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
  • The program is done in C. This program opens a file containing binary or text and...

    The program is done in C. This program opens a file containing binary or text and reads every byte in the file and writes both the ASCII hex value for that byte as well as it’s printable (human-readable) character (characters, digits, symbols) to standard output. The issue I am having is when the file has multiple lines being read. The first line is read and done properly but the other lines do not work correctly. For instance, the text file...

  • Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code...

    Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code for 8-bit signed array, 16-bit unsigned array, 16-bit signed array, 32-bit signed array and 32-bit signed array. 2) Fill in all the blanks in Table 1 using your completed code, following the hints provided within the table. 3) Fill in all the blanks in Table 2 using your completed code, following the hints provided within the table. C++ Program #include <stdio.h> #include <iostream> int...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

  • This question has been asked before but the responses have been wrong. Please do not copy...

    This question has been asked before but the responses have been wrong. Please do not copy and paste their answers. Currently the program opens a file and reads every byte in the file and write both the ASCII hex value for that byte as well as it’s printable character to standard output with non-printable characters printing a "." Now, I want have an option where the program prints in binary instead of hex by typing "-b" at the command line....

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include...

    C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name    loadMusicFile // @Brief   Load the music database //          'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size; song++){       // Allocate memory for each individual character string       database[song] =...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • Using struct.c, as a starting point, implement an application that creates a database of employee personal...

    Using struct.c, as a starting point, implement an application that creates a database of employee personal records. Your implementation should follow these guidelines: the definition of the structure PERSON should be provided in an h-file called person.h that you need to create in the src sub-directory, for the content, refer to the lecture notes, typdef should be used for referencing the PERSON structure, an array employees[] should be declared in the main C file (that is struct.c), a new C...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

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