Improper Infinite Loop no while

Asked

Viewed 565 times

3

I made a program to calculate within a sequence the sum of the positive numbers and the sum of the negative numbers. When I use the command while, the idea is that it is within a sequence of 7 numbers, and then the program leaves the while and make condition, but this is not happening.

Follows code below:

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

int n, num, somaPositivo, somaNegativo, i;

int main ()
{
    setlocale(LC_ALL, "portuguese");

    printf("\n Digite o tamanho da sequência: "); 
    scanf("%f",&n);

    somaPositivo = 0;
    somaNegativo = 0;
    i = 1;
    while (i <= n)
    {
        printf("\n Digite um número da sequência: "); 
        scanf("%f",&num);
    } 

    if (num > 0)
    {
        somaPositivo = somaPositivo + num;  
    }
    else
    {
        somaNegativo = somaNegativo + num;  
    }

    i = i + 1;

    printf("\n A soma dos números positivos da sequência é: \n",somaPositivo);
    printf("\n A soma dos números negativos da sequência é: \n",somaNegativo);
}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

2

This code has several errors and does not even compile, and some bad practices ,so note all changes.

The main problem is that much of what should be inside the loop is outside, as there is no increment the loop never ends. The accumulation performed also needs to be there in the loop.

I preferred to use a for which is best suited for this case. I simplified the code as well.

I didn’t solve the problem of using the scanf() not treating your return because for exercise is good like this.

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

int main() {
    setlocale(LC_ALL, "Portuguese");
    int n;
    printf("\n Digite o tamanho da sequência: "); 
    scanf("%d", &n);
    int somaPositivo = 0;
    int somaNegativo = 0;
    for (int i = 0; i < n; i++) {
        printf("\n Digite um número da sequência: "); 
        int num;
        scanf("%d", &num);
        if (num > 0) somaPositivo += num;  
        else somaNegativo += num;
    } 
    printf("\n A soma dos números positivos da sequência é: %d\n", somaPositivo);
    printf("\n A soma dos números negativos da sequência é: %d\n", somaNegativo);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

2


  1. Conditional control (if) is outside the while, it means that it runs not n-times, but only once: after the exit of the while. No real sum of numbers;
  2. The specifier "%f" is for floating point types like float or double;
  3. The last two printfs do not have the specifier printing "%f" for float/double or "%d" for int;
  4. The variable i, which will be used for counter, should preferably be of the type int, and the scanf for this, must use the specifier %d. Nothing prevents, however, to be float or double;
  5. The loop is infinite, because there is no increment of the variable i, inside the loop, that is, the condition (i <= n), will always be met, I suppose n is >= 1.

Supposing there’s some fair reason to use the while and not another, like the for, the correct code (tested) should be similar to the one below, if the sequence of numbers is actually integers:

 int n, i = 1, num, somaPositivo = 0, somaNegativo = 0;

 int main() {       
   printf("\n Digite o tamanho da sequência: ");
   scanf("%d", &n);

   while (i <= n) {
     printf("\n Digite o %do número da sequência: ", i);
     scanf("%d", &num);

     if (num > 0) {
       somaPositivo += num;
     } else {
       somaNegativo += num;
     }

     i++;
   }      

   printf("\n A soma dos números positivos da sequência é: %d \n", somaPositivo);
   printf("\n A soma dos números negativos da sequência é: %d \n", somaNegativo);
 }

Browser other questions tagged

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