Skip to content

11.14 Tutorial5

intro-to-computer-science-gtiit-tutorial-05-2024.pdf

Assignment Operators

Write the following assignments only using the assignment (=​) operator:

  1. x += y

    x = x + y

  2. y -= x

    y = y - x

  3. x++

    x = x + 1

  4. z--

    z = z - 1

  5. a *= 5

    a = a * 5

  6. x /= 7

    x = x / 7

  7. b >>= a

    b = b >> a

  8. p <<= q

    p = p << q

  9. x &= 1024

    x = x & y

  10. y |= z

    y = y | z

  11. x %= 4096

    x = x % 4096

  12. x ^= 2

    x = x ^ 2

For Statement

Manually execute the following program:

2.1

/* 2.1 */
void main() {
    int limit = 5;
    int evenSum = 0;
    int oddSum = 0;
    for (int i = 0; i < limit; i++) {
        if (i % 2 == 0) {
            evenSum += i;
        } else {
            oddSum += i;
        }
    }
}
**Step-by-Step Execution**

1. **Initialization:**

    * ​`limit = 5`​
    * ​`evenSum = 0`​
    * ​`oddSum = 0`​
2. **Loop Execution:**

    * **Iteration 1 (i = 0):**

      * ​`i % 2 == 0`​ (0 is even)
      * ​`evenSum += i`​ → `evenSum = 0`​
      * ​`oddSum = 0`​
    * **Iteration 2 (i = 1):**

      * ​`i % 2 != 0`​ (1 is odd)
      * ​`oddSum += i`​ → `oddSum = 1`​
      * ​`evenSum = 0`​
    * **Iteration 3 (i = 2):**

      * ​`i % 2 == 0`​ (2 is even)
      * ​`evenSum += i`​ → `evenSum = 2`​
      * ​`oddSum = 1`​
    * **Iteration 4 (i = 3):**

      * ​`i % 2 != 0`​ (3 is odd)
      * ​`oddSum += i`​ → `oddSum = 4`​
      * ​`evenSum = 2`​
    * **Iteration 5 (i = 4):**

      * ​`i % 2 == 0`​ (4 is even)
      * ​`evenSum += i`​ → `evenSum = 6`​
      * ​`oddSum = 4`​
3. **Final Values:**

    * ​`evenSum = 6`​
    * ​`oddSum = 4`​

### Program 2.2 Execution

