random output

This is a discussion on random output within the C++ Programming forums, part of the General Programming Boards category; i am using codeblocks and i am having problems, this is the code: Code: #include <iostream> using namespace std; int ...

  1. #1
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87

    random output

    i am using codeblocks and i am having problems, this is the code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        long long int highest = 0;
        int grid [20][20] = // i initialize the array here
        cout << endl << "benchmark 1/n";
        for(int a=0; a < 20; a++)
        {
            for(int b=0; b < 17; b++)
            {
                if(highest < (grid[a][b]*grid[a][b+1]*grid[a][b+2]*grid[a][b+3]))
                {
                    highest = (grid[a][b]*grid[a][b+1]*grid[a][b+2]*grid[a][b+3]);
                }
            }
        }
        cout << "benchmark 2/n";
        for(int a=0; a < 17; a++)
        {
            for(int b=0; b < 20; b++)
            {
                if(highest < (grid[a][b]*grid[a+1][b]*grid[a+2][b]*grid[a+3][b]))
                {
                    highest = (grid[a][b]*grid[a+1][b]*grid[a+2][b]*grid[a+3][b]);
                }
            }
        }
        cout << "benchmark 3/n";
        for(int a=0; a < 17; a++)
        {
            for(int b=0; b<17; b++)
            {
                if(highest < (grid[a][b]*grid[a+1][b+1]*grid[a+2][b+2]*grid[a+3][b+3]))
                {
                    highest = (grid[a][b]*grid[a+1][b+1]*grid[a+2][b+2]*grid[a+3][b+3]);
                }
            }
        }
        cout << "benchmark 4/n";
        for(int a=0; a<17; a++)
        {
            for(int b=0; b < 20; b++)
            {
                if(highest < (grid[a][b]*grid[a+1][b-1]*grid[a+2][b-2]*grid[a+3][b-3]))
                {
                    highest = (grid[a][b]*grid[a+1][b-1]*grid[a+2][b-2]*grid[a+3][b-3]);
                }
            }
        }
        cout << "benchmark 5" << endl;
        return 0;
    }
    however the output is just a bunch of random numbers
    the benchmarks do not show up.
    what the heck is going on?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The only thing you're outputting in the posted code are the strings:
    Code:
    benchmark 1/nbenchmark 2/nbenchmark 3/nbenchmark 4/nbenchmark 5
    (Presumably you meant to use backslashes and not the forward slashes you used.)

    If your program used to output anything else, you've removed it from the code you posted.
    Also, you should include the initialization of grid, otherwise we can't run it.

    And you can remove some ugly (and error prone) repetition like this:
    Code:
        long long int highest = 0, m;
        int grid [20][20] = // i initialize the array here
    
        cout << endl << "benchmark 1/n";
        for(int a=0; a < 20; a++)
            for(int b=0; b < 17; b++) {
                m = grid[a][b]*grid[a][b+1]*grid[a][b+2]*grid[a][b+3];
                if(highest < m)
                    highest = m;
            }
    I think it's also necessary to cast the first grid[][] to long long to prevent overflow:
    Code:
                m = (long long)grid[a][b]*grid[a][b+1]*grid[a][b+2]*grid[a][b+3];
    Last edited by oogabooga; 02-05-2012 at 04:43 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    the problem must not be with my code then, i didn't get the same output, as i said before the output was a bunch of random numbers eg:
    Code:
     92304920934 902318749018 38974492836 23489374 -234719 347923
    2349082384 3234 43 093284934 -42934879 0930492304
    i tried rebooting codeblocks, no change
    i tried using rebuild, no change
    when i copied and pasted the code into a new project it worked.

    what could cause a problem like this?
    also, please explain the last bit of code, idk what it means

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    what could cause a problem like this?
    idk
    please explain the last bit of code, idk what it means
    don't worry about it

    Your code accesses outside of the grid in the last loop. E.g., when b is 0, b-1 is -1.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    I missed that, thanks
    Btw this is for projecteuler #11
    Thread done

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-02-2012, 05:14 AM
  2. Getting a random output
    By bigmac(rexdale) in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2008, 01:48 PM
  3. Random Output Numbers
    By Apocalyptic_end in forum C++ Programming
    Replies: 2
    Last Post: 01-26-2008, 04:23 AM
  4. Read from file, output random line
    By Asbestos in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2005, 02:53 PM
  5. Output 3 random sets of 5
    By mrcrossroads in forum C++ Programming
    Replies: 7
    Last Post: 10-29-2002, 06:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21