Bsm1501 Assignment Definition

BSM1501/101/3/2017 1 1. INTRODUCTION Dear Student Welcome to UNISA and in particular to Business Management 1A. I am Ms Adele van Lille, your lecturer, and am assisted by Ms Elsa Kruger. This tutorial letter provides you with general information and introduces you to this module. Read it through carefully as it contains important information regarding assignments, year marks and the examination. Because this is a blended module, you need to access the website for BSM1501 on myUnisafrequently.Although you will receive extensive guidance from the assignments, tutorial letters and study material, you are more than welcome to contact us if you need to. We are fully aware that you sometimes study under very difficult circumstances and we therefore invite you to contact us if you require information, encounter problems, or if you want to discuss any aspect of the work. It is very important to us that you pass this module and we will do our best to assist you in this regard. If you would like to discuss anything with us, please feel free to phone us, or make an appointment to see us personally. You are welcome to phone us on any weekday between 08:00 and 16:00. If you would like to see us in person, we would appreciate it if you make an appointment first to ensure that we will be available. We wish you success in your studies. We hope that you will not only pass the examination with flying colours, but also thoroughly enjoy the course.

Assignment #2 Answers

Introductory C Programming

UW Experimental College

Assignment #2 ANSWERS

Question 1. What are the two different kinds of division that the operator can do? Under what circumstances does it perform each?

The two kinds of division are integer division, which discards any remainder, and floating-point division, which yields a floating-point result (with a fractional part, if necessary). Floating-point division is performed if either operand (or both) is floating-point (that is, if either number being operated on is floating-point); integer division is performed if both operands are integers.

Question 2. What are the definitions of the ``Boolean'' values true and false in C?

False is always represented by a zero value. Any nonzero value is considered true. The built-in operators , , , , , , , , and always generate 0 for false and 1 for true.

Question 3. Name three uses for the semicolon in C.

Terminating declarations, terminating statements, and separating the three control expressions in a loop.

Question 4. What would the equivalent code, using a while loop, be for the example

for(i = 0; i < 10; i = i + 1) printf("i is %d\n", i); ?

Just move the first (initialization) expression to before the loop, and the third (increment) expression to the body of the loop:

i = 0; while(i < 10) { printf("i is %d\n", i); i = i + 1; }

Question 5. What is the numeric value of the expression ?

1 (or ``true''), because 3 is in fact less than 4.

Question 6. Under what conditions will this code print ``water''?

if(T < 32) printf("ice\n"); else if(T < 212) printf("water\n"); else printf("steam\n");

If is greater than or equal to 32 and less than 212. (If you said ``greater than 32 and less than 212'', you weren't quite right, and this kind of distinction--paying attention to the difference between ``greater than'' and ``greater than or equal''--is often extremely important in programming.)

Question 7. What would this code print?

int x = 3; if(x) printf("yes\n"); else printf("no\n");

It would print ``yes'', since is nonzero.

Question 8. What would this code print?

int i; for(i = 0; i < 3; i = i + 1) printf("a\n"); printf("b\n"); printf("c\n");

It would print

a a a b c The indentation of the statement is (deliberately, for the sake of the question) misleading. It looks like it's part of the body of the statement, but the body of the statement is always a single statement, or a list of statements enclosed in braces {}. In this case, the body of the loop is the call . The two statements and are normal statements following the loop.

The code would be much clearer if the line were indented to line up with the line. If the intent was that ``b'' be printed 3 times, along with ``a'', it would be necessary to put braces around the pair of lines and .

Exercise 1. Write a program to find out how many of the numbers from 1 to 10 are greater than 3.

#include <stdio.h> int main() { int i; int count = 0; for(i = 1; i <= 10; i = i + 1) { if(i > 3) count = count + 1; } printf("%d numbers were greater than 3\n", count); return 0; }

Tutorial 4b. Print ``On the first day'' instead of ``On the 1 day'' in the days-of-Christmas program.

Simply replace the lines

printf("On the %d day of Christmas, ", day); printf("my true love gave to me\n"); with printf("On the "); if(day == 1) printf("first"); else if(day == 2) printf("second"); else if(day == 3) printf("third"); else if(day == 4) printf("fourth"); else if(day == 5) printf("fifth"); else if(day == 6) printf("sixth"); else printf("%d", day); printf(" day of Christmas, "); printf("my true love gave to me\n"); The final else printf("%d", day); is an example of ``defensive programming.'' If the variable ever ends up having a value outside the range 1-6 (perhaps because we later added more verses to the song, but forgot to update the list of words), the program will at least print something.

Exercise 2. Write a program to compute the average of the ten numbers 1, 4, 9, ..., 81, 100.

#include <stdio.h> int main() { int i; float sum = 0; int n = 0; for(i = 1; i <= 10; i = i + 1) { sum = sum + i * i; n = n + 1; } printf("the average is %f\n", sum / n); return 0; }

Exercise 3. Write a program to print the numbers between 1 and 10, along with an indication of whether each is even or odd.

#include <stdio.h> int main() { int i; for(i = 1; i <= 10; i = i + 1) { if(i % 2 == 0) printf("%d is even\n", i); else printf("%d is odd\n", i); } return 0; }

Exercise 4. Print out the 8 points of the compass, based on the and components of the direction of travel.

if(x > 0) { if(y > 0) printf("Northeast.\n"); else if(y < 0) printf("Southeast.\n"); else printf("East.\n"); } else if(x < 0) { if(y > 0) printf("Northwest.\n"); else if(y < 0) printf("Southwest.\n"); else printf("West.\n"); } else { if(y > 0) printf("North.\n"); else if(y < 0) printf("South.\n"); else printf("Nowhere.\n"); } (You will notice that this code also prints ``Nowhere'' in the ninth case, namely when and .)

Exercise 5. Write a program to print the first 7 positive integers and their factorials.

#include <stdio.h> int main() { int i; int factorial = 1; for(i = 1; i <= 7; i = i + 1) { factorial = factorial * i; printf("%d %d\n", i, factorial); } return 0; }

The answer to the extra credit question is that 8!, which is how mathematicians write ``eight factorial,'' is 40320, which is not guaranteed to fit into a variable of type . To write a portable program to compute factorials higher than 7, we would have to use .

Exercise 6. Write a program to print the first 10 Fibonacci numbers.

#include <stdio.h> int main() { int i; int fibonacci = 1; int prevfib = 0; int tmp; for(i = 1; i <= 10; i = i + 1) { printf("%d %d\n", i, fibonacci); tmp = fibonacci; fibonacci = fibonacci + prevfib; prevfib = tmp; } return 0; } (There are many ways of writing this program. As long as the code you wrote printed 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, it's probably correct. If the code you wrote is clearer than my solution above, so much the better!)

Exercise 7. Make some improvements to the prime number printing program.

Here is a version incorporating both of the suggested improvements:

#include <stdio.h> #include <math.h> int main() { int i, j; double sqrti; printf("%d\n", 2); for(i = 3; i <= 100; i = i + 2) { sqrti = sqrt(i); for(j = 2; j < i; j = j + 1) { if(i % j == 0) break; /* not prime */ if(j > sqrti) { /* prime */ printf("%d\n", i); break; } } } return 0; }

This page by Steve Summit // Copyright 1995-9 // mail feedback

0 thoughts on “Bsm1501 Assignment Definition”


Leave a Comment

Your email address will not be published. Required fields are marked *