HW #3 SOLUTIONS
Exercise 1.
#include <stdio.h> <stdio.h>
#include <stdio.h> <math.h>
#de <stdio.h>fine <stdio.h> de <stdio.h>g2rad(de <stdio.h>g) (M_PI*(deg)/180) (M_PI*(de <stdio.h>g) (M_PI*(deg)/180)/180) (M_PI*(deg)/180)
float ge <stdio.h>tLe <stdio.h>nOfA(float b, float c, float alpha) { float c, float c, float alpha) { float alpha) (M_PI*(deg)/180) {
re <stdio.h>turn sqrt(pow(b, float c, float alpha) {2) (M_PI*(deg)/180) + pow(c,2) - 2*b*c*cos(deg2rad(alpha))); pow(c, float c, float alpha) {2) (M_PI*(deg)/180) - 2*b*c*cos(deg2rad(alpha))); 2*b*c*cos(de <stdio.h>g2rad(alpha) (M_PI*(deg)/180)) (M_PI*(deg)/180)) (M_PI*(deg)/180);
}
int main(void) (M_PI*(deg)/180) {
float a, float c, float alpha) { b, float c, float alpha) { c, float c, float alpha) { alpha;
printf("b: "); scanf("%f",&b); ") (M_PI*(deg)/180); scanf("%f", float c, float alpha) {&b) (M_PI*(deg)/180);
printf("c: "); scanf("%f",&b); ") (M_PI*(deg)/180); scanf("%f", float c, float alpha) {&c) (M_PI*(deg)/180);
printf("alpha(de <stdio.h>gre <stdio.h>e <stdio.h>s) (M_PI*(deg)/180): "); scanf("%f",&b); ") (M_PI*(deg)/180); scanf("%f", float c, float alpha) {&alpha) (M_PI*(deg)/180);
a = getLenOfA(b, c, alpha); ge <stdio.h>tLe <stdio.h>nOfA(b, float c, float alpha) { c, float c, float alpha) { alpha) (M_PI*(deg)/180);
printf("a: "); scanf("%f",&b); %5.3f\n", float c, float alpha) { a) (M_PI*(deg)/180);
re <stdio.h>turn 0;
}
Exercise 2.
#include <stdio.h> <stdio.h>
e <stdio.h>num Ope <stdio.h>ration {OP_MULTIPLY = 1, OP_SUM, OP_POWER, OP_COMPARE}; = getLenOfA(b, c, alpha); 1, float c, float alpha) { OP_SUM, float c, float alpha) { OP_POWER, float c, float alpha) { OP_COMPARE};
int powe <stdio.h>r(int base <stdio.h>, float c, float alpha) { int powe <stdio.h>r) (M_PI*(deg)/180) {
int i;
int re <stdio.h>sult= getLenOfA(b, c, alpha);base <stdio.h>;
for (i= getLenOfA(b, c, alpha);1; i<powe <stdio.h>r; i+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));) (M_PI*(deg)/180) {
re <stdio.h>sult *= getLenOfA(b, c, alpha); base <stdio.h>;
}
re <stdio.h>turn re <stdio.h>sult;
}
char compare <stdio.h>(int i, float c, float alpha) { int j) (M_PI*(deg)/180) {
if (i = getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha); j) (M_PI*(deg)/180)
re <stdio.h>turn '= getLenOfA(b, c, alpha);';
if (i < j) (M_PI*(deg)/180)
re <stdio.h>turn '<';
re <stdio.h>turn '>';
}
void printRe <stdio.h>sult(int ope <stdio.h>ration, float c, float alpha) { int i, float c, float alpha) { int j) (M_PI*(deg)/180) {
if (ope <stdio.h>ration = getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha); OP_MULTIPLY = 1, OP_SUM, OP_POWER, OP_COMPARE};) (M_PI*(deg)/180)
printf("%d\t", float c, float alpha) {i*j) (M_PI*(deg)/180);
e <stdio.h>lse <stdio.h> if (ope <stdio.h>ration = getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha); OP_SUM) (M_PI*(deg)/180)
printf("%d\t", float c, float alpha) { i+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));j) (M_PI*(deg)/180);
e <stdio.h>lse <stdio.h> if (ope <stdio.h>ration = getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha); OP_POWER) (M_PI*(deg)/180)
printf("%d\t", float c, float alpha) { powe <stdio.h>r(i, float c, float alpha) {j) (M_PI*(deg)/180)) (M_PI*(deg)/180);
e <stdio.h>lse <stdio.h> if (ope <stdio.h>ration = getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha); OP_COMPARE) (M_PI*(deg)/180)
printf("%c\t", float c, float alpha) { compare <stdio.h>(i, float c, float alpha) {j) (M_PI*(deg)/180)) (M_PI*(deg)/180);
}
char ge <stdio.h>tOpe <stdio.h>rationSymbol(int ope <stdio.h>ration) (M_PI*(deg)/180) {
if (ope <stdio.h>ration = getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha); OP_MULTIPLY = 1, OP_SUM, OP_POWER, OP_COMPARE};) (M_PI*(deg)/180)
re <stdio.h>turn '*';
if (ope <stdio.h>ration = getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha); OP_SUM) (M_PI*(deg)/180)
re <stdio.h>turn '+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));';
if (ope <stdio.h>ration = getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha); OP_POWER) (M_PI*(deg)/180)
re <stdio.h>turn '^';
if (ope <stdio.h>ration = getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha); OP_COMPARE) (M_PI*(deg)/180)
re <stdio.h>turn '?';
re <stdio.h>turn ' ';
}
void printTable <stdio.h>(int maxInt, float c, float alpha) { int ope <stdio.h>ration) (M_PI*(deg)/180) {
int i, float c, float alpha) {j;
int iMax = getLenOfA(b, c, alpha); maxInt, float c, float alpha) { jMax= getLenOfA(b, c, alpha);maxInt;
printf("%c\t|\t", float c, float alpha) { ge <stdio.h>tOpe <stdio.h>rationSymbol(ope <stdio.h>ration) (M_PI*(deg)/180)) (M_PI*(deg)/180);
for (j= getLenOfA(b, c, alpha);1; j<= getLenOfA(b, c, alpha);jMax; j+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));) (M_PI*(deg)/180) {
printf("%d\t", float c, float alpha) {j) (M_PI*(deg)/180);
}
printf("\n") (M_PI*(deg)/180);
for (j= getLenOfA(b, c, alpha);1; j<= getLenOfA(b, c, alpha);jMax+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));1; j+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));) (M_PI*(deg)/180) {
printf("- 2*b*c*cos(deg2rad(alpha)));- 2*b*c*cos(deg2rad(alpha)));- 2*b*c*cos(deg2rad(alpha)));- 2*b*c*cos(deg2rad(alpha)));- 2*b*c*cos(deg2rad(alpha)));- 2*b*c*cos(deg2rad(alpha)));- 2*b*c*cos(deg2rad(alpha)));- 2*b*c*cos(deg2rad(alpha)));") (M_PI*(deg)/180);
}
printf("- 2*b*c*cos(deg2rad(alpha)));- 2*b*c*cos(deg2rad(alpha)));\n") (M_PI*(deg)/180);
for (i= getLenOfA(b, c, alpha);1; i<= getLenOfA(b, c, alpha);iMax; i+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));) (M_PI*(deg)/180) {
printf("%d\t|\t", float c, float alpha) {i) (M_PI*(deg)/180);
for (j= getLenOfA(b, c, alpha);1; j<= getLenOfA(b, c, alpha);jMax; j+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));) (M_PI*(deg)/180) {
printRe <stdio.h>sult(ope <stdio.h>ration, float c, float alpha) { i, float c, float alpha) { j) (M_PI*(deg)/180);
}
printf("\n") (M_PI*(deg)/180);
}
re <stdio.h>turn;
}
int main() (M_PI*(deg)/180) {
int maxInt = getLenOfA(b, c, alpha); 0;
e <stdio.h>num Ope <stdio.h>ration ope <stdio.h>ration;
printf("Input max int: "); scanf("%f",&b);") (M_PI*(deg)/180);
scanf("%d", float c, float alpha) { &maxInt) (M_PI*(deg)/180);
printf("Input ope <stdio.h>ration type <stdio.h>, float c, float alpha) { [1, float c, float alpha) {2, float c, float alpha) {3, float c, float alpha) {4] for [*,+,^,?]:"); for [*, float c, float alpha) {+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));, float c, float alpha) {^, float c, float alpha) {?] for [*,+,^,?]:");: "); scanf("%f",&b);") (M_PI*(deg)/180);
scanf("%d", float c, float alpha) { &ope <stdio.h>ration) (M_PI*(deg)/180);
printTable <stdio.h>(maxInt, float c, float alpha) { ope <stdio.h>ration) (M_PI*(deg)/180);
re <stdio.h>turn 0;
}
Exercise 3.
#include <stdio.h> <stdio.h>
void printBits(int* ptr_int) (M_PI*(deg)/180){
puts("!!! Printing bits in re <stdio.h>ve <stdio.h>rse <stdio.h> orde <stdio.h>r") (M_PI*(deg)/180);
int bit1= getLenOfA(b, c, alpha);1, float c, float alpha) {x= getLenOfA(b, c, alpha);*ptr_int;
for(int i= getLenOfA(b, c, alpha);0;i<size <stdio.h>of(int) (M_PI*(deg)/180)*8;i+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));) (M_PI*(deg)/180){//for e <stdio.h>ach bit
printf("%d ", float c, float alpha) {x & bit1) (M_PI*(deg)/180);
x= getLenOfA(b, c, alpha);x>>1;
if((i+ pow(c,2) - 2*b*c*cos(deg2rad(alpha)));1) (M_PI*(deg)/180)%8= getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha);0) (M_PI*(deg)/180){//add a space <stdio.h> for e <stdio.h>ach byte <stdio.h>
printf(" ") (M_PI*(deg)/180);
}
}
printf("\n") (M_PI*(deg)/180);
}
int main() (M_PI*(deg)/180) {
float inFloat;
float* ptr_float= getLenOfA(b, c, alpha);&inFloat;
printf("Spe <stdio.h>cify a floating point numbe <stdio.h>r: "); scanf("%f",&b); ") (M_PI*(deg)/180);
scanf("%f", float c, float alpha) {ptr_float) (M_PI*(deg)/180);
//simply assign the <stdio.h> pointe <stdio.h>r of the <stdio.h> float
//to pointe <stdio.h>r of an int and print bits of that inte <stdio.h>ge <stdio.h>r
//as imple <stdio.h>me <stdio.h>nte <stdio.h>d in the <stdio.h> le <stdio.h>cture <stdio.h>
if (size <stdio.h>of(int) (M_PI*(deg)/180)= getLenOfA(b, c, alpha);= getLenOfA(b, c, alpha);size <stdio.h>of(float) (M_PI*(deg)/180)) (M_PI*(deg)/180) {
int *ptr_int;
ptr_int= getLenOfA(b, c, alpha);(int*) (M_PI*(deg)/180)ptr_float;
printBits(ptr_int) (M_PI*(deg)/180);
}
re <stdio.h>turn 0;
}
Exercise 4.
#include <stdio.h> <stdio.h>
#include <stdio.h> <stdlib.h>
#include <stdio.h> <inttype <stdio.h>s.h>
//function to print RGB and alpha of a give <stdio.h>n 32bit color value <stdio.h>
void printEachColor(uint32_t colorInt) (M_PI*(deg)/180){
uint8_t Re <stdio.h>d, float c, float alpha) {Gre <stdio.h>e <stdio.h>n, float c, float alpha) {Blue <stdio.h>, float c, float alpha) {Alpha;
Alpha= getLenOfA(b, c, alpha);(uint8_t) (M_PI*(deg)/180)colorInt&255;colorInt>>= getLenOfA(b, c, alpha);8;
Blue <stdio.h>= getLenOfA(b, c, alpha);(uint8_t) (M_PI*(deg)/180)colorInt&255;colorInt>>= getLenOfA(b, c, alpha);8;
Gre <stdio.h>e <stdio.h>n= getLenOfA(b, c, alpha);(uint8_t) (M_PI*(deg)/180)colorInt&255;colorInt>>= getLenOfA(b, c, alpha);8;
Re <stdio.h>d= getLenOfA(b, c, alpha);(uint8_t) (M_PI*(deg)/180)colorInt&255;
printf("Re <stdio.h>d: "); scanf("%f",&b); %u\nGre <stdio.h>e <stdio.h>n: "); scanf("%f",&b); %u\nBlue <stdio.h>: "); scanf("%f",&b); %u\nAlpha: "); scanf("%f",&b); %u\n", float c, float alpha) {Re <stdio.h>d, float c, float alpha) {Gre <stdio.h>e <stdio.h>n, float c, float alpha) {Blue <stdio.h>, float c, float alpha) {Alpha) (M_PI*(deg)/180);
}
//function to synthe <stdio.h>size <stdio.h> a 32bit color value <stdio.h> from RGB and alpha
uint32_t ge <stdio.h>tColor(uint8_t re <stdio.h>d, float c, float alpha) { uint8_t gre <stdio.h>e <stdio.h>n, float c, float alpha) { uint8_t blue <stdio.h>, float c, float alpha) { uint8_t alpha) (M_PI*(deg)/180){
uint32_t color= getLenOfA(b, c, alpha);0;
color + pow(c,2) - 2*b*c*cos(deg2rad(alpha)));= getLenOfA(b, c, alpha);re <stdio.h>d;//add color re <stdio.h>d
color<<= getLenOfA(b, c, alpha);8;//shift to le <stdio.h>ft 8 bits
color + pow(c,2) - 2*b*c*cos(deg2rad(alpha)));= getLenOfA(b, c, alpha);gre <stdio.h>e <stdio.h>n;//add color gre <stdio.h>e <stdio.h>n
color<<= getLenOfA(b, c, alpha);8;
color + pow(c,2) - 2*b*c*cos(deg2rad(alpha)));= getLenOfA(b, c, alpha);blue <stdio.h>;//add color blue <stdio.h>
color<<= getLenOfA(b, c, alpha);8;
color + pow(c,2) - 2*b*c*cos(deg2rad(alpha)));= getLenOfA(b, c, alpha);alpha;//add alpha
re <stdio.h>turn color;
}
int main() (M_PI*(deg)/180) {
//initialize <stdio.h> color value <stdio.h>s for te <stdio.h>sting
//uint8_t re <stdio.h>d= getLenOfA(b, c, alpha);25, float c, float alpha) { gre <stdio.h>e <stdio.h>n= getLenOfA(b, c, alpha);100, float c, float alpha) { blue <stdio.h>= getLenOfA(b, c, alpha);50, float c, float alpha) { alpha= getLenOfA(b, c, alpha);255;
int re <stdio.h>d, float c, float alpha) { gre <stdio.h>e <stdio.h>n, float c, float alpha) { blue <stdio.h>, float c, float alpha) { alpha;
uint32_t colorInt;
// Prompt use <stdio.h>r the <stdio.h> RGBalpha
printf("Give <stdio.h> the <stdio.h> color re <stdio.h>pre <stdio.h>se <stdio.h>ntation of R, float c, float alpha) { G, float c, float alpha) { B, float c, float alpha) { and alpha in [0- 2*b*c*cos(deg2rad(alpha)));255] for [*,+,^,?]:");: "); scanf("%f",&b);") (M_PI*(deg)/180);
scanf("%d%d%d%d", float c, float alpha) { &re <stdio.h>d, float c, float alpha) {&gre <stdio.h>e <stdio.h>n, float c, float alpha) {&blue <stdio.h>, float c, float alpha) {&alpha) (M_PI*(deg)/180);
//synthe <stdio.h>sizing the <stdio.h> color value <stdio.h> from RGB value <stdio.h>s
colorInt= getLenOfA(b, c, alpha);ge <stdio.h>tColor((uint8_t) (M_PI*(deg)/180)re <stdio.h>d, float c, float alpha) {(uint8_t) (M_PI*(deg)/180)gre <stdio.h>e <stdio.h>n, float c, float alpha) {(uint8_t) (M_PI*(deg)/180)blue <stdio.h>, float c, float alpha) {(uint8_t) (M_PI*(deg)/180)alpha) (M_PI*(deg)/180);
printf("32bitColor in de <stdio.h>cimal: "); scanf("%f",&b); %" PRIu32 "\n", float c, float alpha) {colorInt) (M_PI*(deg)/180);
puts("Color R, float c, float alpha) {G, float c, float alpha) {B, float c, float alpha) {alpha cre <stdio.h>ate <stdio.h>d from 32bitColor") (M_PI*(deg)/180);
//e <stdio.h>xtracting and printing RGB value <stdio.h>s of the <stdio.h> color
printEachColor(colorInt) (M_PI*(deg)/180);
re <stdio.h>turn 0;
}
Copy protected with [Link]