Your tutor has asked a lab pair to present their week 6 work.
Discuss the good, the bad and the ugly aspects of their code.
Please be gentle in any criticism - we are all learning!
int is_monochrome(int height, int width, int pixels[height][width]);
Your function should return 1 to indicate all elements of the array are 1 or 0. It should return 0 otherwise.
density = pixel_count / (height * width);to calculate an attribute suggested in the assignment spec. When they test their code they find that density is always 0.
They are mystified becaused they are calculating pixel_count, height and width correctly. What is happening?
When are for loops preferable?
#include <stdio.h> #define MAX_LINE 4096 int main(void) { char line[MAX_LINE]; int i; while (fgets(line, MAX_LINE, stdin) != NULL) { i = MAX_LINE; while (line[i] != '\n') { i = i - 1; } printf("line %d characters long\n", i); } return 0; }
line_length.c
which reads lines from its input and prints
how many characters each line contains.
The only functions you can use are fgets
and printf
.
You can assume lines contain at most 4096 characters.
For example:
./line_length Andrew Rocks line 12 characters long A very long line. line 17 characters long short line 5 characters long line 0 characters long
strip_comments.c
which reads lines from its input and prints
them after removing any C // style comments. In another words if the line contains //
it does not print the // or anything after it.
The only functions you can use are fgets
and printf
.
You can assume lines contain at most 4096 characters.
For example:
./strip_comments x = x + 1; // This means add one to the variable x x = x + 1;Also - is that a good comment to add to a C program?
What if the input contains printf("//");
?
filter_empty_lines.c
which reads lines from its input and prints
them only if they contain a non-white-space-character.
In another words remove lines are empty or contain only white-space.
The only functions you can use are fgets
and printf
.
You can assume lines contain at most 4096 characters.
You can assume there are only 3 white space characters, space, tab & new-line.
For example:
./filter_empty_lines full line full line another no-empty line another no-empty line
reverse.c
which reads lines and writes them out
with the characters of each line in reverse order.
It should stop when it reaches the end of input.
For example:
./reverse The quick brown fox jumped over the lazy dog. .god yzal eht revo depmuj xof nworb kciuq ehT It was the best of times. It was the worst of times. .semit fo tsrow eht saw tI .semit fo tseb eht saw tI This is the last line. .enil tsal eht si sihT <control-d>
Your tutor may still choose to cover some of the questions time permitting.
double get_density(int height, int width, int pixels[height][width]);
void rotate_array(int height, int width, int pixels[height][width], int rotation);
int scrabble(char letters[], int num_letters, char *string);