CS1101C Lab 1 (Odd Week)

Circular Addition

The deadline for this lab question is Thursday 09 February 2006, 23:59:59 hours.

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

Preliminary

At this early stage of the course, you would probably have written a program that involves some integer addition. Below is an example of a very simple program add.c that accepts two integers from the user, computes and displays their sum.

/**********************************************************************/
/* Matric Number: U051234A and U059876B                               */
/* Userid: u0501234 and u0509876                                      */
/* (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: 08 February 2006                                 */
/* Lab Session Time: 0800 - 0945                                      */
/* Title: Simple Addition                                             */
/* Purpose: Calculates the sum of two integers.                       */
/**********************************************************************/

#include <stdio.h>

int main()
{
    /* Variable declaration */
    int num1, num2;
    int result;

    /* Get user input */
    printf("Enter the first number: ");
    scanf("%i", &num1);

    printf("Enter the second number: ");
    scanf("%i", &num2);

    /* Compute and print result */
    result = num1 + num2;
    printf("%i + %i = %i\n\n", num1, num2, result);

    return 0;
}

The above program is, however, not foolproof. Try out with different integer numbers and see if the computed result differs from the expected result. Find an example of two positive numbers, each between the range of 1 to 2147483647 inclusive, that leads to an erroneous result. What is the cause of the error? Note down these two numbers as you will use them again in another program.

Circular Addition

We now introduce an alternative form of addition termed circular addition. Given a bound, say N, circular addition wraps back to 1 after N. For example, if N is 5 and assume the operator (+5) denotes circular addition with wraparound after 5, then

1 (+5) 3 = 4  // no wrapping required
1 (+5) 4 = 5  // no wrapping required
1 (+5) 5 = 1  // 6 wraps back to 1 after 5
1 (+5) 6 = 2  // 7 wraps back to 2
1 (+5) 7 = 3  // 8 wraps back to 3
1 (+5) 8 = 4  // 9 wraps back to 4
1 (+5) 9 = 5  // 10 wraps back to 5
1 (+5) 10 = 1 // 11 wraps twice back to 1

Again we assume the two numbers to be circularly added are always positive non-zero integers in the range between 1 and 2147483647 inclusive. Moreover, we assume N to be in the range of 1 to 999 inclusive. So

1 (+5) 2147483647 = 3
2147483647 (+5) 2147483647 = 4
2147483647 (+999) 2147483647 = 560

The Task

Your task is to extend add.c to cadd.c so that it now performs circular addition. Assuming that the executable is cadd, a sample run of the program is shown below. User input is denoted in bold.

$ gcc -Wall cadd.c -o cadd
$ cadd
Enter N for circular addition: 5
Enter the first number: 1
Enter the second number: 3
1 (+5) 3 = 4

$ cadd
Enter N for circular addition: 5
Enter the first number: 1
Enter the second number: 6
1 (+5) 6 = 2

$ cadd
Enter N for circular addition: 5
Enter the first number: 1
Enter the second number: 10
1 (+5) 10 = 1

$ cadd
Enter N for circular addition: 5
Enter the first number: 1
Enter the second number: 2147483647
1 (+5) 2147483647 = 3

$ cadd
Enter N for circular addition: 5
Enter the first number: 2147483647
Enter the second number: 2147483647
2147483647 (+5) 2147483647 = 4

$ cadd
Enter N for circular addition: 1
Enter the first number: 1
Enter the second number: 1
1 (+1) 1 = 1

$ cadd
Enter N for circular addition: 999
Enter the first number: 1000000000
Enter the second number: 2000000000
1000000000 (+999) 2000000000 = 3

$ cadd
Enter N for circular addition: 999
Enter the first number: 2147483647
Enter the second number: 2147483647
2147483647 (+999) 2147483647 = 560

$

For the two numbers that caused an error in program add.c which you have noted down previously, test these two numbers again in cadd.c for any values of N from 1 to 999 inclusive and see if the result is as expected. Document your observations as comments in cadd.c. Specifically, you should explain whether the expected results were obtained in both add.c and cadd.c, giving reasons as to why this is so.

Points to Note


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

A total of 11 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 9 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.