Decoding Image Homework: Troubleshoot Function

  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    Image
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
15 replies · 2K views
sandy.bridge
Messages
797
Reaction score
1

Homework Statement


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; count << F[i][k];
				count++; num_pixels_decoded++;
				if (count == Z){count << endl;i++; count = 0;}
				}
				}

				count << endl;

				for(int x = 0; x < Y; x++)
				{
					for(int y = 0; y < Z; y++)
					{
						count << F[x][y];
					}
				}cin >> skipws;
return;}
}

I believe the issue has something to do with assigning the array. The first count << 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, count << 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:o2: 1:,1:'1:`1:.2: 1:o1:,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:o3: 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 count << produces an image of Homer Simpson. The second count does nothing at all.
 
Physics news on Phys.org
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.
sandy.bridge said:

Homework Statement


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; 
        count << F[i][k];
        count++; 
        num_pixels_decoded++;
        if (count == Z)
        {
          count << endl;
          i++; 
          count = 0;
        }
      }
    }

    count << endl;

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

    cin >> skipws;
    return;
  }
}

I believe the issue has something to do with assigning the array. The first count << 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, count << 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:o2: 1:,1:'1:`1:.2: 1:o1:,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:o3: 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 count << produces an image of Homer Simpson. The second count does nothing at all.
 
My apologies! How do you indent inside the
PHP:
? I tried but it kept taking me out of the text box.
 
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:
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.
 
Mark44 said:
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:
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.
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.
 
This is what the first loop outputs:
Code:
                      ,;-.                      
                    ,((--\).                    
                   /        \                   
                  |          |                  
                  |          |                  
                 (,'"`.  ,'"`.)                 
                 :     \/     ;                 
                 `.o  ,'`.  o,'                 
                 (|`>'`--'`<'|)            ,-,  
     ,.           |/        \|         ,-./ /   
   _ | \,-.       (          )        | `-'`--. 
  ( `' (_/|__      \   (o   /       ,-'     ,-' 
   ;         )    ,|`.  - ,'|.      `-.   ) \   
   | (    ,-'   _/ `-.`""',-' \---.   /      ;  
   |     |   ,-'  \  /\  / \  |   |--/       |  
   |     |_,|    / \/  \/   \/\   |          |  
   |     `  \   |              \  /        ,'   
   |         \  |              | /      _,'     
   :          \ ,              `/------'        
    `-.___,---')                `.              
             ,'                   \             
            /                      \            
           :                        :           
           |                      _,|           
            \--.___         __,--' ;            
             `.    `"""""""'      ,'            
               |                  |             
               |      .____,      |             
               |         |        |             
               |         |        |             
               |         |        |             
               |         |        |             
               |         |        |             
     -hrr-     |         |        |             
               |         |        |             
               |-._____,-|-.____,-|             
               |_        |_       |             
             ,'  `------'| `-----' \            
            /           _|_         \           
            `--._____,-'   `-.___,-'

Second loop outputs a bunch of mumble.
 
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:
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.
 
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!
 
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!
 
Any variable, regardless of type? Not directly. You can fill every variable with a random content, but you have to code it by yourself.
 
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: