Decoding Image Homework: Troubleshoot Function

  • Thread starter sandy.bridge
  • Start date
  • Tags
    Image
In summary, the conversation discusses a function that is supposed to decode a string of integers and characters into an image, but is not working correctly. The code for the function is also provided. The issue seems to be with assigning the array, as the first output correctly displays the image, but the second loop does not output anything. The conversation also includes an example input and output, demonstrating the issue.
  • #1
sandy.bridge
798
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: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: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nothing at all.
 
Physics news on Phys.org
  • #2
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: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.
 
  • #3
My apologies! How do you indent inside the
PHP:
? I tried but it kept taking me out of the text box.
 
  • #5
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.
 
  • #6
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.
 
  • #7
This is what the first loop outputs:
Code:
                      ,;-.                      
                    ,((--\).                    
                   /        \                   
                  |          |                  
                  |          |                  
                 (,'"`.  ,'"`.)                 
                 :     \/     ;                 
                 `.o  ,'`.  o,'                 
                 (|`>'`--'`<'|)            ,-,  
     ,.           |/        \|         ,-./ /   
   _ | \,-.       (          )        | `-'`--. 
  ( `' (_/|__      \   (o   /       ,-'     ,-' 
   ;         )    ,|`.  - ,'|.      `-.   ) \   
   | (    ,-'   _/ `-.`""',-' \---.   /      ;  
   |     |   ,-'  \  /\  / \  |   |--/       |  
   |     |_,|    / \/  \/   \/\   |          |  
   |     `  \   |              \  /        ,'   
   |         \  |              | /      _,'     
   :          \ ,              `/------'        
    `-.___,---')                `.              
             ,'                   \             
            /                      \            
           :                        :           
           |                      _,|           
            \--.___         __,--' ;            
             `.    `"""""""'      ,'            
               |                  |             
               |      .____,      |             
               |         |        |             
               |         |        |             
               |         |        |             
               |         |        |             
               |         |        |             
     -hrr-     |         |        |             
               |         |        |             
               |-._____,-|-.____,-|             
               |_        |_       |             
             ,'  `------'| `-----' \            
            /           _|_         \           
            `--._____,-'   `-.___,-'

Second loop outputs a bunch of mumble.
 
  • #8
Should the inner loop of the later cout be going from 0 to N - 1 rather than from 0 to Z - 1?
 
  • #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:
  • #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:

1. What is the purpose of decoding images in homework?

The purpose of decoding images in homework is to understand and interpret the information contained within an image. This can include identifying objects, analyzing data, and extracting relevant information to solve a problem or answer a question.

2. How do I troubleshoot decoding image homework?

To troubleshoot decoding image homework, first make sure that you have a clear understanding of the decoding process and the specific instructions given for the assignment. Then, check for any errors or inconsistencies in your decoding method. If you are still having trouble, seek assistance from a teacher or classmate.

3. What are some common challenges when decoding images?

Some common challenges when decoding images include identifying unfamiliar symbols or patterns, dealing with low resolution or blurry images, and interpreting conflicting or incomplete information. It is important to approach these challenges with patience and critical thinking.

4. Are there any tools or resources that can help with decoding images?

Yes, there are several tools and resources that can aid in decoding images. These include image enhancement software, online image databases, and decoding guides or tutorials. It is important to use these tools in conjunction with your own analysis and critical thinking skills.

5. How can decoding image homework improve my scientific skills?

Decoding image homework can improve your scientific skills by honing your ability to observe and analyze data, think critically and creatively, and make connections between different pieces of information. These skills are essential for success in the scientific field and can also be applied to other areas of life.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
949
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
668
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
888
  • Engineering and Comp Sci Homework Help
Replies
1
Views
324
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top