CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Jan 2009
    Location
    IN my computer,
    Posts
    24

    Question Why does this loop turn out negatives?

    Ok so i am using a loop to make a large array of additions.

    the loop takes all of the values before it and adds them together, then saves them into the array.

    (in this case the short had would be to multiply the preceding number by 2)

    But for some reason near the end (the 29th spot to be exact) the number is negative, while still being the correct absolute value. Then it just turns 0's

    The referancing of the array and array places is correct, and there is no subtraction, of refrencing of places that do not exist.

    so why is place 29 negative?

    1 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <fstream>
    
    using namespace std;
    
    int as = 100;
    int state;
    int loop;
    int pre;
    int array[100];
    
    int main(int argc, char *argv[])
    {
    ofstream ST("Out.txt", ios::out | ios::app);
    for(state = 0; state != as; state++)
    {
               array[0] = 1;
               pre = 0;
    for(loop = 0; loop != state; loop++)
               {
              pre = array[loop] + pre;
              }
              array[state] = pre;
    }
    for(pre = 0; pre != as; pre++)
    {
    ST << array[pre] << " ";
    cout << array[pre] << " ";
    }          
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Last edited by H aun; March 21st, 2009 at 02:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured