Note:
x += 1; //is shorthand for x = x + 1;
// Test if a point is inside a triangle. // Julian Saknussemm // // Given Three points of a triangle, and another arbitrary point this program determines if that point lies inside // the triangle. // // This is determined by satisfying the following rule: // A point P(x,y) is inside triangle A(x0,y0), B(x1,y1), C(x2,y2) // iff // P is on the same side of the line AB as C // P is on the same side of the line BC as A // and // P is on the same side of the line AC as B // // A special case exits for a vertical line (inf gradient) when testing the side of the line #include <stdio.h> int test2( double px, double py, double m, double b ) { if( py < m * px + b ) { return -1; // point is under line }else if ( py == m * px + b ){ return 0; } else { return 1; // over } } int // two points lie on the same side of a line test1( double px, double py, double m,double b, double lx,double ly) { return (test2( px,py, m,b )==test2(lx,ly,m,b)); } int tritest(double x0,double y0,double x1,double y1,double x2,double y2,double px, double py){ int line1, line2, line3; // line eqns double m01 = (y1-y0)/(x1-x0); // b: y - y1 = m( x - x1 ), x = 0 double b01 = m01 * -x1 + y1; double m02, m12, b02, b12; m02 = (y2-y0)/(x2-x0); m12 = (y2-y1)/(x2-x1); b02 = m02 * -x2 + y2; b12 = m12 * -x2 + y2; // vertical line checks if( x1 == x0 ) { line1 = ( (px <= x0) == (x2 <= x0) ); } else { line1 = test1( px, py, m01, b01,x2,y2); } if( x1 == x2 ) { line2 = ( (px <= x2) == (x0 <= x2) ); } else { line2 = test1(px,py, m12, b12,x0,y0); } if( x2 == x0 ) { line3 = ( (px <= x0 ) == (x1 <= x0) );} else { line3 = test1(px, py, m02,b02,x1,y1); } return line1 && line2 && line3; } int main(void) { double x0,y0,x1,y1,x2,y2,px; double py; int scanfsReturnValueAggregatedOverAllScanfs = 0; // get input printf("Triangle Vertex A (x,y): "); scanfsReturnValueAggregatedOverAllScanfs += scanf("%lf,%lf", &x0,&y0); printf("Triangle Vertex B (x,y): "); scanfsReturnValueAggregatedOverAllScanfs += scanf("%lf,%lf", &x1,&y1); printf("Triangle Vertex C (x,y): "); scanfsReturnValueAggregatedOverAllScanfs += scanf("%lf,%lf", &x2,&y2); printf("Test Point (x,y): "); scanfsReturnValueAggregatedOverAllScanfs += scanf("%lf,%lf", &px,&py); // print error if( scanfsReturnValueAggregatedOverAllScanfs != 8 ) { printf("You're stupid and didn't put in the right inputs!\n"); return 1; } // print answer printf("Point (%.2lf,%.2lf) is ", px,py); if(tritest(x0,y0,x1,y1,x2,y2,px,py)) printf("inside"); else printf("outside"); printf(" the Triangel\n"); // return 0 return 0; }
It should behave in the following way
./integerCounter Enter a number: 1 You have entered 1 1 times Enter a number: 1 You have entered 1 2 times Enter a number: 99 You have entered 99 1 times Enter a number: 3 You have entered 3 1 times Enter a number: -1 Try again: number has to be between 0 and 99 inclusive Enter a number: 3 You have entered 3 2 times Enter a number:
#include <stdio.h> int main(void) { int x = 'B'; printf("%c\n",x+1); return 0; }
upper_case.c
which reads characters from its input
and convert all lower case letters to upper case.
The only functions you can use are getchar
and putchar
.
For example:
./upper_case I'm very angry that you ate my chips!!!!!! I'M VERY ANGRY THAT YOU ATE MY CHIPS!!!!!!
encrypt.c
that encrypts a message by
shifting letters one position up in the alphabet: 'a' => 'b', 'b' => 'c' ... 'y' => 'z'
and shifts 'z' => 'a'
It should do the same for upper case leters.
Other characters should be unchanged.
The only functions you can use are getchar
and putchar
.
For example:
./encrypt Zulu Kilo Alpha proceed to Foxtrot Juliet at 1900 HOURS. Avmv Ljmp Bmqib qspdffe up Gpyuspu Kvmjfu bu 1900 IPVST.
decrypt.c
that decrypts (inverts the encryption)
a message which
has been encrypted by encrypt.c
from the previous question.
Again the only functions you can use are getchar
and putchar
.
For example:
./decrypt Avmv Ljmp Bmqib qspdffe up Gpyuspu Kvmjfu bu 1900 IPVST. Zulu Kilo Alpha proceed to Foxtrot Juliet at 1900 HOURS.
sum_digits.c
which reads characters from its input
and counts digits.
When the end of input is reached it should print a count of how many digits occured in its input and their sum.
The only functions you can use are getchar
and printf
.
For example:
./sum_digits 1 2 3 o'clock 4 o'clock rock Input contained 4 digits which summed to 10 ./sum_digits 12 twelve 24 twenty four thirty six 36 Input contained 6 digits which summed to 18
#include <stdio.h> int main(void) { char str[10]; str[0] = 'H'; str[1] = 'i'; printf("%s", str); 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 C Rocks line 7 characters long A very long line. line 17 characters long short line 5 characters long line 0 characters long
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>
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
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?
input_statistics.c
that for the characters provided on standard input.:
For example:
./input_statistics "Beauty is truth, truth beauty," ? that is all Ye know on earth, and all ye need to know. Input contains 27 blanks, tabs and new lines Number of words: 19 Length of shortest word: 2 Length of longest word: 8 ./input_statistics And here is another example with only one line of input!!!!!!!!! Input contains 11 blanks, tabs and new lines Number of words: 11 Length of shortest word: 2 Length of longest word: 14
letter_triangle.c
that read an positive integer n and outputs a triangle of letters
of height n as below.
For example:
./letter_triangle Enter height: 3 A BCB DEFED ./letter_triangle Enter height: 7 A BCB DEFED GHIJIHG KLMNONMLK PQRSTUTSRQP VWXYZABAZYXWV ./letter_triangle Enter height: 10 A BCB DEFED GHIJIHG KLMNONMLK PQRSTUTSRQP VWXYZABAZYXWV CDEFGHIJIHGFEDC KLMNOPQRSRQPONMLK TUVWXYZABCBAZYXWVUT