C Practice Tasks

The following are a series of tasks intended to provide initial experience and/or practice in writing C programs. Each task will require the use of printf() statements(see printf). For tasks that are more complicated, consider first writing either pseudocode or drawing a flow chart in order to plan you attack.

Task 1

Part 1

Implement the equation \(x/y\), using ONLY INTEGERS such that if \(x < y\), the result is not zero, assuming \(x,y > 0\). Also define x and y as global variables assuming they’ll exist in the range of 0 to 1000. Save the result in a local variable of appropriate size and print it out. Give x and y various values, either in the variable definition or as value assignments within the program, and run the program to ensure correctness. Note that you will use the variables x and y for the rest of the tasks as well.

Part 2

Modify Part 1 such that if y == 0 the program will print out a warning message indicating an invalid divide by 0; otherwise, the programs continues as normal.

Task 2

Set bits 0-7 in x to 1 and the rest to 0 using a HEX number. Set y such that it is the decimal equivalent of 10000 using HEX. Using only the bitwise operations & and |, make it so only BIT8 in each variable is 1 (all else are zero) while only changing the bits that you need to change (e.g., if BIT11 is already 0, don’t force it to 0, leave it alone). Print out the values of each in both decimal and hexadecimal form before and after the bitwise operations.

Task 3

Test the value of x to see if BIT4 is 1 or 0 - if 1, print BIT4 of X is TRUE; otherwise, print BIT4 of X is FALSE. Test the program, change the value of BIT4 in x and run again to ensure the value check works.

Task 4

Part 1

Use the C function rand() to produce a random number and save it into variable y. Keep doing this until the random number produced is larger than 30000. For each random number generated, print out the value. Also keep track of how many numbers have been generated and print that out as well (make a new variable). Note that rand() produces a random number between 0 and 0x7FFF (32767). Calling of functions and saving of the output is done the same way as in python and MATLAB (at least for simple functions).

Part 2

Modify the Part 1 answer in order to stop generating random numbers after 10 tries.

Part 3

Assuming Task 4.1 and 4.2 was done using while loops, rewrite the task to use a for loop instead or vice versa. The break command is useful here: break will immediately exit the innermost loop.

Task 5

Part 1

Write code using only the variables x and y such that the output below is produced:

1
1 2
1 2 3
1 2 3 4
... (continue the pattern)
1 2 3 4 5 6 7 8 9 10
Done!

Hint

Using two loops, one inside the other (known as “nesting”) is a good way to do this. Of course, you could manually make 11 print statements to do this manually, but what if the maximum number we want to print is 1000?

Hint

You can avoid moving to the new line by omitting the rn within the printf() statement. After a line is completed, a printf() with just the new line command may be issued to cause the next one to start fresh.

Part 2

Think of (and test) two different ways to cause Task 5.1 to only print the ODD numbers.

Hint

The modulus operator % may be useful here, or maybe a bit test.

Part 3

Modify Task 5.1 or Task 5.2 such that the whole print structure is reversed:

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
...
1
Done!