plot - Print a representation of the Pythagorean Triple in C -
i trying create program prints mapping data found pythagorean triples in c. far, have coded program able find triples.
#include <stdio.h> #include <math.h> int main (int argc, char * argv[]) { int a, b, c; int a2, b2, c2; int limit = 60; (a = 1; <= limit; a++) { a2 = * a; (b = 1; b <= limit; b++) { b2 = b * b; (c = 0; c <= ((int)sqrt(a2+b2)); c++) { c2 = c * c; if (a < b && (c2 == (a2 + b2))) { printf("triple: %d %d %d\n", a, b, c); } } } } } the intended output expected in format:
123456789012345678901234567890123456789012345678901234567890 1\ 2 \ 3 \ 4 *\ 5 \ 6 \ 7 \ 8 * \ 9 \ 0 \ 1 \ 2 * * \ i trying write loop this, cannot think of how print in way. suggestions?
update: managed print x , y axis (x = , y = b). values correct, mapping part left.
(int x = 0; x < a; x++) { // x-axis = printf("%d ", x); } printf("\n"); (int y = 0; y < b; y++) { // y-axis = b printf("%d\n", y); } update: modified code, output printing, having problems printing spaces. tried manually adding spaces " \ " , " * " distorts whole image.
#include <stdio.h> #include <math.h> #include <stdbool.h> bool is_perfect_square(int num); int main (int argc, char * argv[]) { int a, b, c; int a2, b2, c2; int limit = 60; bool flag = false; (a = 1; <= limit; a++) { a2 = * a; (b = 1; b <= limit; b++) { b2 = b * b; (c = 0; c <= ((int)sqrt(a2+b2)); c++) { c2 = c * c; if (a < b && (c2 == (a2 + b2))) { // printf("triple: %d %d %d\n", a, b, c); } } } } (int x = 0; x < a; x++) { (int y = 0; y < b; y++) { if (x == 0) { printf("%d ", ((y+1)% 10)); } else if (y == 0) { printf("%d ", (x % 10)); } else if (x == y) { printf("\\"); } else if (is_perfect_square((x*x) + (y*y))) { printf("*"); } } printf("\n"); } } bool is_perfect_square (int num) { int root = (int)(sqrt(num)); if (num == root * root) { return true; } else { return false; } } still working on possible solution.
hint :
have nested loop index i,j;
for 0.. limit { j 0 .. limit { /*implement below algorithm here!!*/ } printf("\n") } algorithm used inside loop:
- if
i == 0print x axis values printing(j+1)% 10[see note @ end] - else if
j == 0print y axis values printingi % 10 - else if
i == jprint'\' - else if is_perfect_square(
(i*i) + (j*j)) returns 1, print'*' - else print space.
specifications is_perfect_square function: function returns 1 if input perfect square , 0 otherwise. example:
is_perfect_square(25)shouldreturn 1is_perfect_square(7)shouldreturn 0is_perfect_square(49)shouldreturn 1
note: i == 0 case should print j%10 start output 0 represent origin. output provided in question starts 1. hence using (j+1)%10
you might need handle corner cases, should straight forward once algorithm implemented in code.
Comments
Post a Comment