Thread: Array question

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    11

    Array question

    Hi.. Working on a function.. which should reverse a simple array...

    Code:
    void reverser(char input[], char output[])
    {
    	int i, c;
    	for(i=0; input[i] != '\0'; ++i);
    	for (c=0;i !=0; ++c){
    		output[c] = input[i];
    		--i;
    	}
    	output[c+1] = input[i]
    
    	/*while (i != 0) {
    		if (i==0)
    			output[c+1] = input[i];
    		output[c] = input[i];
    		--i;
    		++c;
    		}
    	*/
    
    	/*output[0] = input[4];
    	output[1] = input[3];
    	output[2] = input[2];
    	output[3] = input[1];
    	output[4] = input[0]; */
    
    	printf("C = %d I = %d \nWhich makes : %s\n", c, i, output);
    }
    This is the code .. As you can see I tried it before with a while statement which is marked out now .. same result .. the printf of output is empty ..

    As you can see in the end is a marked out code of manually reversing it .. which does strangly work...

    As far as I can see c and i in the for statement represent the same numbers as when I manually fill the array of output.

    what is going wrong here??

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Basically you don't need two loops to do this...

    You need your loop to start at the beginning of the array.
    On each iteration of the loop, swap two values ... array[i] for array[end - i]
    when you get to the array size / 2 you're done.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    I see your point...

    Though I must add I;m into my 3th day of C programming so that's why my codes are somewhat bloated...

    But aside from that (which I'm interested in also ofcourse...). This could should be working as far as I can see. why isn't it.?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    which should reverse a simple array
    O_o

    Your code has the property that it swaps every single byte of a "C String".

    That doesn't sound right at all does it?

    [Edit]Just focus on fixing that logic and you should get the rest.[/Edit]

    Basically you don't need two loops to do this...
    You need your loop to start at the beginning of the array.
    On each iteration of the loop, swap two values ... array[i] for array[end - i]
    when you get to the array size / 2 you're done.
    He isn't using two loops for the reverse algorithm; the first loop is just `strlen'.

    He isn't modifying the input.

    Soma
    Last edited by phantomotap; 03-29-2011 at 09:14 AM. Reason: none of your business

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Thanks for the reply ...

    I do not really understand what is not right about swapping the bytes in an array ...
    This code is just for learning purposes so I'm not asking for a code re-write, I want to understand what is going on here..

    The way I see it .. When I reverse the array manually :
    Code:
    output[0] = input[4];
    	output[1] = input[3];
    	output[2] = input[2];
    	output[3] = input[1];
    	output[4] = input[0];
    It does work .. (but I don't have to eleborate why this isn;t ideal :P)
    But when I automate it, it does not output anything.

    the c and i var represent the same numbers (even tested this with a printf of it after bumping my head 3 times ) as the ones in the manually reverse.

    Thanks.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by viper2k View Post
    I see your point...

    Though I must add I;m into my 3th day of C programming so that's why my codes are somewhat bloated...

    But aside from that (which I'm interested in also ofcourse...). This could should be working as far as I can see. why isn't it.?
    Well, here's a little exercise for you that will help you develop troubleshooting skills...
    Take your 2 loops, on paper and track the values in the arrays through the loops. Don't look at your code, look at the result of your code...

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    *shrug*

    You either got lucky, or you managed a very specific test case.

    Either way, I'll give you a hint.

    After you follow up with CommonTater's suggestion.

    Soma

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Hmm...

    I've been following the values .. and the numbers check out...
    I've edited the code so it outputs the value changes.

    Code:
    void reverser(char input[], char output[])
    {
    	int i, c;						//initialize vars
    		
    	for(i=0; input[i] != '\0'; ++i);// counts length of input
    	printf("starting value of i :%d\n", i);	// tests the result of i
    	
    	for (c=0;i !=0; ++c){			// for as long as i is not 0 loop & increase c by 1
    		printf("i=%d c=%d\n", i, c);// tests results of i & c while looping
    		output[c] = input[i];		// copy input into output
    		--i;						// decrease i by 1
    	}
    	output[c+1] = input[i];			//copy final character on array
    
    	printf("Final : c = %d i = %d Which makes : %s\n", c, i, output); // test final output
    }
    When executed it results :
    (hello & ^Z are my input)

    input or EOF ?
    hello



    input or EOF ?
    ^Z
    starting value of i :5
    i=5 c=0
    i=4 c=1
    i=3 c=2
    i=2 c=3
    i=1 c=4
    Final : c = 5 i = 0 Which makes :

    My analyses of this is:
    The values itself are correct so the loops (although it might be done more efficient) do the job right.

    The copying of input to output goes wrong though

    Code:
    output[c] = input[i];		// copy input into output
    There... Though the strange thing is .. when I replace the c in output to i ... It DOES copy array content.. Though ofcourse it's not the right output.


    Am I analyzing this wrong??

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The original does not work because i is at the end of the array - pointing to the null terminator. You are indexing input[i] one element beyond where it should be.
    And because the loop stops early, the assignment after it should be output[c] = input[i];

    Sorry, I just posted this before seeing your above analysis. I think you've covered it.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Hint: (As I promised.) "C Strings" are null terminated.

    Soma

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Ah .. that is it ...

    thats fun about coding ... this is something I should have known.. I just was unable to see it. ..


    I placed a

    Code:
    --i;
    After the strlen

    and realized that

    Code:
    output[c] = input[i];			//copy final character on array
    Was useless ..

    **edit ... incorrect ofcourse... this IS useful.. but the c+1 I originally had was wrong.

    Thanks a bunch guys ... I'm learning

    btw minor side question... I know the mark // is actually a C++ syntax .. but it's easier for marking a single or partial line ..

    I'm trying to learn ANSI C no vague interpertation .. should I drop this habbit or is it ok to use in the world of C ??

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    // is legal in the newer C standards.

    From C99 section 6.4.9:
    1 Except within a character constant, a string literal, or a comment, the characters /*
    introduce a comment. The contents of such a comment are examined only to identify
    multibyte characters and to find the characters */ that terminate it.

    2 Except within a character constant, a string literal, or a comment, the characters //
    introduce a comment that includes all multibyte characters up to, but not including, the
    next new-line character. The contents of such a comment are examined only to identify
    multibyte characters and to find the terminating new-line character.
    Last edited by itsme86; 03-29-2011 at 10:42 AM.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It isn't standard C89, but it is standard C99.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question : Passing Array Arguments as Pointers
    By NEMEAN in forum C Programming
    Replies: 1
    Last Post: 12-04-2010, 03:33 PM
  2. Array Question
    By Alecroy in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2010, 03:22 PM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  4. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM