The name of your C program file must be called swap1.c, files with any other name will not be marked.
If I swap the digits in position 1 and position 2, the result is the four-digit number 6578.
If I swap the digits in position 3 and position 4, the result is the four-digit number 5687.
If I swap the digits in position 4 and position 2, the result is the four-digit number 5876.
If I swap the digits in position 3 and position 3, the result is the four-digit number 5678. There is no change since the position numbers are the same.
/**********************************************************************/ /* Matric Number: U061234A and U069876B */ /* Userid: u0601234 and u0609876 */ /* (For those doing pair programming, only ONE of you should submit */ /* your program. The other student should NOT submit.) */ /* Lab: 1 */ /* Lab Group Number: 99 */ /* Lab TA's Name: Who needs a lab TA when you're Bill Gates? */ /* Lab Session Date: 14 February 2007 */ /* Lab Session Time: 1000 - 1145 */ /* Title: Digit swapping. */ /* Purpose: Perform digit swapping. */ /**********************************************************************/ #include <stdio.h> #include <math.h> int main(void) { /* Variable declaration. */ /* You can declare any other variables here. */ int input, pos1, pos2, output; /* Get user input. Do not change this. */ printf("Enter a four-digit number (0 - 9999): "); scanf("%d", &input); printf("Enter position one to swap (1 - 4): "); scanf("%d", &pos1); printf("Enter position two to swap (1 - 4): "); scanf("%d", &pos2); /* Compute result. Type your code here. */ /* Print result. Do not change this. */ printf("The output is: %04d\n", output); return 0; }
$ gcc -Wall -lm swap1.c -o swap1 $ ./swap1 Enter a four-digit number (0 - 9999): 7536 Enter position one to swap (1 - 4): 4 Enter position two to swap (1 - 4): 1 The output is: 6537 $ ./swap1 Enter a four-digit number (0 - 9999): 0825 Enter position one to swap (1 - 4): 1 Enter position two to swap (1 - 4): 3 The output is: 2805 $ ./swap1 Enter a four-digit number (0 - 9999): 7940 Enter position one to swap (1 - 4): 4 Enter position two to swap (1 - 4): 1 The output is: 0947 $ ./swap1 Enter a four-digit number (0 - 9999): 0000 Enter position one to swap (1 - 4): 2 Enter position two to swap (1 - 4): 2 The output is: 0000 $
A total of 13 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 7 times.
If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.