Undefined infinite loop no while

2

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 to be within a sequence of 7 numbers, and then the program exits from while and make condition, but this is not happening.

Follow the 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);
}
    
asked by anonymous 05.10.2017 / 03:59

2 answers

1
  • Conditional control (if) is out of while , this means that it does not run n-times, but only one: after the while exit. There is no real sum of numbers;
  • The character specifier "% f " is for float types such as float or double / li>
  • The last two printfs do not have the character specifier of print "% f " for float / double or "% d " for int ;
  • The i variable, which will be used for counter, should preferably be of type int , and scanf to do this, character specifier % d . Nothing prevents, however, from being float or double;
  • The loop is infinite, since there is no increment of the variable i, inside the loop, that is, the condition (i <= n) , will always be met, I suppose that n is > = 1.
  • Assuming there is a fair reason to use while and not another, such as for , the correct (tested) code should look like the below if the sequence of numbers is really 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);
     }
    
        
    05.10.2017 / 04:22
    1

    This code has several errors and neither compiles, and some bad practices, so note all changes.

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

    I preferred to use% w / w which is the most appropriate for this case. I simplified the code too.

    I have not solved the problem of using for not treating your return because for exercise this is good.

    #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);
    }
    

    See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

        
    05.10.2017 / 04:17