Home » Archimedes archive » Acorn User » AU 1995-02.adf » !C_c » c/Arithmetic
c/Arithmetic
This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.
Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.
| Tape/disk: | Home » Archimedes archive » Acorn User » AU 1995-02.adf » !C_c |
| Filename: | c/Arithmetic |
| Read OK: | ✔ |
| File size: | 056E bytes |
| Load address: | 0000 |
| Exec address: | 0000 |
File contents
/* Program arithmetic: a contrived example of functions in C */
#include <stdio.h>
/* The variable �error� is defined outside any functions, at the lowest */
/* brace level. This means that it is visible to all the functions in */
/* the program. Here it is used as an error indicator to flag a �divide */
/* by zero error on the divide() function. */
char error = 'n';
int add(int x, int y)
{ int result;
result = x+y;
return result;
}
int subtract(int x, int y)
{ int result;
result = x-y;
return result;
}
int multiply(int x, int y)
{ return x*y ;
}
double divide(int x, int y)
{ if (y == 0)
{ error = 'z';
return 0;
}
return (double)x/(double)y;
}
main()
{ int arg1, arg2, sum, difference, product;
double dividend;
do
{ printf("Enter two numbers\n");
scanf("%d %d", &arg1, &arg2);
while (getchar() != '\n') ;
sum = add(arg1, arg2);
difference = subtract(arg1, arg2);
product = multiply(arg1, arg2);
dividend = divide(arg1, arg2);
printf("%d + %d = %d\n", arg1, arg2, sum);
printf("%d - %d = %d\n", arg1, arg2, difference);
printf("%d � %d = %d\n", arg1, arg2, product);
if (error == 'z')
{ error = 'n';
printf("Division by zero: undefined result\n");
} else
{ printf("%d � %d = %f\n", arg1, arg2, dividend);
}
}
while((arg1!=0) || (arg2!=0));
return 0;
} 00000000 2f 2a 20 50 72 6f 67 72 61 6d 20 61 72 69 74 68 |/* Program arith|
00000010 6d 65 74 69 63 3a 20 61 20 63 6f 6e 74 72 69 76 |metic: a contriv|
00000020 65 64 20 65 78 61 6d 70 6c 65 20 6f 66 20 66 75 |ed example of fu|
00000030 6e 63 74 69 6f 6e 73 20 69 6e 20 43 20 2a 2f 0a |nctions in C */.|
00000040 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f |.#include <stdio|
00000050 2e 68 3e 0a 0a 2f 2a 20 54 68 65 20 76 61 72 69 |.h>../* The vari|
00000060 61 62 6c 65 20 90 65 72 72 6f 72 91 20 69 73 20 |able .error. is |
00000070 64 65 66 69 6e 65 64 20 6f 75 74 73 69 64 65 20 |defined outside |
00000080 61 6e 79 20 66 75 6e 63 74 69 6f 6e 73 2c 20 61 |any functions, a|
00000090 74 20 74 68 65 20 6c 6f 77 65 73 74 20 2a 2f 0a |t the lowest */.|
000000a0 2f 2a 20 62 72 61 63 65 20 6c 65 76 65 6c 2e 20 |/* brace level. |
000000b0 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 |This means that |
000000c0 69 74 20 69 73 20 76 69 73 69 62 6c 65 20 74 6f |it is visible to|
000000d0 20 61 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f | all the functio|
000000e0 6e 73 20 69 6e 20 20 20 2a 2f 0a 2f 2a 20 74 68 |ns in */./* th|
000000f0 65 20 70 72 6f 67 72 61 6d 2e 20 48 65 72 65 20 |e program. Here |
00000100 69 74 20 69 73 20 75 73 65 64 20 61 73 20 61 6e |it is used as an|
00000110 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 6f 72 | error indicator|
00000120 20 74 6f 20 66 6c 61 67 20 61 20 90 64 69 76 69 | to flag a .divi|
00000130 64 65 20 2a 2f 0a 2f 2a 20 62 79 20 7a 65 72 6f |de */./* by zero|
00000140 20 65 72 72 6f 72 20 6f 6e 20 74 68 65 20 64 69 | error on the di|
00000150 76 69 64 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e |vide() function.|
00000160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | |
00000170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f | */|
00000180 0a 0a 63 68 61 72 20 65 72 72 6f 72 20 3d 20 27 |..char error = '|
00000190 6e 27 3b 0a 0a 69 6e 74 20 61 64 64 28 69 6e 74 |n';..int add(int|
000001a0 20 78 2c 20 69 6e 74 20 79 29 0a 7b 20 69 6e 74 | x, int y).{ int|
000001b0 20 72 65 73 75 6c 74 3b 0a 20 20 72 65 73 75 6c | result;. resul|
000001c0 74 20 3d 20 78 2b 79 3b 0a 20 20 72 65 74 75 72 |t = x+y;. retur|
000001d0 6e 20 72 65 73 75 6c 74 3b 0a 7d 0a 0a 69 6e 74 |n result;.}..int|
000001e0 20 73 75 62 74 72 61 63 74 28 69 6e 74 20 78 2c | subtract(int x,|
000001f0 20 69 6e 74 20 79 29 0a 7b 20 69 6e 74 20 72 65 | int y).{ int re|
00000200 73 75 6c 74 3b 0a 20 20 72 65 73 75 6c 74 20 3d |sult;. result =|
00000210 20 78 2d 79 3b 0a 20 20 72 65 74 75 72 6e 20 72 | x-y;. return r|
00000220 65 73 75 6c 74 3b 0a 7d 0a 0a 69 6e 74 20 6d 75 |esult;.}..int mu|
00000230 6c 74 69 70 6c 79 28 69 6e 74 20 78 2c 20 69 6e |ltiply(int x, in|
00000240 74 20 79 29 0a 7b 20 72 65 74 75 72 6e 20 78 2a |t y).{ return x*|
00000250 79 20 3b 0a 7d 0a 0a 64 6f 75 62 6c 65 20 64 69 |y ;.}..double di|
00000260 76 69 64 65 28 69 6e 74 20 78 2c 20 69 6e 74 20 |vide(int x, int |
00000270 79 29 0a 7b 20 69 66 20 28 79 20 3d 3d 20 30 29 |y).{ if (y == 0)|
00000280 0a 20 20 7b 20 65 72 72 6f 72 20 3d 20 27 7a 27 |. { error = 'z'|
00000290 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a |;. return 0;.|
000002a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 64 6f | }. return (do|
000002b0 75 62 6c 65 29 78 2f 28 64 6f 75 62 6c 65 29 79 |uble)x/(double)y|
000002c0 3b 0a 7d 0a 0a 6d 61 69 6e 28 29 0a 7b 20 69 6e |;.}..main().{ in|
000002d0 74 20 61 72 67 31 2c 20 61 72 67 32 2c 20 73 75 |t arg1, arg2, su|
000002e0 6d 2c 20 64 69 66 66 65 72 65 6e 63 65 2c 20 70 |m, difference, p|
000002f0 72 6f 64 75 63 74 3b 0a 20 20 64 6f 75 62 6c 65 |roduct;. double|
00000300 20 64 69 76 69 64 65 6e 64 3b 0a 20 20 64 6f 0a | dividend;. do.|
00000310 20 20 7b 20 70 72 69 6e 74 66 28 22 45 6e 74 65 | { printf("Ente|
00000320 72 20 74 77 6f 20 6e 75 6d 62 65 72 73 5c 6e 22 |r two numbers\n"|
00000330 29 3b 0a 20 20 20 20 73 63 61 6e 66 28 22 25 64 |);. scanf("%d|
00000340 20 25 64 22 2c 20 26 61 72 67 31 2c 20 26 61 72 | %d", &arg1, &ar|
00000350 67 32 29 3b 0a 20 20 20 20 77 68 69 6c 65 20 28 |g2);. while (|
00000360 67 65 74 63 68 61 72 28 29 20 21 3d 20 27 5c 6e |getchar() != '\n|
00000370 27 29 20 3b 0a 20 20 20 20 73 75 6d 20 3d 20 61 |') ;. sum = a|
00000380 64 64 28 61 72 67 31 2c 20 61 72 67 32 29 3b 0a |dd(arg1, arg2);.|
00000390 20 20 20 20 64 69 66 66 65 72 65 6e 63 65 20 3d | difference =|
000003a0 20 73 75 62 74 72 61 63 74 28 61 72 67 31 2c 20 | subtract(arg1, |
000003b0 61 72 67 32 29 3b 0a 20 20 20 20 70 72 6f 64 75 |arg2);. produ|
000003c0 63 74 20 3d 20 6d 75 6c 74 69 70 6c 79 28 61 72 |ct = multiply(ar|
000003d0 67 31 2c 20 61 72 67 32 29 3b 0a 20 20 20 20 64 |g1, arg2);. d|
000003e0 69 76 69 64 65 6e 64 20 3d 20 64 69 76 69 64 65 |ividend = divide|
000003f0 28 61 72 67 31 2c 20 61 72 67 32 29 3b 0a 20 20 |(arg1, arg2);. |
00000400 20 20 70 72 69 6e 74 66 28 22 25 64 20 2b 20 25 | printf("%d + %|
00000410 64 20 3d 20 25 64 5c 6e 22 2c 20 61 72 67 31 2c |d = %d\n", arg1,|
00000420 20 61 72 67 32 2c 20 73 75 6d 29 3b 0a 20 20 20 | arg2, sum);. |
00000430 20 70 72 69 6e 74 66 28 22 25 64 20 2d 20 25 64 | printf("%d - %d|
00000440 20 3d 20 25 64 5c 6e 22 2c 20 61 72 67 31 2c 20 | = %d\n", arg1, |
00000450 61 72 67 32 2c 20 64 69 66 66 65 72 65 6e 63 65 |arg2, difference|
00000460 29 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 25 |);. printf("%|
00000470 64 20 d7 20 25 64 20 3d 20 25 64 5c 6e 22 2c 20 |d . %d = %d\n", |
00000480 61 72 67 31 2c 20 61 72 67 32 2c 20 70 72 6f 64 |arg1, arg2, prod|
00000490 75 63 74 29 3b 0a 20 20 20 20 69 66 20 28 65 72 |uct);. if (er|
000004a0 72 6f 72 20 3d 3d 20 27 7a 27 29 0a 20 20 20 20 |ror == 'z'). |
000004b0 7b 20 65 72 72 6f 72 20 3d 20 27 6e 27 3b 0a 20 |{ error = 'n';. |
000004c0 20 20 20 20 20 70 72 69 6e 74 66 28 22 44 69 76 | printf("Div|
000004d0 69 73 69 6f 6e 20 62 79 20 7a 65 72 6f 3a 20 75 |ision by zero: u|
000004e0 6e 64 65 66 69 6e 65 64 20 72 65 73 75 6c 74 5c |ndefined result\|
000004f0 6e 22 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65 0a |n");. } else.|
00000500 20 20 20 20 7b 20 70 72 69 6e 74 66 28 22 25 64 | { printf("%d|
00000510 20 f7 20 25 64 20 3d 20 25 66 5c 6e 22 2c 20 61 | . %d = %f\n", a|
00000520 72 67 31 2c 20 61 72 67 32 2c 20 64 69 76 69 64 |rg1, arg2, divid|
00000530 65 6e 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a |end);. }. }.|
00000540 20 20 77 68 69 6c 65 28 28 61 72 67 31 21 3d 30 | while((arg1!=0|
00000550 29 20 7c 7c 20 28 61 72 67 32 21 3d 30 29 29 3b |) || (arg2!=0));|
00000560 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d |. return 0;.}|
0000056e
.