# While repeat structure to recalculate in C

6

I solved this exercise below and thought about putting a `while` repetition structure, so the person recalculates. But when I put 's' to return and recalculate, the exercise sums up the values of the 10 numbers I previously calculated and adds it to the next 10 numbers. Why does this occur?

``````/*Ex: Faça um programa que solicite ao usuário a entrada de 10 números e
imprima como resultado a soma de todos os pares */

int vetor[10], i;
float res=0;
char op;

do {

for (i=0; i<=9; i++) {

printf("\nInforme um numero: ");
scanf("%i", &vetor[i]);

if (vetor[i] % 2 == 0) {

res = res + vetor[i];

}

}

printf("\nA soma dos pares e: %.2f", res);

printf("\nDeseja calcular novamente? ");
scanf("%s", &op);
fflush(stdin);

} while (op == 's' || op == 'S');

return(0);
``````

asked by anonymous 11.09.2015 / 23:44

3

The problem is that you are not restarting the value of the `res` variable when it will recalculate:

``````#include <iostream>
using namespace std;

int main() {
int vetor[10], i;
char op;
do {
float res = 0; //agora toda vez que for repetir o pedido vai zerar a variável
for (i = 0; i <= 9; i++) {
printf("\nInforme um numero: ");
scanf("%i", &vetor[i]);
if (vetor[i] % 2 == 0) {
res += vetor[i];
}
}
printf("\nA soma dos pares e: %.2f", res);
printf("\nDeseja calcular novamente? ");
scanf("%s", &op);
fflush(stdin);
} while (op == 's' || op == 'S');
return(0);
}
``````

Some small problems in the code besides this but that does not stop the operation.

12.09.2015 / 00:00
3

The problem is that the `res` variable does not have its value reset at the next iteration of the `do..while` loop. This can be corrected in two ways

Moving the declaration of the `res` variable into the `do...while` loop:

``````do {

float res = 0;

for (i=0; i<=9; i++) {
``````

Note that in this way the correct one is to remove the previous declaration of the variable `res` , since it will not be used due to differences in scopes.

2- Restarting its value at every iteration of `do...while` through an assignment only:

``````do {

res = 0;

for (i=0; i<=9; i++) {
``````

Note

As `res` receives integer values, it does not have to be declared as `float` , and it is sufficient to declare it as `int` or as `long` , if you believe its values will cause `integer overflow` . p>

I hope I have helped.

11.09.2015 / 23:54
1

You can also use `while` as follows:

``````#include <stdio.h>

#define MAX 10

int main(void)
{
int vetInt[MAX], soma_par, i, op;

while(1)
{
soma_par = 0;
printf("\nInforme 10 numeros inteiros.\n");

/*Leitura dos numeros inteiros.*/
for (i = 0; i < MAX; i++)
{
printf("\nInforme o %i° numero: ", i + 1);
scanf("%i", &vetInt[i]);
}

/*Soma dos numeros pares.*/
for (i = 0; i < MAX; i++)
if (vetInt[i] % 2 == 0)
soma_par += vetInt[i];

printf("\nSoma de todos numeros pares é %i\n\n", soma_par);

printf("\nDigite 0 para finalizar o programa ou 1 para continuar: ");
scanf("%d", &op);

if (op == 0)
break;
else
system("clear");
}

return 0;
}
``````

In this case to close the loop and require the user to type 0 , so the loop will be terminated by `break;` .

15.09.2015 / 05:06