CS1101C Lab 1 (Even Week)

Swapping Digits

The deadline for this lab question is Friday 16 February 2007, 23:59:59 hours.

The name of your C program file must be called swap1.c, files with any other name will not be marked.

Preliminary

Given a four-digit number, we want to swap among individual digits in the number.

Example

Suppose the four-digit number is 5678. The digit in position 1 is the first digit 5, the digit in position 2 is the second digit 6, the digit in position 3 is the third digit 7, and the digit in position 4 is the fourth digit 8. The four-digit number can range from 0 to 9999 inclusive, and it may contain repeated digits.

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.

Your Task

Write a program called swap1.c that prompts the user for the four-digit number, and the position numbers of the digits to swap. Your program will compute and display the four-digit number with the digits swapped.

Template

The following swap1.c program has been provided for you that reads in the three inputs. You must follow the template given below. Your task is to simply extend the program to compute and print out the four-digit number with the digits swapped.

/**********************************************************************/
/* 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;
}

Sample Runs

Assuming that the executable is swap1, a sample run of the program is shown below. User input is denoted in bold.


$ 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

$

Points to Note

Ponder

  1. Why do we use %d instead of %i in the scanf statements?
  2. Why do we use %04d in the printf statement?


This document, index.html, has been accessed 21 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

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.