Why the result of this code in C of the 55?

Asked

Viewed 92 times

1

I wanted to know why the result of this code of 55, I am reading in the book and did not understand, I compiled in codeblocks.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, soma = 0;
    for(i=1;i<=10;i++) {

    soma = soma + i;
}
printf("Soma %d\n", soma);
system("pause");
return 0;
}
  • 1

    You are accumulating the counter in the variable soma.

2 answers

4


I suggest adding the following line within the:

printf("i vale %d Soma vale%d\n",i, soma);

Staying:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int i, soma = 0;
    for(i=1;i<=10;i++) {
        soma = soma + i;
        printf("i vale %d Soma vale %d\n",i, soma);
    }
    printf("Soma %d\n", soma);
    system("pause");
    return 0;
}

Then you’ll know that...

i vale 1 Soma vale 1  //1ª Iteração
i vale 2 Soma vale 3  //2ª Iteração
i vale 3 Soma vale 6  //3ª Iteração
i vale 4 Soma vale 10  //4ª Iteração
i vale 5 Soma vale 15  //5ª Iteração
i vale 6 Soma vale 21  //6ª Iteração
i vale 7 Soma vale 28  //7ª Iteração
i vale 8 Soma vale 36  //8ª Iteração
i vale 9 Soma vale 45  //9ª Iteração
i vale 10 Soma vale 55  //10ª Iteração

When you do soma = soma + i; you increase the sum value by adding what you had previously.

For example, when i = 3 and soma = 3, then sum will be worth 6 because soma recebe soma (que vale 3) + i (que vale 3)

I recommend to you that you are starting to take a paper and pencil and write the value of each iteration within the for loop. So you will understand what happens in each iteration.

  • i + soma or soma + i is the same thing. You only have to be careful with multiplications and divisions, for this takes precedence over addition and subtraction. What’s happening is that every iteration soma changes value to soma + i. Imagine the operator = as an assignment. soma gains a new value, which is i + the previous value of soma

  • 1

    Then it is the sum of the previous variable "sum" plus i.

  • Exactly :) I hope I helped. If you have any more questions, post in the comments.

  • Humm, it’s good to comment on the code.

  • Yes, of course. But when I was studying algorithms, I did it on paper. Believe me, it’s the best way to learn. Or insert printf in the code and boots to print the value of your variables.

  • @Avelino Good morning, "You should only be careful with multiplications and divisions.. " In fact one should beware of divisions and SUBTRACTIONS, after all one of the properties of multiplication is 'the order of the factors does not alter the product' so, in multiplication, whatever the order ;)

  • @Marianasempe meant to tell him to be careful with the precedence of operators. As you can see at this link multiplication and division takes precedence over addition and subtraction. So if he did something like soma = i * j / k + l - m multiplication and division would be performed before addition and subtraction. I think you must have confused precedência de operadores with the property of comutatividade. I know what you both mean.

  • @Avelino I know what is precedence and in this case does not apply, after all there is no more of an operation, only a sum, so what matters in the case would be to be careful with - and / so I deduced that had been mistaken. Another thing, you edited the comment, no? hehe worth it

  • There is no way to edit the comment after 15 minutes, my comments were 2 days ago. I wanted to better explain my reasoning. I just wanted to give him a hint, because he’s a beginner. When I formulated my answer and wrote these comments I was about 30 hours without sleep (as I am now) so I explained it wrong, I recognize it. It’s just a misunderstanding.

  • @Avelino man, quiet, no problems.

Show 5 more comments

0

Good come on, you’re making a loop with the variable int i that in the first iteration of for has the value of 1. You also assumed that int soma has a value of 0. Your loop will rotate until int i reach the value of 10, remembering that you started it as 1 for (i = 1; your loop will rotate while int i is less than or equal to 10 i <= 10;

We will now simulate your loop step by step.

First Iteration

sum = sum + i; Soma = 0 | int i = 1 | Resultado = 1

Second Iteration

sum = sum + i; Soma = 1 | int i = 2 | Resultado = 3

Third Iteration

sum = sum + i; Soma = 3 | int i = 3 | Resultado = 6

Fourth Iteration

sum = sum + i; Soma = 6 | int i = 4 | Resultado = 10

Fifth Iteration

sum = sum + i; Soma = 10 | int i = 5 | Resultado = 15

Sixth Iteration

sum = sum + i; Soma = 15 | int i = 6 | Resultado = 21

Seventh Iteration

sum = sum + i; Soma = 21 | int i = 7 | Resultado = 28

Eighth Iteration

sum = sum + i; Soma = 28 | int i = 8 | Resultado = 36

Nona Iteração

sum = sum + i; Soma = 36 | int i = 9 | Resultado = 45

Tenth Iteration

sum = sum + i; Soma = 45 | int i = 10 | Resultado = 55

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, soma = 0;

    for (i = 1; i <= 10; i++) 
    {  
        soma = soma + i;
    }

    printf("Soma %d\n", soma);
    getchar();
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.