Tell me why this program is equal to 9?

  • Thread starter Thread starter the_d
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around understanding the output of a C program that calculates a value through nested loops. Participants are exploring the logic behind the program's calculations and how the final result of 9 is achieved, including manual verification of the output.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant questions why the program outputs 9, noting discrepancies when calculating by hand.
  • Another participant provides a step-by-step breakdown of the loop iterations, showing how the variable 'a' accumulates values to reach 9.
  • A request for clarification is made regarding the sequence of values for 'i' and 'j' during the program's execution.
  • A further explanation is provided, detailing the mathematical representation of the program's logic using summation notation and evaluating the inner and outer sums.
  • A participant suggests an alternative implementation using while loops to achieve the same result.

Areas of Agreement / Disagreement

Participants are engaged in clarifying the program's logic and calculations, but there is no consensus on the initial misunderstanding of the output. Some participants provide explanations while others seek further clarification.

Contextual Notes

The discussion includes various interpretations of the program's logic and calculations, with some participants expressing confusion over the execution sequence and the mathematical representation of the code.

the_d
Messages
127
Reaction score
0
can someone tell me why this program is equal to 9? I have been sitting for 2

hours trying to figure out how the program gets and when doing it by hand I

get something different. Here is the program:

#include <stdio.h>

int main()
{
int i, j;
int a=0;
for (i=0;i<3;i++)
for (j=0;j<2;j++)
a+=2*i+j-1;
printf("%d\n", a);
return 0;
}

a=9
 
Technology news on Phys.org
what you mean why?
i=0, j=0: 2*i+j-1=-1, a=-1
i=0, j=1: 2*i+j-1=0, a=-1
i=1, j=0: 2*i+j-1=1, a=0
i=1, j=1: 2*i+j-1=2, a=2
i=2, j=0: 2*i+j-1=3, a=5
i=2, j=1: 2*i+j-1=4, a=9
 
can u explain why you alternated the values in i and j and in what sequence you did it in please?
 
the_d said:
can u explain why you alternated the values in i and j and in what sequence you did it in please?

They represent
the_d said:
Code:
  for (i=0;i<3;i++)
   for (j=0;j<2;j++)

In other words, whatta executed the program by hand.

Mathematically, your code is calculating

a=\sum_{i=0}^2\sum_{j=0}^1 2i+j-1

First evaluate the inner sum (the sum over j). Note that the term 2i-1 is a constant in this inner sum. Using

\sum_{k=0}^n c = (n+1)*c

\sum_{k=0}^n k = \frac{k*(k+1)}2

The inner sum evaluates to

\sum_{j=0}^1 2i+j-1 = 2*(2i-1)+\frac{1*2}2 = 4i-1

The outer sum thus becomes

a=\sum_{i=0}^2 4i-1 = 4\frac{2*3}{2}-3*1 = 12-3 = 9
 
or, in the rant straight from mighty wikipedia: click
 
Perhaps it makes more sense with while loops:
Code:
int main()
{
	int i, j;
	int a=0;
	
	i = 0;
	while (i < 3) {
		j = 0;
		while (j < 2) {
			a += 2*i+j-1;
			j++;
		}
		i++;
	}
	printf("%d\n", a);
	return 0;
}
 

Similar threads

Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
14
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
1
Views
2K
Replies
47
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K