7)

Program 6 - weight conversion

Program 6 Weight conversion Source code - prog6.c
The program below displays a value in drams as pounds, ounzes and drams. It doesn't use anything new, but presents a larger program than previous programs and shows that the order that the lines of code are placed in is important. An alternative for the same program using division and modulus is also shown in the description.

There are 256 drams to the pound, and 16 drams to an ounze.

 

Program code

#include <stdio.h>

void main(void)
{

int drams=1234,lbs,ozs,drs;
lbs=drams/256;
drams=drams-(lbs*256);
ozs=drams/16;
drs=drams-(ozs*16);
printf("%d lbs %d ozs %d drs",lbs,ozs,drs);

}

 

Description of the program code
The value of the drams variable is the input to be converted ( in the example it is 1234 ). The value of the lbs variable is calculated by dividing the drams by 256. Because the lbs variable was defined as an integer ( whole number ) only the whole number part of the equation is calculated ( rounded down ). This value is then multiplied by 256 and subtracted from the drams value to give the number of drams remaining after the pounds is taken away. It then goes on to use this value to calculate the ounzes and any remaining drams.

The only thing to note about this program is the use of brackets drams=drams-(lbs*256); they are not needed and can be left out, however the program is more readable with them, so they are included.

There is an alternative shown below, using division and modulus, which does exactly the same thing.

#include <stdio.h>

void main(void)
{

int drams=1234,lbs,ozs,drs;
lbs=drams/256;
drams=drams%256;
ozs=drams/16;
drs=drams%16;
printf("%d lbs %d ozs %d drs",lbs,ozs,drs);

}

 

Summary
This program can be quite difficult to follow, it may help if you follow the code through manually and at each stage right down the values of the variables on paper, like below : ( the changes made to variables at each line are shown in bold red )

After line 1 : int drams=1234,lbs,ozs,drs;

Variable Value
drams 1234
lbs ?
ozs ?
drs ?

After line 2 : lbs=drams/256;

Variable Value
drams 1234
lbs 4
ozs ?
drs ?

After line 3 : drams=drams-(lbs*256);

Variable Value
drams 210
lbs 4
ozs ?
drs ?

After line 4 : ozs=drams/16;

Variable Value
drams 210
lbs 4
ozs 13
drs ?

After line 5 : drs=drams-(ozs*16);

Variable Value
drams 210
lbs 4
ozs 13
drs 2

The final line, printf("%d lbs %d ozs %d drs",lbs,ozs,drs); of the program just displays the values of the variables lbs, ozs and drs and nothing is changed.

The next stage is to do the same program, but this time instead of hard-coding in the drams value, the program will ask the user of the program to enter it.

 

Tasks

6.1) Try to trace the 2nd program given ( like in the summary ) and note the differences, if any ?

6.2) Write the same program the opposite way around, ie. covert three values given in the lbs, ozs, and drs variables to drams. Eg if lbs was 1, ozs 0, and drs 0 then output would be 256 drams.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001