```c
void main() {
    int limit = 10;
    int sum = 0;
    for (int i = 0; i < limit; i += 2) {
        if (i % 2 == 0) {
            continue;
        }
        sum += i;
    }
}
  1. Initialization:

    • limit = 10
    • sum = 0
    • Loop Execution:

    • The loop increments i​ by 2 each time, starting from i = 0​.

    • Iteration 1 (i = 0):

    • i % 2 == 0​ (true)

    • continue​ is executed, skipping sum += i
    • sum = 0
    • Iteration 2 (i = 2):

    • i % 2 == 0​ (true)

    • continue​ is executed, skipping sum += i
    • sum = 0
    • Iteration 3 (i = 4):

    • i % 2 == 0​ (true)

    • continue​ is executed, skipping sum += i
    • sum = 0
    • Iteration 4 (i = 6):

    • i % 2 == 0​ (true)

    • continue​ is executed, skipping sum += i
    • sum = 0
    • Iteration 5 (i = 8):

    • i % 2 == 0​ (true)

    • continue​ is executed, skipping sum += i
    • sum = 0
    • Final Value:

    • sum = 0

Explanation: Since i​ is incremented by 2 and starts from 0, it is always even, so the continue​ statement is executed in every iteration, and sum​ is never modified.

Program 2.3 Execution

void main() {
    int limit = 0;
    int product = 0;
    for (int i = 6; i > limit; i--) {
        product += i;
        if (product <= i * 2) {
            continue;
        }
        product *= 2;
    }
}
  1. Initialization:

    • limit = 0
    • product = 0
    • Loop Execution:

    • The loop decrements i​ by 1 each time, starting from i = 6​.

    • Iteration 1 (i = 6):

    • product += i​ → product = 0 + 6 = 6

    • product <= i * 2​ → 6 <= 6 * 2​ (true)
    • continue​ is executed, skipping product *= 2
    • product = 6
    • Iteration 2 (i = 5):

    • product += i​ → product = 6 + 5 = 11

    • product <= i * 2​ → 11 <= 5 * 2​ (true)
    • continue​ is executed, skipping product *= 2
    • product = 11
    • Iteration 3 (i = 4):

    • product += i​ → product = 11 + 4 = 15

    • product <= i * 2​ → 15 <= 4 * 2​ (false)
    • product *= 2​ → product = 15 * 2 = 30
    • Iteration 4 (i = 3):

    • product += i​ → product = 30 + 3 = 33

    • product <= i * 2​ → 33 <= 3 * 2​ (false)
    • product *= 2​ → product = 33 * 2 = 66
    • Iteration 5 (i = 2):

    • product += i​ → product = 66 + 2 = 68

    • product <= i * 2​ → 68 <= 2 * 2​ (false)
    • product *= 2​ → product = 68 * 2 = 136
    • Iteration 6 (i = 1):

    • product += i​ → product = 136 + 1 = 137

    • product <= i * 2​ → 137 <= 1 * 2​ (false)
    • product *= 2​ → product = 137 * 2 = 274
    • Final Value:

    • product = 274

Linear Accumulation with For

Translate the following summations to C programs with for​ statements:

a. \(\sum_{i=0}^{1024} i\)

#include <stdio.h>

int main () {
    int sum = 0;
    for (int i = 0; i <= 1024; i++) {
        sum = sum + i;
    }
    printf("%d", sum);
    return 0;
}

b. \(\sum_{i=1}^{10} \frac{36}{i}\)

#include <stdio.h>

int main () {
    float sum = 0;
    for (int i = 1; i <= 36; i++) {
        sum = sum + (36 / i);
    }
    printf("%f", sum);
    return 0;
}

c. \(\sum_{i=0}^{10} i^i\)

#include <stdio.h>
#include <math.h>

int main () {
    double sum = 0;
    for (double i = 0; i <= 10; i++) {
        sum = sum + pow(i, i);
    }
    printf("%lld", (long long int)sum);
    return 0;
}

d. \(\sum_{i=0}^{10} \frac{i}{2}\)

#include <stdio.h>

int main () {
    float sum = 0;
    for (int i = 0; i <= 10; i++) {
        sum = sum + (i / 2);
    }
    printf("%f", sum);
    return 0;
}

e. \(\sum_{i=1}^{100} i \cdot \sum_{k=1}^{i} k \cdot \sum_{j=1}^{k} j\)

#define Oo0Oo0oO int
#define o0O0OoOo long
#define O0oO0o0O for
#define oOo0O0o0 main 
#define OO0oO0Oo printf
#define o0O0oO0o return

#include <stdio.h> 
Oo0Oo0oO oOo0O0o0(){o0O0OoOo o0O0OoOo Oo0Oo0oO O0o0O0o0 = 0;O0oO0o0O
(Oo0Oo0oO o0O0o0OO = 1; o0O0o0OO <=
100; o0O0o0OO++) {O0oO0o0O (Oo0Oo0oO 
Oo0Oo0Oo = 1; Oo0Oo0Oo <= o0O0o0OO; 
Oo0Oo0Oo++) {O0oO0o0O (Oo0Oo0oO oO0oO0oO = 1; oO0oO0oO 
<= Oo0Oo0Oo; oO0oO0oO++) {O0o0O0o0 += 
o0O0o0OO * Oo0Oo0Oo * 
oO0oO0oO;}}}OO0oO0Oo(
"%lld", O0o0O0o0);o0O0oO0o 0;
}
#include <stdio.h>

int main () {
    long long int sum=0;
    for (int i = 1; i <= 100; i++) {
        for (int k = 1; k <= i; k++) {
            for (int j = 1; j <= k; j++) {
                sum += i * k * j;
            }
        }
    }
    printf("%lld", sum);
    return 0;
}

f. \(\frac{\sum_{i=0}^{10} 2 \cdot i}{\sum_{i=0}^{5} 4 \cdot i}\)

#include <stdio.h>

int main () {
    int sum1 = 0, sum2 = 0;
    for (int i = 0; i <= 10; i++) {
        sum1 = sum1 + 2 * i;
    }
    for (int j = 0; j <= 5; j++) {
        sum2 = sum2 + 4 * j;
    }
    // pay attention to the type of variable!
    // if the result may be float, then the variable should have at least one float
    // or this may casue integer division, then leading to loss of the fractional part.
    float result = (float)sum1 / (float)sum2;
    printf("%f", result);
    return 0;
}

4. Comma operator

Rewrite the following C programs without using the comma operator and correctly indenting them:

image

#include <stdio.h>

int main() {
    int i = 1, number = 1000;

    while (i < number) {
        if (number % i == 0) {
            printf("%d\n", i);
        }
        i++;
    }

    return 0;
}
#include <stdio.h>

int main() {
    int a, b;
    int number = 10;

    for (a = 1; a < number; a++) {
        for (b = 1; b < number; b++) {
            printf("%d^2 + %d^2 = %d\n", a, b, (a * a) + (b * b));
        }
    }

    return 0;
}