1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Decoding into image

  1. Mar 20, 2013 #1
    1. The problem statement, all variables and given/known data
    Hello all. I have a function that is supposed to decode a string of integers and characters into an image. The function is not working 100%, and I cannot seem to figure out why.

    Here is my code:

    PHP:
    void part5(char F[80][80], int Y, int Z) // function for q1p5.
    {
    cin >> noskipws;

    int num_pixels = Y*Z;
    int num_pixels_decoded = 0;
    while (num_pixels_decoded < num_pixels)
    {

                    int N, i = 0, count = 0;
                    char ch; char dy;


                    while(i < Y){
                    cin >> N >> dy >> ch;
                    for (int k=0; k<N; k++)
                    {
                    F[i][k]=ch; cout << F[i][k];
                    count++; num_pixels_decoded++;
                    if (count == Z){cout << endl;i++; count = 0;}
                    }
                    }

                    cout << endl;

                    for(int x = 0; x < Y; x++)
                    {
                        for(int y = 0; y < Z; y++)
                        {
                            cout << F[x][y];
                        }
                    }


    cin >> skipws;
    return;}
    }
    I believe the issue has something to do with assigning the array. The first cout << F[k] outputs the image perfectly; however, the second loop (which I made just to ensure that the array was indeed saving correctly), is not giving anything to the console. That is, cout << F[x][y]; does not do a thing.


    As an example:
    If I input :22: 1:,1:;1:-1:.42: 1:,2:(2:-1:\1:)1:.39: 1:/8: 1:\37: 1:|10: 1:|36: 1:|10: 1:|35: 1:(1:,1:'1:"1:`1:.2: 1:,1:'1:"1:`1:.1:)34: 1::5: 1:\1:/5: 1:;34: 1:`1:.1:eek:2: 1:,1:'1:`1:.2: 1:eek:1:,1:'34: 1:(1:|1:`1:>1:'1:`2:-1:'1:`1:<1:'1:|1:)12: 1:,1:-1:,7: 1:,1:.11: 1:|1:/8: 1:\1:|9: 1:,1:-1:.1:/1: 1:/6: 1:_1: 1:|1: 1:\1:,1:-1:.7: 1:(10: 1:)8: 1:|1: 1:`1:-1:'1:`2:-1:.3: 1:(1: 1:`1:'1: 1:(1:_1:/1:|2:_6: 1:\3: 1:(1:eek:3: 1:/7: 1:,1:-1:'5: 1:,1:-1:'4: 1:;9: 1:)4: 1:,1:|1:`1:.2: 1:-1: 1:,1:'1:|1:.6: 1:`1:-1:.3: 1:)1: 1:\6: 1:|1: 1:(4: 1:,1:-1:'3: 1:_1:/1: 1:`1:-1:.1:`2:"1:'1:,1:-1:'1: 1:\3:-1:.3: 1:/6: 1:;5: 1:|5: 1:|3: 1:,1:-1:'2: 1:\2: 1:/1:\2: 1:/1: 1:\2: 1:|3: 1:|2:-1:/7: 1:|5: 1:|5: 1:|1:_1:,1:|4: 1:/1: 1:\1:/2: 1:\1:/3: 1:\1:/1:\3: 1:|10: 1:|5: 1:|5: 1:`2: 1:\3: 1:|14: 1:\2: 1:/8: 1:,1:'6: 1:|9: 1:\2: 1:|14: 1:|1: 1:/6: 1:_1:,1:'8: 1::10: 1:\1: 1:,14: 1:`1:/6:-1:'12: 1:`1:-1:.3:_1:,3:-1:'1:)16: 1:`1:.27: 1:,1:'19: 1:\25: 1:/22: 1:\23: 1::24: 1::22: 1:|22: 1:_1:,1:|23: 1:\2:-1:.3:_9: 2:_1:,2:-1:'1: 1:;25: 1:`1:.4: 1:`7:"1:'6: 1:,1:'27: 1:|18: 1:|28: 1:|6: 1:.4:_1:,6: 1:|28: 1:|9: 1:|8: 1:|28: 1:|9: 1:|8: 1:|28: 1:|9: 1:|8: 1:|28: 1:|9: 1:|8: 1:|28: 1:|9: 1:|8: 1:|18: 1:-1:h2:r1:-5: 1:|9: 1:|8: 1:|28: 1:|9: 1:|8: 1:|28: 1:|1:-1:.5:_1:,1:-1:|1:-1:.4:_1:,1:-1:|28: 1:|1:_8: 1:|1:_7: 1:|26: 1:,1:'2: 1:`6:-1:'1:|1: 1:`5:-1:'1: 1:\24: 1:/11: 1:_1:|1:_9: 1:\23: 1:`2:-1:.5:_1:,1:-1:'3: 1:`1:-1:.3:_1:,1:-1:'60: .


    Then the first cout << produces an image of Homer Simpson. The second cout does nothing at all.
     
  2. jcsd
  3. Mar 20, 2013 #2

    Mark44

    Staff: Mentor

    Your code was a little difficult to read, as you had some statements "hidden" on the same line as one or two other statements. I have re-indented your code to make it more understandable to other readers.
     
  4. Mar 20, 2013 #3
    My apologies! How do you indent inside the
    PHP:
    ? I tried but it kept taking me out of the text box.
     
  5. Mar 20, 2013 #4

    Mark44

    Staff: Mentor

    Use [ code ] and [ /code ] (no spaces).
     
  6. Mar 20, 2013 #5

    Mark44

    Staff: Mentor

    Your two loops seem to be doing different things. In the first loop, the outer loop (while) counter i is incremented only if count == Z.

    In the second loop, shown below, the outer loop runs from 0 to Y - 1, and the inner loop runs from 0 to Z - 1.

    Code (Text):
    for(int x = 0; x < Y; x++)
    {
       for(int y = 0; y < Z; y++)
      {
         cout << F[x][y];
      }
    }
     
    BTW, you could have reused the same loop variables i and j in the second nested loop, instead of declaring two new variables. It's probably a carryover from Fortran days, but for loop control variables are often i, j, and k.
     
  7. Mar 20, 2013 #6
    Right. Counter i is incremented if count == Z, meaning the array has reached the end of the row (which is Z), by increasing i at this point, we are merely going to the next row, no?

    Also, that makes sense to me for the second loop. Y = # of rows in the array, Z = number of columns.

    I'm obviously missing something here, but I am not seeing it.
     
  8. Mar 20, 2013 #7
    This is what the first loop outputs:
    Code (Text):

                          ,;-.                      
                        ,((--\).                    
                       /        \                  
                      |          |                  
                      |          |                  
                     (,'"`.  ,'"`.)                
                     :     \/     ;                
                     `.o  ,'`.  o,'                
                     (|`>'`--'`<'|)            ,-,  
         ,.           |/        \|         ,-./ /  
       _ | \,-.       (          )        | `-'`--.
      ( `' (_/|__      \   (o   /       ,-'     ,-'
       ;         )    ,|`.  - ,'|.      `-.   ) \  
       | (    ,-'   _/ `-.`""',-' \---.   /      ;  
       |     |   ,-'  \  /\  / \  |   |--/       |  
       |     |_,|    / \/  \/   \/\   |          |  
       |     `  \   |              \  /        ,'  
       |         \  |              | /      _,'    
       :          \ ,              `/------'        
        `-.___,---')                `.              
                 ,'                   \            
                /                      \            
               :                        :          
               |                      _,|          
                \--.___         __,--' ;            
                 `.    `"""""""'      ,'            
                   |                  |            
                   |      .____,      |            
                   |         |        |            
                   |         |        |            
                   |         |        |            
                   |         |        |            
                   |         |        |            
         -hrr-     |         |        |            
                   |         |        |            
                   |-._____,-|-.____,-|            
                   |_        |_       |            
                 ,'  `------'| `-----' \            
                /           _|_         \          
                `--._____,-'   `-.___,-'
    Second loop outputs a bunch of mumble.
     
  9. Mar 20, 2013 #8

    Mark44

    Staff: Mentor

    Should the inner loop of the later cout be going from 0 to N - 1 rather than from 0 to Z - 1?
     
  10. Mar 20, 2013 #9
    N varies throughout the loop, though. For example, if the string 5A2B6Y is input, N first equals 5, then 2, then 6. I tried changing it just to see and same skewed results.
     
    Last edited: Mar 20, 2013
  11. Mar 20, 2013 #10

    Mark44

    Staff: Mentor

    Both nested loops need to do the same thing if you want the output to be the same (obviously!). The inner loop of the first nested loop doesn't run a predetermined number of times. The inner loop of the second nested loop runs Z times.
     
  12. Mar 21, 2013 #11

    Borek

    User Avatar

    Staff: Mentor

    I don't see endl in the second loop, isnt' it the problem?
     
  13. Mar 21, 2013 #12
    I got it working last night. Both of you were right. I changed the k variable to count, and I added an endl; on the second loop! Thanks!
     
  14. Mar 21, 2013 #13

    Mark44

    Staff: Mentor

    That's why they pay us the big bucks. Oh, wait, we're volunteers! Never mind...
     
  15. Mar 21, 2013 #14
    Mwahaha!!!!

    Question: is there a way to generate a random variable? I know how to generate a random number, or a random character, but not sure about variable. I have a while loop that is filling up N records. The problem I am having is the value N depends on what the user inputs, hence I don't know how many records to "pre initialize". Thanks!
     
  16. Mar 21, 2013 #15

    Borek

    User Avatar

    Staff: Mentor

    Any variable, regardless of type? Not directly. You can fill every variable with a random content, but you have to code it by yourself.
     
  17. Mar 21, 2013 #16
    It be the name of a record that had floating components.


    EDIT: I think I will just try to use an array to store the records.
     
    Last edited: Mar 21, 2013
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Decoding into image
  1. Address Decoder (Replies: 1)

  2. Decoding fail (Replies: 7)

  3. Runlength Decoding ( (Replies: 10)

Loading...