1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Reverse array in C++

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

  1. j+e

    j+e New Member

    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

    Just loop it reverse and thats it.
  3. xpi0t0s

    xpi0t0s Mentor

    > 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

    how does this grab ya?
    #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;
    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

    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

    ha ha ha... woops...lol...
  7. shabbir

    shabbir Administrator Staff Member

    This would do nothing.

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

    Ryzer New Member

    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

    Thank you very much Ryser! It worked :)
  10. shabbir

    shabbir Administrator Staff Member

    You dont need value2 array as you are just printing the array from last.
  11. xpi0t0s

    xpi0t0s Mentor

    > 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

    this just prints the string s in reverse.

    Code: C++

    cout<<"enter size of string:";
    cout<<"enter string:";
    for (int i = 0; i < n; i++)

    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

    sorry bout the paragraph tags
  14. shabbir

    shabbir Administrator Staff Member

    Removed for you.
  15. j+e

    j+e New Member

    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

    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

    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> 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>
  18. shabbir

    shabbir Administrator Staff Member

    vsachar, Please try to keep HTML out when you paste codes
  19. j+e

    j+e New Member

    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

    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