Note that there are some explanatory texts on larger screens.

plurals
  1. POReading into arrays in C++
    primarykey
    data
    text
    <p>I'm practicing for a competition (that's where <a href="https://stackoverflow.com/questions/502443/prime-numbers">my previous question</a> came from). I got the algorithm sorted out for the question, but I'm having some problems with the actual programming. It's a solo competition, so I really need to get this sorted out before I go for it. This is the question.</p> <blockquote> <p><strong>TASK 3: GECKO</strong> During the rainy season, one of the walls in the house is infested with mosquitoes. The wall is covered by h × w square tiles, where there are h rows of tiles from top to bottom, and w columns of tiles from left to right. Each tile has 1 to 1000 mosquitoes resting on it. A gecko wants to eat as many mosquitoes as possible, subject to the following restrictions. It starts by choosing any tile in the top row, and eats the mosquitoes in that tile. Then, it moves to a tile in the next lower row, eats the mosquitoes on the tile, and so on until it reaches the floor. When it moves from one tile to a tile in the next lower row, it can only move vertically down or diagonally to the left or right (see Figure 1). Given the values of h and w, and the number of mosquitoes resting on each tile, write a program to compute the maximum possible number of mosquitoes the gecko can eat in one single trip from the top to the bottom of the wall.</p> <p>An example input file would be:<br> Example</p> <p>6 5<br> 3 1 7 4 2<br> 2 1 3 1 1<br> 1 2 1 1 8<br> 2 2 1 5 3<br> 2 1 4 4 4<br> 5 7 2 5 1</p> </blockquote> <p>The problem I have is in reading in the numbers (or the top of the list of problems). My current code for reading in is:</p> <pre><code>ifstream read; read.open("input.txt"); write.open("output.txt"); int width, height, wall[500][500]; read &gt;&gt; height; read &gt;&gt; width; for ( int count1 = 0; count1 &lt; height; count1++) { for ( int count2 = 0; count2 &lt; width; count2++) { read &gt;&gt; wall[count1][count2]; } } </code></pre> <p>When I tested it with a <code>cout</code> to print all the numbers read in, all i got was gibberish. Right now I can't spot any errors with it, can anyone see the problem? Thanks. (fixed) Thanks again. </p> <p>I tested the read in and it's perfect now. However, the number of flies I'm getting is still off. For example the input<br> 1 1<br> 23<br> Should give the output 23, but I'm getting 0 as an output. Here's my code:</p> <pre><code>int h = 0, w = 0, compare1, compare2, compare3, currentInt; for ( int count3=( height-2 ); count3 &gt;= 0; count3--) { for( int count4 = 0; count4 &lt; width; count4++) { h = count3; w = count4; currentInt = wall[h][w]; // read in affected integers. if( w != 0 ) { compare1 = wall[h+1][w-1]; } compare2 = wall[h+1][w]; compare3 = wall[h+1][w+1]; if( w!= 0) // Compare and replace. { if((( currentInt + compare1) &gt;=(currentInt + compare2)) &amp;&amp; ((currentInt + compare1)&gt;=(currentInt + compare3))) { wall[h][w] = ( currentInt+compare1); } if((( currentInt + compare2) &gt;(currentInt + compare1)) &amp;&amp; ((currentInt + compare2)&gt;(currentInt + compare3))) { wall[h][w] = ( currentInt+compare2); } if((( currentInt + compare3) &gt;=(currentInt + compare2)) &amp;&amp; ((currentInt + compare1)&gt;(currentInt + compare1))) { wall[h][w] = ( currentInt + compare3); } } else { if ((currentInt + compare2) &gt;= ( currentInt + compare3)) { wall[h][w] = ( currentInt + compare2); } else { wall[h][w] = ( currentInt + compare3); } } } } int maxFlies=0; for (int count5 = 1; count5 &lt; width;count5++) { maxFlies = wall[0][0] ; if ( maxFlies &lt; wall[0][count5]) { maxFlies = wall[0][count5] ; } } write&lt;&lt;maxFlies&lt;&lt;endl; read.close(); write.close(); return 0; </code></pre> <p>What I'm trying to do with that is basically: Read in from the second last line. Add it with the only blocks it can reach below. Then move up and finally test which one at the top line is the biggest integer. Thanks for the fast replies.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload