Tell me why this program is equal to 9?

  • Thread starter the_d
  • Start date
  • Tags
    Program
In summary, the program is equal to 9 because it is calculating the sum of 4i-1, where i ranges from 0 to 2. This is achieved through nested for loops, with the outer loop iterating over i and the inner loop iterating over j. Alternating the values of i and j in a specific sequence allows for the correct calculation to be performed.
  • #1
the_d
127
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
  • #2
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
 
  • #3
can u explain why you alternated the values in i and j and in what sequence you did it in please?
 
  • #4
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

[tex]a=\sum_{i=0}^2\sum_{j=0}^1 2i+j-1[/tex]

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

[tex]\sum_{k=0}^n c = (n+1)*c[/tex]

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

The inner sum evaluates to

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

The outer sum thus becomes

[tex]a=\sum_{i=0}^2 4i-1 = 4\frac{2*3}{2}-3*1 = 12-3 = 9[/tex]
 
  • #5
or, in the rant straight from mighty wikipedia: click
 
  • #6
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;
}
 

FAQ: Tell me why this program is equal to 9?

What is the significance of the number 9 in this program?

The number 9 in this program could be a result, a target, or a constant value used in a mathematical equation.

How do you determine that this program is equal to 9?

This program could have various steps and calculations that ultimately lead to the result of 9. It is important to carefully analyze the code and understand the logic behind it to determine how the number 9 is obtained.

Can you explain the algorithm used in this program to obtain 9?

The algorithm used in this program may vary depending on the specific context and purpose. It could involve arithmetic operations, conditional statements, or other mathematical concepts. A thorough understanding of the code is necessary to explain the algorithm used.

Are there any alternative solutions to make this program equal to 9?

Depending on the problem being solved, there could be multiple ways to make this program equal to 9. It could involve using different values, functions, or approaches. Experimenting and testing different solutions could lead to alternative ways to achieve the desired result.

How can I modify this program to obtain a different value instead of 9?

By analyzing the code and understanding the logic behind it, you can make changes to the program to obtain a different value than 9. This could involve changing the input values, the mathematical operations, or the overall structure of the code.

Back
Top