Reverse array in C++

Discussion in 'C++' started by j+e, Feb 18, 2009.

  1. j+e

    j+e New Member

    Joined:
    Feb 18, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi! I have just started programming, and I have a question for my assignment:

    When the input is ABCDE, i want the output to be EDCBA. But for some reason I cant understand he output i get is: EDCDE

    My code is:
    "
    for (int i = 0; i < b; i=i++)
    {
    char temp1 = a;
    a = a[(b-1)-i];
    a[(b-1)-i] = temp1;
    cout << a ;
    "

    b is the word...

    Please help me:) Thank you!
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Just loop it reverse and thats it.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > i=i++

    Um, no. Either use i++ on its own or use i=i+1.

    Try as a test displaying the values of i and (b-1)-i in the loop instead. What values do you expect? What values do you get? Does that explain why you get EDCDE?
     
  4. cpulocksmith

    cpulocksmith New Member

    Joined:
    Jul 23, 2008
    Messages:
    289
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    student
    Location:
    canada
    how does this grab ya?
    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
    char word[5]={'b','i','r','d','s'};
    char temp1[5];
    
    for (int i = 5; i > -1; i--)
    {
    char temp1 = word[i];
    word[i] = temp1;
    cout<<word[i];
    }
    cout<<endl;
    }
    
    hope it helps. i think this is what xpi0t0s was saying. you just turn the signs in the for loop, and make everything opposite.
    also i the for loop you put i=i++, i++ will work fine ^^
     
    Last edited: Feb 18, 2009
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Did you run that code cpulocksmith?
    What did word contain after it finished?
    What's the point of word = word; ?
     
  6. cpulocksmith

    cpulocksmith New Member

    Joined:
    Jul 23, 2008
    Messages:
    289
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    student
    Location:
    canada
    ha ha ha... woops...lol...
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    This would do nothing.

    char temp1 = word;
    word = temp1;
     
  8. Ryzer

    Ryzer New Member

    Joined:
    Feb 15, 2009
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Pipeline Maintenance
    Location:
    Saskatchewan, Canada
    Code:
    int size = 5;
    char value1[size] = {'a','b','c','d','e'};
    char value2[size];
    
    for(int i = 0; i < size; i++) {
      value2[i] = value1[i];
    }
    for(int j = size - 1; j > -1; j--) {
      cout << value[j];
    }
    don't have my compiler on this cpu so didnt test it but unless i'm missing something it should work :confused: correct me if i'm wrong
     
  9. j+e

    j+e New Member

    Joined:
    Feb 18, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Thank you very much Ryser! It worked :)
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You dont need value2 array as you are just printing the array from last.
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    OP:
    > When the input is ABCDE, i want the output to be EDCBA

    So do you need to reverse the string at all, or are you just outputting a string in reverse order?
    Did you try what I suggested earlier - displaying the loop variables?

    Ryzer's example won't work because value[] doesn't exist. But what I don't understand about it is what value2[] is for. This seems to be an exact duplicate of value1[], not reversed or anything. The next loop then displays value2[] in reverse order. So why doesn't it just display value1[] in reverse order?
     
  12. vsachar

    vsachar New Member

    Joined:
    Feb 19, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    this just prints the string s in reverse.


    Code: C++

    cout<<"enter size of string:";
    cin>>n;
    cout<<"enter string:";
    cin>>s;
    for (int i = 0; i < n; i++)
    {
    cout<<s[n-i-1];
    }



    if you meant that the variable b is the string i don't think you should use it in the loop.
    hope this helped
     
  13. vsachar

    vsachar New Member

    Joined:
    Feb 19, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    sorry bout the paragraph tags
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Removed for you.
     
  15. j+e

    j+e New Member

    Joined:
    Feb 18, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I have already defined a function that counts how many characters, that is b. So what I should do is to take this input (regardless of length) and reverse the characters. Rysers example work just if you wrote the input in main.

    So how can I use what I already have? I feel that I am really close, just that I get EDCDE instead of EDCBA:

    void reverse(char a[], int b)
    {
    for (int i = 0; i < 5; i=i++)
    {

    char temp1 = a;
    a= a[(b-1)-i];
    a[(b-1)-i] = temp1;
     
  16. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well, what about what I suggested? Did you display the values of i and (b-1)-i, and did they match what you expected?

    Of course I could just debug it for you and give you the answer but you learn nothing that way. This way you learn to debug, and since 90% of programming is debugging this is by far the much greater skill. Any code monkey can knock out code but it takes skill to debug.

    I see in fact you've completely ignored my previous post because you've still got "i=i++". Perhaps you think I don't know what I'm talking about (hint: eyes left, see what it says under my nick. Maybe I do...)
     
  17. vsachar

    vsachar New Member

    Joined:
    Feb 19, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    What you are doing gives this result if you print temp1 and a in that loop

    temp1: abcba
    a: ebcbe

    You are in no way reversing an array.

    Next time, please try to write down in simple english what you want to do (meaning an algorithm) and then try again. Do not simply try to use bits and pieces from everywhere such as exchange used in sorting. No matter, here's a better way:


    Code: C++
    </p>
    <p> a='\0';</p>
    <p> for (int i = 0; i < b; i++)</p>
    <p> {</p>
    <p> temp1 = a[b-i-1];</p>
    <p> cout<<temp1;</p>
    <p> }</p>
    <p>
     
  18. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    vsachar, Please try to keep HTML out when you paste codes
     
  19. j+e

    j+e New Member

    Joined:
    Feb 18, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    To xpi0t0s:

    I tried to do what you said. I printed out i and (b-1-i), what I got was

    i = 0, b-1-i = 4
    i = 1, b-1-i = 3
    i = 2, b-1-i = 2
    i = 3, b-1-i = 1
    i = 4, b-1-i = 0

    And that is what I want, because this I suppose would reverse all the letters. So I don't understand what I am doing wrong.

    My teacher says that i++ is the same as i = i+1, therefore I didn't change.

    But I have tried and tried now, and I don't see what is wrong, so maybe you could give me another hint?

    just to correct myself om my last reply: for (int i = 0; i < b; i++)
     
  20. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yes, i++ is the same as i=i+1, but it's not the same as i=i++, which is what you had.

    Try changing the last line to this, see if you can spot what's happening.
    cout << a << endl << a << endl;
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice