Session 2 Summary This is all here: http://www.bath.ac.uk/%7Epy8ieh/academic/ccourse/ ***** Q2.1: #include /* for input/output */ main() { int integerVariable1, integerVariable2; printf("Lab Session 2, Question 2.1\n"); /* Display program header */ integerVariable1 = 1; /* Assign first number */ integerVariable2 = 2; /* Assign second number */ printf("result: %i+%i=%i\n", integerVariable1, integerVariable2, integerVariable1+integerVariable2); /* Add numbers together and print the result */ } /* OUTPUT: Lab Session 2, Question 2.1 result: 1+2=3 */ ***** Q2.2: #include /* for input/output */ main() { float floatVariable1, floatVariable2; printf("Lab Session 2, Question 2.2\n"); /* Display program header */ floatVariable1 = 0.1; /* Assign first number */ floatVariable2 = 0.2; /* Assign second number */ printf("result: %f+%f=%f\n", floatVariable1, floatVariable2, floatVariable1+floatVariable2); /* Add numbers together and print the result */ } /* OUTPUT: Lab Session 2, Question 2.2 result: 0.100000+0.200000=0.300000 */ ***** Q2.3: #include /* for input/output */ main() { float floatVariable1, floatVariable2; printf("Lab Session 2, Question 2.3\n"); /* Display program header */ printf("Enter two floating point numbers separated by a space: "); scanf("%f %f", &floatVariable1, &floatVariable2); /* Get the numbers from the user */ printf("result: %f+%f=%f\n", floatVariable1, floatVariable2, floatVariable1+floatVariable2); /* Add numbers together and print the result */ } /* OUTPUT: Lab Session 2, Question 2.3 Enter two floating point numbers separated by a space: 3.2e-1 1e-2 result: 0.320000+0.010000=0.330000 */ ***** Q2.4: #include /* for input/output */ main() { char TheLovelyCharacterThatTheUserGaveUs; printf("Lab Session 2, Question 2.4\n"); /* Display program header */ printf("Enter a lovely character: "); /* Prompt the user */ scanf("%c", &TheLovelyCharacterThatTheUserGaveUs); /* Get the character from the user */ printf("You typed: '%c'\n", TheLovelyCharacterThatTheUserGaveUs); /* Print it out again */ } /* OUTPUT: Lab Session 2, Question 2.4 Enter a lovely character: g You typed: 'g' */ ***** Q2.5: #include /* for input/output */ /* WARNING: This program does not accept multibyte characters!!! */ main() { char TheLovelyCharacterThatTheUserGaveUs; printf("Lab Session 2, Question 2.5\n"); /* Display program header */ printf("Enter a lovely character: "); /* Prompt the user */ scanf("%c", &TheLovelyCharacterThatTheUserGaveUs); /* Get the character from the user */ printf("I think that that character has the integer value %i.\n", TheLovelyCharacterThatTheUserGaveUs); /* Print it out again, effectively casting the character as an integer (hmm!). */ } /* OUTPUT: Lab Session 2, Question 2.5 Enter a lovely character: 6 I think that that character has the integer value 54. */ /* SEQUENCE: space, numbers, capital letters, lowercase letters, symbols interspersed everywhere. ' ' = 32 '0' = 48 '1' = 49 '9' = 57 'A' = 65 'Z' = 90 'a' = 97 'z' = 122 */ ***** Q3.1: main() { float x; x = sin(30); printf("%f\n",x); } /* COMPILER OUTPUT: Compiling was fine, but sin(30) gave answer 30.000!!! :-/ */ /* REASON: Did not refer to the math library so sin() is unknown. */ ***** Q3.2: #include /* for input/output */ #include /* for sin */ main() { float x; x = sin(30); printf("%f\n",x); } /* OUTPUT: -0.988032 */ /* NOTES: Added #include ...to correct problem. */ /* NOTES: Unlike the expectations of others, sin(30) will of course give sin(30 radians) not sin(30 degrees). */ ***** Q3.3: #include /* for input/output */ #include /* for asin */ main() { float x; x = asin(1.0); printf("%f\n",x); } /* OUTPUT: 1.570796 */ /* (which is pi/2) */ ***** Q3.4: #include /* for input/output */ #include /* for cbrt, sqrt, hypot */ main() { float cbrtResult, sqrtResult, hypotResult; cbrtResult = cbrt(27); /* cube root of 27 */ sqrtResult = sqrt(9); /* square root of 9 */ hypotResult = hypot(3,4); /* the hypotenuse of the 3,4,5 triangle */ printf("cbrt(27) = %f\nsqrt(9) = %f\nhypot(3,4) = %f\n", cbrtResult, sqrtResult, hypotResult); /* print results */ } /* OUTPUT: cbrt(27) = 3.000000 sqrt(9) = 3.000000 hypot(3,4) = 5.000000 */ /* cbrt - cube root sqrt - square root hypot - find the hypotenuse of a right angled triangle */ ***** Q4.1: #include /* for input/output */ main() { int x=10; printf("Lab Session 2, Question 4.1\n"); /* Display program header */ while(--x) { printf("%f\n",(float)5/x); } } /* OUTPUT: 0.555556 0.625000 0.714286 0.833333 1.000000 1.250000 1.666667 2.500000 5.000000 */ /* ANSWER: what it does: start at 9 and count down to 1, displaying five divided by the number for each step of the way. float statement: to cast the 5/x as a float result, otherwise it would be trying to do it as an integer calculation which would be a lot less accurate (and which the %f specifier would not interpret correctly). stop: the loop stops executing when x reaches 0, since it is reduced by one before each loop. */ ***** Q5.2: #include /* for input/output */ #include /* for pow */ main() { double y; int bits = 0; /* set bits to 0 */ int base = 2; /* assume computer uses binary */ do { bits++; /* increase number of bits known to exist */ y = pow(base, bits+1); /* test next number of bits */ } while (y != y+1); /* stop if 1 is no longer significant */ printf ("C uses %d bits for the mantissa of a double.\n", bits); } ***** Q5.4: /* COMPILATION ERRORS "s3q5-2.c", line 10: syntax error before or at: y cc: acomp failed for s3q5-2.c -- solved by linking in the math library */ ***** Q5.5: /* OUTPUT: C uses 52 bits for the mantissa of a double. */ /* This is what the documentation says! */ ****** END;