954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Confused by Passing by Address and Arrays

I'm trying to make a Palindrome program, that basically returns whatever you enter, just backwards. For some reason, the cNewArray is not only couting the original array backwards, but also the original array. For example if I enter "Hello" it returns "Hello = olleHHello". I'm very new to referencing pointers and addressing and all that stuff, and I think that might be what's causing it. Can someone help?

#include <iostream>



int Absolute ( int nOrigNum )
{
    int nAbsolute = 0;
    if ( nOrigNum < 0 )
    {
        while ( nOrigNum < 0 )
        {
            nOrigNum++;
            nAbsolute++;
        }
        return nAbsolute;
    }
    else
    {
        return nOrigNum;
    }
}

int PosSwitch ( int nOrigPos )
{
    int nTempPos = nOrigPos - 10;
    nTempPos++;
    int nNewPos = Absolute ( nTempPos );

    return nNewPos;
}



void SwitchPrint ( char *cOrigArray, char *cNewArray )
{

    for ( int x = 0; x < 10; x++ )
    {
        if (!( ( cOrigArray [ x ] >= 65 && cOrigArray [ x ] <= 90 ) || ( cOrigArray [ x ] >= 97 && cOrigArray [ x ] <= 122 )  ))
        {
            cOrigArray [ x ] = ' ';
        }
    }


    for ( int i = 0; i < 10; i++ )
    {
        int j = PosSwitch ( i );
        if ( ( cOrigArray [ i ] >= 65 && cOrigArray [ i ] <= 90 ) || ( cOrigArray [ i ] >= 97 && cOrigArray [ i ] <= 122 )  )
        {
            cNewArray [ j ] = cOrigArray [ i ];
        }
        else
        {
            cNewArray [ j ] = ' ';
        }
    }


    std::cout << cOrigArray << " = " << cNewArray;
    std::cout << "\n" << "\n";

}


int main ( int argc, char** argv )
{
    char PalindromeArrayOrig [ 10 ];
    std::cout << "Enter a word, 10 letters or less." << "\n";

    std::cin >> PalindromeArrayOrig;

    char PalindromeArrayNew [ 10 ];
    SwitchPrint( PalindromeArrayOrig, PalindromeArrayNew );
    return 0;
}
zck17
Newbie Poster
8 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Is there a reason you are not permitted to use strings and a simple loop?

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
 

havent really learned strings.. just the c style ones in arrays. and what kind of loop are you talking?

zck17
Newbie Poster
8 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Although your code needs a lot of improvement a simple way to work around this is to end the cNewArray string with the escaped zero character:

...
...
...
int endPos = 0;
for ( int i = 0; i < 10; i++ )
    {
        int j = PosSwitch ( i );
        if ( ( cOrigArray [ i ] >= 65 && cOrigArray [ i ] <= 90 ) || ( cOrigArray [ i ] >= 97 && cOrigArray [ i ] <= 122 )  )
        {
            cNewArray [ j ] = cOrigArray [ i ];
        }
        else
        {
            cNewArray [ j ] = ' ';
        }
        endPos = i;
    }
<strong>cNewArray[endPos + 1] = '\0';</strong>
...
...
...


I would also suggest you prompt the user enter 9 letters or less (you have to have in mind the terminal '\0' character), or you could increase the size of each array to 11. :)

mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
 

Thank you so much! and what could I improve on? I've kind of gotten to the point where I understand the concepts of the basics and don't know where to go from here.. I got to pointers and references, and although I've read over it 300 times, I just don't know when to use them, or for that matter how. Any tips?

zck17
Newbie Poster
8 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Well one sure hint for you is try to think less complex. Things that can get done the simple way, must be done so. You always have to examine your code and ask yourself: "Is there a more simple, cleaner and more direct way to do this?" If so, do it.
Look for example your Absolute function. It can get done as simple as:

unsigned int Absolute(int n) {
   if(n<0) 
      n *= (-1);
   return n;
}
mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
 

I've noticed I'm terrible at that so far. My friend from school who knows it simplifies my code ALL the time. And thank you for all feedback!

zck17
Newbie Poster
8 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

No problem :)
Don't worry, all you need is some experience, you're on the right way.

mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: