By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

how to convert pseudo code to C++ ?

P: 18
can anyone helps me to convert this pseudo code to C++

The Pseudo code of the proposed method is as follows:
Expand|Select|Wrap|Line Numbers
  1. /* inputs and variables */ 
  2. image cov_img, output_cov_img;             /* input and output images */ 
  3. bitstring secret_msg_bit; 
  4. int embedding_bit_no;                 /* will be input by user */ 
  5. int total_counter=1;                    /* holds number of checked colors */ 
  6. int qualified_counter=1;             /* holds number of qualified colors */ 
  7. int secret_msg_bit_len= length(secret_msg_bit); 
  8. input embedding_bit_no; 
  9. input cov_img; 
  10. qualified_counter_limit =ceiling(secret_msg_bit_len/embedding_bit_no); 
  11. int threshold; 
  12. output_cov_img= cov_img; 
  13. input secret_msg_bit; 
  14. bitstring cropped_secret_msg; 
  15. int original_color;               /* holds the original value of the color */ 
  16. bitstring new_color;            /* holds the new value of the color after embedment */ 
  17. int height=height(cov_img); 
  18. int width=width(cov_img); 
  19. int total_color = height×width×3; 
  20. int color_counter=1; 
  21. int height_counter=1;             /* provides the height of the pixel to be checked */ 
  22. int width_counter=1;            /* provides the width of the pixel to be checked */ 
  23. intarray color_array[total_color];          /* to hold all colors of the pixels in 1D array */ 
  24. int case_one; 
  25. int case_zero; 
  26. /* start of the code */ 
  27. /* putting all colors of the cover image into a 1D array */ 
  28. copy the bytes of the cov_img into 1D array called color_array; 
  29. sort color_array in descending manner; 
  30. threshold is the qualified_counter_limit-th value of color_array; 
  31. if qualified_counter_limit-th value of color_array does not exist then  
  32.     Output “optimal threshold does not exist”; 
  33.     Exit; 
  34. while (qualified_counter <= qualified_counter_limit)           /* ensures embedment of 
  35. whole  the message */ 
  36. if (most significant bits value(color_array[total_counter]) >= threshold)    
  37. /* ensures the color to be qualified */ 
  38. original_color = color_array[total_counter];    /* to be used in adaption */ 
  39. cropped_secret_msg = get next embedding_bit_no bits from secret_msg_bit[ 
  40. (qualified_counter - 1)×embedding bit_no];               /* to be replaced to lsb of 
  41. the color */ 
  42. new_color=Replace least significant bit(s)(color_array[total_count]) with 
  43. cropped_secret_msg; 
  44. qualified_counter = qualified_counter + 1;              /* counter increment */ 
  45. /* matching (adaption) */ 
  46. case_one = decimal value of new_color by changing the matching bit to 1; 
  47. case_zero = decimal value of new_color by changing the matching bit to 0; 
  48. if |original_color – case_one| > |original_color – case_zero| then matching bit 
  49. of new_color=0; 
  50. else matching bit of new_color=1; 
  51. replace corresponding color of output_cov_img  with new_color; 
  52. total_counter= total_counter + 1; 
  53. Show output_cov_img; 
  54. }          /* End of the code */
Sep 23 '13 #1
Share this Question
Share on Google+
10 Replies

Expert 2.5K+
P: 3,112
Hi sarah Alashram and welcome to!

We will be happy to help you convert this code, however we won't do it for you. So, let us see what you've done and tell us where you're stuck or unsure.
Sep 23 '13 #2

P: 18

dear Nepomuk
actualy i don't know how to deal with pseudo code i've just learned how to program using C++ and i need to run this program in C++ urgently for my graduation project
Sep 23 '13 #3

Expert Mod 5K+
P: 9,197
The posted code looks like C++ with parts of it described rather than coded.

I see this:
Expand|Select|Wrap|Line Numbers
  1. sort color_array in descending manner; 
I presume you could write a function which when passed the address of color_array[0] and an int with the number of elements in the array would sort the array in descending sequence.

I suggest you make // comments out of every line of the code and removr all /* and */. Then surround all these commented lines with:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    put the commented lines here
  4. }
Now compile it and Bingo! you have a running program.


Expand|Select|Wrap|Line Numbers
  1. image cov_img, output_cov_img;             /* input and output images 
and re-compile. This will fail because image is not declared.

So determine how to declare image.

Recompile. Keep a it until it compiles.

Then UN-COMMENT something else yu can get your head around. Then recompile.

The idea is to keep the code compiling without error all the time. When you add to it and it doesn't compile it's probably the last thing you added that has a problem.

Eventually, all of the code will be uncommented and replaced with code that compiles.

Post again and let us know how it's going.
Sep 24 '13 #4

Expert 2.5K+
P: 3,112
Pseudo-code is a description of an algorithm that i supposed to be relatively easy to read, ideally without assuming familiarity with any specific programming language; it often looks like "real" code though and theoretically real code could be interpreted as pseudo-code.

So, the process of translating one to the other is as weaknessforcats describes, though I'd put a different emphasis: The idea is to find solutions for the various actions described in pseudo-code in the programming language of your choice, here C++. It's similar to when someone describes an algorithm with words rather than with code and you try to implement it. An example:
  • Line 4 defines the variable embedding_bit_no which is an integer. This should work in C++ without change. There is also a comment here: /* will be input by user */. Interesting.
  • Line 8 is
    Expand|Select|Wrap|Line Numbers
    1. input embedding_bit_no;
    This means you should read input and save it to the value. Now remember the comment from line 4 - the input should be read from the user. How do you read input from the user? I'll leave that to you.
In some cases the translation will be more complicated of course, but if so try to break it down into smaller steps just as you would when solving any problem in C++ knowing the basic solution.
Sep 24 '13 #5

P: 18

firstly i would like you for trying to help me if it possible i'll try to compile the program step by step but i need help to check if i'm writting the codes in the right way
Sep 24 '13 #6

P: 18

like those 2 lines
Expand|Select|Wrap|Line Numbers
  1. image cov_img, output_cov_img;             // input and output images  
  2. bitstring secret_msg_bit;    
how should i declare them?
Sep 24 '13 #7

Expert Mod 5K+
P: 9,197
I don't know. It depends on what you think an image is.

What do you think it is?
Sep 24 '13 #8

P: 18

this codes supposed to describe SLSB algorithm this algorithm deals with an image do some changes in the pixel to add a secret message and then come up with another image with the secret message
the thing now i don't know what is bitstring for example
do you think if i used MATLAB is better than C++??
Sep 24 '13 #9

Expert Mod 5K+
P: 9,197
I'm just a C++ person. There is an slsb in both MATLAB and Python and there is a bitstring in Python. Neither of these is C++ so if you need to code in C++ you can't uses either one.

Here's where I'd ask the author of your pseudo code what is the intent. Especially things like image and bitstring.
Sep 24 '13 #10

P: 18
ok if i need to translate it in MATLAB who may help me in doing this
Sep 24 '13 #11

Post your reply

Sign in to post your reply or Sign up for a free account.