a reverse function

This is a discussion on a reverse function within the C Programming forums, part of the General Programming Boards category; Hi, Say,my data is as follows: +1 +2 -3 +4 -5 What I want to do is following: I want ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    a reverse function

    Hi,

    Say,my data is as follows:
    +1
    +2
    -3
    +4
    -5

    What I want to do is following:

    I want to write a reverse function that will look like:

    reverse (char *data[],pos1,pos2)

    An example to illustrate this fn is:

    Say I call reverse (data,0,3).

    Then my o/p will be +3 -2 -1 +4 -5 i.e. it will not only reverse the digits between the 2 positions but also the signs.

    Firstly I'm trying this with what if pos2-pos1 =1 then I need to change the sign of only data[pos2][0]. I'm writing:
    Code:
    	if (abs(pos2-pos1)==1)
    	{
    		if(data[pos1][0]=='+')
    			data[pos1][0]=='-';
    		else
    			data[pos1][0]=='+';
    	}
    But strangely even if I check that the difference is 1 the fn is not entering the if loop!!!
    Any idea why this can happen?
    Also what should be a better approach (so that I dont need to consider the difference = 1 case)

    Appreciate your guidance.

    Thanks,
    Angkar

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Oops I used the == instead of =!
    Ok that problem is solved!
    But the second question remains...
    Can anyone suggest me a generic logic?

    AK

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    why dont you just use an int? if it's less than zero, multiply it by negative one. if its greater than zero, multiply it by negative one. after you do that all you have to do is put the numbers in the order that you want...


    also..
    if (abs(pos2-pos1)==1)
    {
    if(data[pos1][0]=='+')
    data[pos1][0]=='-';
    else
    data[pos1][0]=='+';
    }
    that doesn't work. it would be
    Code:
    data[pos1][0] = '-';
    you're not trying to compare anything, you're trying to assign something.
    Last edited by willc0de4food; 04-27-2006 at 08:31 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    My suggestion would be to simplify your life. Positive and negative are part of the nature of numbers, you don't need to complicate things by bringing in strings. There is a bit in the int type dedicated to holding the sign of a number. So use an integer array or whatever.

    Code:
    int reverse (int data[], size_t pos, size_t pos2) 
    {
       /* just swap  and times by a negative one to change the sign */
       int temp = data[pos] * -1; 
       data[pos] = data[pos2];
       data[pos2] = temp;
       
       return data[pos]; /* return the value that changed */
    }
    Last edited by whiteflags; 04-27-2006 at 08:44 PM.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks a lot for the logic! But I may have strings too!Like
    +a
    -b
    +c
    .
    .
    .
    What to do then?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Uh well that depends, if you are coding a program to do algebra I guess you would have to do this, BUT if you're just getting numbers from the user:

    - convert the string data to a numeric type with strtol()
    - shove it in a long array
    - use the function, but make it work with long parameters and a long return type.

    Since In don't know where or why positive or negative char would matter.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Can't I just work on the strings?

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    ...Yes, but it's better if you use dataypes more responsibly. Programmers do not use char to hold numbers, ever, I think.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    But I'm not holding numbers in char!
    I've an array of strings on which I want to apply that reverse operation.

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    phone numbers. lol addresses...birthdays. there would be no point in having something like 3 ints for the mo / day / yr of a birthday and then outputting the 3 int's with the slashes in between. itd be much easier to store such data in a char array.



    well if you're not holding numbers in a char and thats what you told us before, why dont you give us the actual data / code? lol
    Last edited by willc0de4food; 04-27-2006 at 09:14 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thats great . But I think I've not made my problem clear!
    I have to reverse (Along with signs) from pos to pos2 and not just pos and pos2!

    Again thanks for showing the fn strpbrk. Never knew it!

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Oh, well... that's a sorting algorithm, in part. I thought you just wanted to swap. You could use qsort() to solve the problem in part. Study up on it. It's a function in <stdlib.h>

  13. #13
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    iii think i get it now.
    first, analyze the whole string and reverse the signs.
    then go through and with the reversing, find out how far in to the string you need to go, and with a conditional i'd probably copy the information from the original string to a temp string so that it would be in a sorted manner without the data being overwritten. after you've sorted it, simply add the remaining part of the string to the temp string, and then output / do whatever with the temp string. you could copy it back over to the original string but that would be pointless as you already have the string in the format that you need it.
    Registered Linux User #380033. Be counted: http://counter.li.org

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    I've tried to write this:
    Code:
    int start;
    		int end;
    		for (start = p1,end = p2-1;start<=end;start++,end--)
    		{
    			val = (char *) malloc(sizeof(data[end]));
    			val = rev_sign(data[end]);
    			strcpy(i_data[start],val);
    			val = rev_sign(data[start]);
    			strcpy(i_data[end],val);
    
    		}
    
    char * rev_sign(char *ip)
    {
    	if(ip[0]='+')
    		ip[0]='-';
    	else
    		ip[0]='+';
    	return ip;
    }
    But its not working!Could anyone please help me find out where I'm wrong!

    AK

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    btw, i_data is a copy of data.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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