1

I got a problem with my program that should sum the numbers from 1 to 70000 (1+2+3+4+...+69999+70000). My program can sum the numbers up to 65535 without a problem, but for any summing above 65535, the result shows negative numbers, which is wrong. Can anyone explain to me why my program can not sum numbers above 65535?

this is my code :

#include <stdio.h>

void sum(int *s)
{
    *s=0;
    int i=1;
    int n=70000;
    while(i<=n)
    { 
        *s+=i; 
        i++;
    }   
}

main() 
{
    int s;
    sum(&s);

    printf("Suma prirodnih brojeva od 1 do 70000 je: %d\n",s);
}
Daniel S.
  • 6,458
  • 4
  • 35
  • 78
user3127589
  • 37
  • 1
  • 7

8 Answers8

9

Your int overflowed it's capabilities.

Try a long. Preferably unsigned since you don't need negative numbers.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
2

Firstly you should fix formatting in your post.

First solution that comes to my mind may be that in your implementation integer's size is equal to 16 bits (2 bytes) instead of usual 32 bits (4 bytes). You should use long instead, which guarantees you by standard 32 bits of memory.

Also I don't know why you have decided to use reference when you could simply return value from the sum function like this:

long sum(long s)
{
    //while code

    return value;
}

I hope it helps.

Peter Nimroot
  • 545
  • 5
  • 14
  • basicaly l understand but lm not sure how will l use this since l got a few more limitations for this program :D thank you anyway – user3127589 Dec 22 '13 at 17:49
2

Each integral number can hold a predefined range of values. In C you can see this range by using special macros. For example for type int you can get the maximum and the minimum values that can be stored in an object of this type the following way.

#include <limits.h>
#included <stdio.h>

int main()
{
   printf( "The maximum value of type int is %d and the minimum value is %d\n", INT_MAX, INT_MIN );
}

If some value can not fit into an object of a given type you should choice an integral type with a greater rank. For example the range of unsigned int is usually greater than the range of signed int. You should select an appropriate type by running the program I showed. But do not forget to use correct format specifiers in the the printf statement. You can check unsigned int, long, unsigned long, long long and unsigned long long.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

That is because an integer can hold upto the original limit is -32767 to 32767. you can use long as data type

you can refer the capability of datatype below (even though it's depends upon the complier)

  • short int and int: -32,767 to 32,767
  • unsigned short int and unsigned int: 0 to 65,535
  • long int: -2,147,483,647 to 2,147,483,647
  • unsigned long int: 0 to 4,294,967,295
Kirk
  • 4,957
  • 2
  • 32
  • 59
  • 1
    It is worth pointing out that these ranges are platform dependent. Only the minimal size of some of these are specified in the standard. – juanchopanza Dec 22 '13 at 17:52
1

What you've encountered is covered in computer architecture.

A basic int is designed to have 16 bits which gives you a range up to 65535. That means that all the given bits at this point would be 1 i.e. 1111 1111 1111 1111. Adding 1 to this bit pattern is not possible as it only supports up to 16 bits, hence adding 1 (or more) gives undesired behaviour: look up overflow.

Hence you need to use an implementation which has more bit patterns: long int is something that you should be using. It uses 32 bits to represent data. Gives you much more range. more than 4 billion if I'm not wrong.

Next you shouldn't be manually adding 1 to 7000 (as mentioned in the comments above). sum of 1 to n = n(n+1)/2

Hence your code : when n = 7000

long sum (){
    return ((7000(7000 + 1))/2)
}
Rahul Shardha
  • 399
  • 7
  • 16
  • 1
    Don't make the assumption that int is 16 bit. Its usually 32 (or 64). Check sizeof(int) http://stackoverflow.com/a/5036313/14065. long is usually the same as int. To force 64 you need to use `long long`. http://stackoverflow.com/a/271132/14065 – Martin York Dec 22 '13 at 18:27
1

Actually when you store a number in memory, it is mapped into its equivalent binary code e.g 4 is stored as 100 in memory. It takes 3 bits to store 4 in memory because its binary takes 3 bits. It takes roughly log(n) (with base 2) bits to store n in memory. So if n is very large, log(n) is large too and overflow occurs. Int is 4 bytes long which means 32 bits. You should try a long int (8 bytes) for handling your sum. You can use long long int if the numbers get even bigger and for numbers that can't be handled by even long long int can be handled by THE GNU MULTIPLE PRECISION LIBRARY in C++. here's a reference to this library https://gmplib.org

Ahmed
  • 2,176
  • 5
  • 26
  • 40
0

You can optimize it using that fact:

1 + 2 + 3 + ... + (n - 1) + n = (1 + n) + (2 + (n - 1)) + (3 + (n - 2)) + ... + ((n / 2) + (n / 2) + 1) = (n + 1) * (n / 2) = (n + 1) * n / 2

enedil
  • 1,605
  • 4
  • 18
  • 34
0

The formula for the sum of N numbers is N*(N-1)/2.

In your case, this would be 70000*69999/2 = 2449965000 (2.5 billion approximately)

To store that in a variable you need a variable capable of storing enough bits to represent 2.5 billion.

8 bits = 0 to 256 16 bits = 0 to 65535 32 bits = 0 to 4294967295 (big enough)

You need a 32-bit variable. You can also use this as a cheat sheet: http://www.cplusplus.com/reference/climits/

The type you want is an unsigned long.

Change the types of i and n to an unsigned long and your code will work.

Carl
  • 43,122
  • 10
  • 80
  • 104