Skip to content

1

Assignment

This assignment aims to assess your understanding of the basic constructs of imperative programming, including alternative (if-then-else​) and iterative (while​, for​) programming constructs, and how to appropriately use them to algorithmically solve simple problems.

Exercise 1

Accompanying this assignment, you will find a C source code file combinatorial_number.c​. This file contains a partial implementation of a C program that computes the combinatorial number (or binomial coefficient) \(\binom{m}{n}\)​, according to the following formula:

\[ \binom{m}{n}=\frac{m!}{n!\times(m-n)!} \]

Your task is to complete this implementation to correctly compute the binomial coefficient, ensuring that the inputs are valid. Study the provided code and follow the instructions therein. Test your program with some values of m​ and n​ to verify that it correctly computes the binomial coefficient.

#include <stdio.h>

int main(void) {
    int m, n, combinatorial;
    printf("Please enter a positive integer m:");
    scanf("%d", &m);
    printf("Please enter a positive integer n, smaller or equal than m:");
    scanf("%d", &n);
    // do not modify above this line
    combinatorial = 1;
    // check if the input is valid
    if (m < n || n < 0) {
        printf("Invalid input!!! Please try again later!!!");
    }
    else {
        // Start the main calculation when m and n are valid.
        // My idea is to calculate it step by step
        // Firstly, I calculate m!
        for (int i = 1; i <= m; i++) {
            combinatorial = combinatorial * i;  
        }
        // Then I calculate m! / n! by combinatorial dividing n!
        for (int j = 1; j <= n; j++) {
            combinatorial = combinatorial / j;  
        }
        // Finally, I calculate m! / (n! x (m - n)!) by combinatorial dividing (m-n)!
        for (int k = 1; k <= m - n; k++) {
            combinatorial = combinatorial / k;  
        }
        printf("The resulting combinatorial number:%d", combinatorial);
    }
    return 0;
}

Exercise 2

Accompanying this assignment, you will find a C source code file sum_product.c​. This file contains a partial implementation of a C program that computes the following:

\[ \sum_{i=0}^{m}\left(i\times\prod_{j=1}^{n}(i+j)\right) \]

given positive values m​ and n​.

Your task is to complete this implementation to correctly compute the result of the above expression, ensuring that the inputs are valid. Study the provided code and follow the instructions therein. Test your program with some values of m​ and n​ to verify that it correctly computes the result of the expression.

#include <stdio.h>

int main(void) {
    int m, n;
    int prod = 1;
    int result = 0;
    // Check the input is valid or not
    // If not valid, do it again
    do {
        printf("Please enter two positive integer number m and n:\n"); 
        scanf("%d %d", &m, &n);
    } while (m <= 0 || n <= 0);
    //calculate the result
    for (int i = 0; i <= m; i++) {
        // Initialing the product is important to ensure the accuracy
        prod = 1;
        for (int j = 1; j <= n; j++) {
            prod *= i + j;
        }
        // when finish the internal product, add it to result
        result += prod * i;
    }
    printf("The result is %d", result);
    return 0;
}

Exercise 3

Accompanying this assignment, you will find a C source code file non_zero_digits.c​. This file contains a partial implementation of a C program that, given a positive number n​, must print, in order, all positive values in the range 1..n​ that do not have zero as a digit. For example, 10​, 107​, and 1024​ contain zero as a digit (and must not be printed), whereas 3​, 27​, 133​, and 1124​ do not contain zero as a digit and thus must be printed.

Your task is to implement the functionality described above.

#include <stdio.h>

int main() {
    // n is the input, is_zero is the flag whether the digit has 0
    int n, is_zero, number;
    // Check the input is valid or not
    // If not valid, do it again
    do {
        printf("Please enter a positive integer number n:\n"); 
        scanf("%d", &n);
    } while (n <= 0);
    // Generating the numbers from 1 to n
    for (int i = 1; i <= n; i++) {
        // initializing the flag of is_zero
        is_zero = 0;
        // We will use i to do some calculation, but we donn't want to change the value of i
        number = i;
        // number > 0 means there exists a digit to be examine
        while (number > 0 && is_zero == 0) {
            // examining the zero in number
            if (number % 10 == 0) {
                is_zero = 1;
            }
            else {
                // take a upper digit
                number /= 10;
            }  
        }
    if (is_zero == 0) 
        printf("%d\n", i);
    }
    return 0;
 } 

Submission Instructions

Your solution to this assignment must be submitted through Moodle. The correctness of the solution, as well as the quality of the code (correct indentation, clarity, good code structure, etc.), will be taken into account when assessing your submission. In particular, efficiency is a secondary issue for this assignment: more efficient solutions that sacrifice clarity and good structure may receive lower grades than well-structured and clear solutions, even if they are less efficient.