0
#include <stdio.h>

int main()
{

    int i; //counter for the loop
    int n; //integer
    int series;

    printf("Enter an integer number: ");
    scanf("%d" , &n);

        for(i = 1; i <= n; i++)     
    {
        if (i % 2 == 0)             
        (series -= i * i);      
    else 
        (series += i * i);      
}
    printf("The value of the series is: %d\n" , series);

return 0;
}

So the the loop is just a basic for loop, using I as the counter for as long as it is less than or equal to n

the series that I have to replicate adds odd numbers and subtracts even numbers so the if condition tests if the number is even or odd. The program compiles fine but when I enter the integer as 5 the sum of the series should be 15, however my program gives the sum 32779. Any help on fixing my program would be appreciated.

chrisHG
  • 80
  • 1
  • 2
  • 18
  • 4
    Uninitialized variable `series` will cause undefined behaviour – John3136 Sep 25 '17 at 02:40
  • 1
    Possible duplicate of [What happens to a declared, uninitialized variable in C? Does it have a value?](https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – John3136 Sep 25 '17 at 02:41
  • Can you clarify please? – chrisHG Sep 25 '17 at 02:45

1 Answers1

2

you didn't initialize series, so it's a random value in the beginning of the calculation.

#include <stdio.h>

int main()
{

    int i = 0; //counter for the loop
    int n = 0; //integer
    int series = 0;

    printf("Enter an integer number: ");
    scanf("%d" , &n);

    for(i = 1; i <= n; i++)     
    {
        if (i % 2 == 0)             
            (series -= i * i);      
        else 
            (series += i * i);      
    }
    printf("The value of the series is: %d\n" , series);

    return 0;
}
parkerzhu
  • 21
  • 2
  • Would be more accurate to say that the value of `series` will be "undefined". For more info, see: [Uninitialized variable: Example of the C language](https://en.wikipedia.org/wiki/Uninitialized_variable#Example_of_the_C_language). – Louis Langholtz Sep 25 '17 at 02:54
  • @LouisLangholtz there is no such thing as "undefined value". There are *indeterminate* or *unspecified* values, and *undefined behaviour*. In this case it is *indeterminate value*, and, depending on a few things which I won't go into right now, reading a variable of indeterminate value either causes undefined behaviour, or produces an unspecified value. – M.M Sep 25 '17 at 02:58
  • @M.M Seems Wikipedia's article could use the perfecting you suggest too. Section **6.7.8 Initialization, 10** in the referenced C-standard document [ISO/IEC 9899:TC3](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf) states: "If an object that has automatic storage duration is not initialized explicitly, its value is *indeterminate*." That seems like the exact portion of the doc that applies. Is that your interpretation then too? Thanks for the clarification! – Louis Langholtz Sep 25 '17 at 03:31