Question: Below are the errors that I have in my code. After that is the actual code that I wrote. I cannot figure out what is
Below are the errors that I have in my code. After that is the actual code that I wrote. I cannot figure out what is wrong. Could someone please correct it for me to have it run please? Thank you!
lab6.c:25:5: warning: implicit declaration of function 'convert_tohuffman' [-Wimplicit-function-declaration] convert_tohuffman(code, text, text_huff); ^~~~~~~~~~~~~~~~~ lab6.c:26:26: warning: initialization makes integer from pointer without a cast [-Wint-conversion] char book_filename = "book.txt"; //We will be given book.txt file once we get the book.txt we can generate the huffman code ^~~~~~~~~~ lab6.c:31:16: warning: passing argument 1 of 'fopen' makes pointer from integer without a cast [-Wint-conversion] fp = fopen(book_filename, "r"); ^~~~~~~~~~~~~ In file included from lab6.c:4:0: /usr/include/stdio.h:274:14: note: expected 'const char * restrict' but argument is of type 'char' extern FILE *fopen (const char *__restrict __filename, ^~~~~ lab6.c: At top level: lab6.c:84:6: warning: conflicting types for 'convert_tohuffman' void convert_tohuffman(huffman code[], char text[], char text_huff[]) { ^~~~~~~~~~~~~~~~~ lab6.c:25:5: note: previous implicit declaration of 'convert_tohuffman' was here convert_tohuffman(code, text, text_huff); ^~~~~~~~~~~~~~~~~ lab6.c: In function 'translate_tomorse': lab6.c:122:21: warning: implicit declaration of function 'beeper' [-Wimplicit-function-declaration] beeper(100); ^~~~~~ lab6.c: At top level: lab6.c:142:6: warning: conflicting types for 'beeper' void beeper(int delay_time) //function that makes the buzzer on breadboard beep ^~~~~~ lab6.c:122:21: note: previous implicit declaration of 'beeper' was here beeper(100); ^~~~~~ lab6.c: In function 'beeper': lab6.c:145:18: error: 'BeepPin' undeclared (first use in this function) digitalWrite(BeepPin, LOW); ^~~~~~~ lab6.c:145:18: note: each undeclared identifier is reported only once for each function it appears in
#include
//List prototypes here void calc_stats(float *stats); void print_stats(float *stats); typedef struct { char letter; char code[10]; }huffman;
int main(int argc, char *argu[]){ char text[] = "C is great!"; char text_huff[1000]; huffman code[MAX_CHAR]; //maximum 26 of these strucutres convert_tohuffman(code, text, text_huff); char book_filename = "book.txt"; //We will be given book.txt file once we get the book.txt we can generate the huffman code float stats[MAX_CHAR]; //generates the statistics for (int i = 0; i < MAX_CHAR; i++) { stats[i] = 0; } fp = fopen(book_filename, "r"); calc_stats(stats); print_stats(stats); return 0; }
void calc_stats(float stats[]) { char temp[100]; //reads in 100 characters at a time int ch; int index; int count = 0; while (!feof(fp)) { fgets(temp, 100, fp); for (int i = 0; i < strlen(temp); i++) { ch = temp[i]; if (ch >= 'A' && ch <= 'Z') index = ch - 'A'; if (ch >= 'a' && 'z') { index = ch - 'a'; } if (index >= 0 && index <= 25) { stats[index]++; } } //ends the for loop }//ends the while loop fclose(fp); for (int i = 0; i < MAX_CHAR; i++) { stats[i] = stats[i] / count; } } //ends calc_stats
void print_stats(float stats[]) { for (int i = 0; i < MAX_CHAR; i++) { printf("%d=%f ", 'a' + i, stats[i]); } }
void read_huffman(huffman code[]) { int index; int count; char ch; char temp[10]; fp = fopen("huffman.txt", "r"); while (!feof(fp)){ fscanf(fp, "%c %s", &ch, temp); index = ch - 'a'; code[index].letter = ch; strcpy(code[index].code, temp); } //ends while fclose(fp); } //read_huffman
void convert_tohuffman(huffman code[], char text[], char text_huff[]) { char ch; int index; int count; char temp[10]; for (int i = 0; i < strlen(text); i++) { ch = text[i]; if (ch >= 'A' && ch <= 'Z') index = ch - 'A'; if (ch >= 'a' && ch <= 'z') index = ch - 'a'; strcpy(temp, code[index].code); for (int i = count; i < count + strlen(temp); i++) { text[i] = temp[i - count]; } count = strlen(temp); } }
void translate_tomorse(char str[100], char morse_code[26][10]) //function that translates words to morse code { int textlength;
textlength = strlen(str);
for (int i = 0; i if (index >= 0 && index <= 25) { char morse[10]; strcpy(morse_code[index], morse); for (int j = 0; j void read_morse(FILE *fp, char morse_code[26][10]) //function that reads morse code { int i = 0; while (!feof(fp)) { fscanf(fp, "%s", morse_code[i]); i++; } } void beeper(int delay_time) //function that makes the buzzer on breadboard beep { digitalWrite(BeepPin, LOW); delay(delay_time); digitalWrite(BeepPin, HIGH); delay(delay_time); } Laboratory Assignment #6 In this laboratory assignment, you will work in a group of two (2) to implement the Huffman encoding and decoding system. The design of this program will be more flexible compared to the previous lab assignments in this class. The tasks are: 1. Write a program to read a text file that I provide (book.txt) and calculate the probability for each of the characters contained in the file. 2. Use the probability values that you computed in Step 1 and come up with a Huffman code for the letters represented by the probability values. Type the Huffman code that you found into a text file and save it with the name Huffman.txt with the first column containing the letter and the second column containing the corresponding binary code. The contents of the file Huffman.txt should look like this: A 10 B 101 Etc 3. Write another program that reads in the Huffman.txt file that you created and a Message.txt file (that I provide) to encode the text message using your Huffman code and to decode it back into text. Your program will also transmit the Huffman encoded binary message to your breadboard to drive the LED. The design of the LED on your breadboard will be up to you. As an example, you may use a green light to represent the ONE (1) and a red light to represent the ZERO (0), you may use the brightness of the light to represent the ONE and ZERO (i.e., bright light for ONE and dim light for ZERO) by varying the resistors, you may change the length of time an LED remains lighted, etc FOR EXTRA CREDIT: Your program will convert the text message into Morse code and used to drive the LED on your breadboard. I suggest that you write the Morse code into a text file (Morse.txt) and read it in similarly to the way you implement the Huffman code. The design of the LED on your breadboard will again be up to you. Perhaps use the same scheme developed above with the dash as ONE and dot as ZERO. I will leave the design of this laboratory assignment (i.e., what functions to write, what to name your functions, how to implement the Huffman code, how to implement the LED to encode the binary ONE and ZERO, etc.). I encourage you and your teammate to discuss the design of the code, draw a block diagram showing the flow of the programs, and pseudo-code everything before attempting to write the C code. Note that the design of the Huffman code from the probabilities you calculated in Step 1 will be done by hand and then typed into Huffman.txt. Sample program files: 1. book.txt adfadfafTTWEdaaTFadaRKFGitUgjfj dfafTTWEdaaTFadaRKFGitUgjfjerA . . . 2. Huffman.txt a 1 b 101 . . . 3. Message.txt This laboratory assignment is so fun!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
