Decoding Image Homework: Troubleshoot Function

  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    Image
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a function designed to decode a string of integers and characters into an image. Participants are examining the code's functionality, particularly focusing on why the output is not as expected. The scope includes technical debugging and code analysis.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet and describes the issue where the first output loop works correctly, but the second loop does not produce any output.
  • Another participant suggests that the code is difficult to read due to formatting and provides a re-indented version for clarity.
  • Several participants note that the two loops serve different purposes, with one participant explaining that the outer loop in the first section increments the counter only under specific conditions, while the second loop runs through all indices.
  • There is a suggestion that the same loop variables could be reused in the second loop for consistency.
  • One participant expresses confusion about the differences in loop behavior and seeks clarification on the logic behind the array indexing.
  • A participant shares the output of the first loop, illustrating the image produced by the function.

Areas of Agreement / Disagreement

Participants generally agree on the observation that the two loops are functioning differently, but there is no consensus on the exact cause of the issue or how to resolve it. The discussion remains unresolved.

Contextual Notes

There are indications of potential misunderstandings regarding loop control and array indexing, but these are not fully clarified within the discussion.

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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'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: 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|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.
 
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 count 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 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K