Solution to Self Check
Short Answers
1. How many different values can be stored using 8 bits?
2^8 = 256
2. What are the similarity/differences between float and double data type?
- Similarities:
- both are are are used to store numbers with decimal places
- both use some of their memory to store the mantissa and some to store the exponent
- Differences:
- different amounts of memory used for each variables of these types
- doubles have higher accuracy than floats
3. What is the best data type to store each of the values below?
-
The number of people living in Canada
- int or unsigned int. either of these options will work as Canada's population is below 2 billion. Note that often if the value is small enough, we use the signed version of the type even if it is never negative.
-
pi to 2 decimal places
- float or double. pi to 2 decimal places is 3.14. There is no need for the extra space for more accuracy as there is only 2 significant digits. However, if this is part of a series of calculations involving the value of pi, it may not be a bad idea to use doubles for accuracy. Note that in something like video games, floats are more commonly used because the amount of data is huge. Using doubles would double the memory/storage requirements to store the same object.
-
The answer to a multiple choice question where the choices are A,B,C or D
- char because the values are only going to be one of 4 letters
-
pi to 10 decimal places
- double as more accuracy is required, we need to have a larger mantissa. 10 decimal places is 11 digits in total. This would require a larger mantisa than a float can store
-
Number of muffins sold by your company
- int (or unsigned int) - hopefully no one is trying to sell you half of a muffin... Unless the company is a large multinational conglomerate, you are also not likely to sell over 2 billion/4 billion muffins. However, this program is being used by such a company then a long long int or long long would be more appropriate.
-
Taxes due on a bottle of pop
- float or double. double is more accurate so if you end up having to use this as part of a larger sequence of operations, having that accuracy is good to avoid rounding errors.
-
The number of people living in the entire world
- long long or long long int. The number of people in the world is roughly 8.1 Billion. This is a really big number and you can't store it within an int or even an unsigned int.
4. What does mantissa mean? Provide an example to illustrate your answer
The mantissa forms the significant digits of a number in scientific notation. For example 123 is in scientific notation. The mantissa for this number is 1.23
Programming Problems
Problem 1:
1. Write a program that will write out the following, note that all indentations are tabs.
The itsy bitsy spider crawled up the water spout
down came the rain to wash the spider out
then came the sun that dried up all the rain
and the itsy bitsy spider crawled up the spout again
Solution:
#include <stdio.h>
int main(void){
printf("The itsy bitsy spider crawled up the water spout\n");
printf("\tdown came the rain to wash the spider out\n");
printf("\t\tthen came the sun that dried up all the rain\n");
printf("\t\t\tand the itsy bitsy spider crawled up the spout again\n");
return 0;
}
Problem 2:
2. Write a program that will write out the following to the screen.
In DOS based systems, you use \ in your path
In UNIX based systems you use / in your path
99% of students polled love ice-cream!
"Always be wary of any helpful item that weighs less than its operating manual."
- Terry Pratchett
Solution:
#include <stdio.h>
int main(void){
printf("In DOS based systems, you use \\ in your path\n");
printf("In UNIX based systems you use / in your path\n");
printf("99%% of students polled love ice-cream!\n");
printf("\"Always be wary of any helpful item that weighs less than its operating manual.\"\n");
printf("- Terry Pratchett\n");
return 0;
}
Problem 3:
3. Re-write the hello world program so that it will make a "beep" sounds just before it ends
Solution:
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
printf("\a");
}
Problem 4:
4. Write a program that will ask the user to enter a decimal number. It will then print out the part of the number before the decimal point and after the decimal point separately. You may assume there will be no more than 9 digits before or after the the decimal point.
Solution:
#include <stdio.h>
int main(void)
{
int beforeDecimal;
int afterDecimal;
printf("Please enter a decimal number: ");
scanf("%d.%d",&beforeDecimal, &afterDecimal);
printf("the part before the decimal is %d\n",beforeDecimal);
printf("the part after the decimal is %d\n",afterDecimal);
}