Decoding Image Homework: Troubleshoot Function

  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    Image
AI Thread Summary
The function designed to decode a string of integers and characters into an image is encountering issues, particularly with the output of the second loop, which fails to display the expected results. The first loop successfully outputs the image, but the second loop does not reflect the stored values in the array, suggesting a problem with how the array is being accessed or populated. Discussions highlight that the two loops serve different purposes, with the first loop iterating based on the count of pixels while the second loop runs through fixed dimensions of the array. Adjustments were made, including changing loop variables and adding necessary line breaks, which resolved some output issues. The conversation also touched on generating random variables, suggesting that while random numbers can be generated, creating random variable names requires a more complex approach.
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; 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: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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 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.
 
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; 
        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: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: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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.
 
My apologies! How do you indent inside the
PHP:
? I tried but it kept taking me out of the text box.
 
Use [ code ] and [ /code ] (no spaces).
 
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.
 
Should the inner loop of the later cout be going from 0 to N - 1 rather than from 0 to Z - 1?
 
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:
  • #10
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.
 
  • #11
I don't see endl in the second loop, isnt' it the problem?
 
  • #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!
 
  • #13
That's why they pay us the big bucks. Oh, wait, we're volunteers! Never mind...
 
  • #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!
 
  • #15
Any variable, regardless of type? Not directly. You can fill every variable with a random content, but you have to code it by yourself.
 
  • #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:

Similar threads

Replies
0
Views
162
Replies
3
Views
1K
Replies
1
Views
2K
Replies
10
Views
3K
Replies
7
Views
2K
Replies
7
Views
1K
Replies
3
Views
3K
Back
Top