Loop not working Correctly

This is a discussion on Loop not working Correctly within the C Programming forums, part of the General Programming Boards category; Hi, I am working on image steganography code using MATLAB and s, n, p are calculated values which I am ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Loop not working Correctly

    Hi,
    I am working on image steganography code using MATLAB and s, n, p are calculated values which I am getting as per requirement but struggling to get “Bits_to_Modify ” column result shown in below table

    Output of Existing program


    Code:
    My text=’ab’
        TextLength=2
        Binary representation of text:
        s= 0110000101100010   
        n=8 (Textlength*4)
        p=
        2
        0
        1
        2 
        0 
        1
        2 
        0
    First converting text to bytes, then calculating index variable (called as p) & formula is MOD(No of Bytes,3).I have text length as 2 bytes so Index Variable (P) is 2..

    I will always have index variable values as 0 or 1 or 2 which will be based on no. of Bytes I have assigned

    for first two bits (01) , index variable assigned as 2
    for next two bits (10), index variable assigned as 0
    for next two bits (00), index variable assigned as 1
    for next two bits (01), index variable assigned as 2..
    so on till end of my bits.

    In 5th Pixel need to embed the data bits 01 in 6th and 8th bit locations, and next value of p becomes 0. Need to embed the next data bits 10 into 6th Pixel in 6th and 7th bit locations, next value of p becomes 1. Now need to embed the next two bits 00 into 7th Pixel in 7th and 8th bit locations and so on.

    Code:
    Pixcel      IndexVariable         Bits_to_Modify
    5	  2	               b6=0 b8=1
    6	  0	               b6=1 b7=0
    7	  1	               b7=0 b8=0
    8	  2	               b6=0 b8=1
    9	  0	               b6=0 b7=1
    10	  1	               b7=1 b8=0
    11	  2	               b6=0 b8=0
    12	  0	               b6=1 b7=0
    Question

    I have written the following program syntax most same to C, taking above values of S,n & P which i am getting

    but it dont give following output..It keeps repeating values in output..I have tried logic on paper and looks correct. Please provide me direction for what i am missing in logic



    Code:
    for j=0:2:2*n-2
       for i=1:1:n
         if p(i)==2
         p1= p(i)
         pv=(i+4)
         b6=s(j+1)
         b8=s(j+2) 
         end     if p(i)==0
         p1= p(i)
         pv=(i+4)
         b6=s(j+1)
         b7=s(j+2) 
         end     if p(i)==1
         p1= p(i)
         pv=(i+4)
         b7=s(j+1)
         b8=s(j+2) 
         end    
    end
    end


    Expected Output

    Code:
    p1=2
    pv=5
    b6=0
    b8=1
    p1=0
    pv=6
    b6=1
    b7=0
    p1=1
    pv=7
    b7=0
    b8=0
    p1=2
    pv=8
    b6=0
    b8=1
    p1=0
    pv=9
    b6=0
    b7=1
    p1=1
    pv=10
    b7=1
    b8=0
    p1=2
    pv=11
    b6=0
    b8=0
    p1=0
    pv=12
    b6=1
    b7=0
    Last edited by patneel; 03-15-2012 at 02:59 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There is nothing "same to C" here I'm afraid. Please post in the appropriate forum.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by claudiu View Post
    There is nothing "same to C" here I'm afraid. Please post in the appropriate forum.
    Hi,

    Equivalent c code could be

    s string/numeric array with following values
    p string/numeric array with following alues

    Code:
     s= 0110000101100010   
        n=8 
        p=
        2
        0
        1
        2 
        0 
        1
        2 
        0
    
    for (j=0;j<2*n-2;j=j+2)
       for (i=1;i<n;i++)
         if p(i)==2
         p1= p(i)
         pv=(i+4)
         b6=s(j+1)
         b8=s(j+2) 
         end     if p(i)==0
         p1= p(i)
         pv=(i+4)
         b6=s(j+1)
         b7=s(j+2) 
         end     if p(i)==1
         p1= p(i)
         pv=(i+4)
         b7=s(j+1)
         b8=s(j+2) 
         end    
    end
    end

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For, While and Do While Loops in C - Cprogramming.com
    Arrays in C - Cprogramming.com

    If you want C, learn C. If you want MATLAB, go find a MATLAB forum.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by patneel View Post
    Hi,

    Equivalent c code could be

    s string/numeric array with following values
    p string/numeric array with following alues

    Code:
     s= 0110000101100010   
        n=8 
        p=
        2
        0
        1
        2 
        0 
        1
        2 
        0
    
    for (j=0;j<2*n-2;j=j+2)
       for (i=1;i<n;i++)
         if p(i)==2
         p1= p(i)
         pv=(i+4)
         b6=s(j+1)
         b8=s(j+2) 
         end     if p(i)==0
         p1= p(i)
         pv=(i+4)
         b6=s(j+1)
         b7=s(j+2) 
         end     if p(i)==1
         p1= p(i)
         pv=(i+4)
         b7=s(j+1)
         b8=s(j+2) 
         end    
    end
    end
    Not only are you stubborn but now you have embarked on a crusade to show us we are wrong about what is and is not C. Well then, I think you should be having no problems solving your problem.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working correctly
    By Charles Seat in forum C Programming
    Replies: 3
    Last Post: 01-24-2012, 11:17 AM
  2. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  3. Pointers are not working correctly
    By adrian_fpd in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 06:55 PM
  4. function not working correctly
    By mackieinva in forum C Programming
    Replies: 0
    Last Post: 09-29-2007, 08:22 PM
  5. File I/O not working correctly
    By gL_nEwB in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2006, 10:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21