Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 18th, 2006, 11:36 AM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Well, he tried tags, but he put the opening tag BELOW the code and has no closing tag.
Put [/code] BELOW your code and [ code ] (no spaces before and after the word) ABOVE your code, woiks like this:
This chere is code
   with some indentations
   {
      and other blocked up stuff
   }
See?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 18th, 2006, 12:00 PM   #12
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int i,j;
	cout << "enter the array";
	char arr[10], rr1[10];

	for(i = 0; i < 10; i++)
		cin >> arr[i];
	
	for(i = 9; i >= 0; i--)
		for(j = 0; j <= 9; j++)
			rr1[j] = arr[i];

	for(j = 0; j <= 9; j++)
		cout << rr1[j];

	system("pause");
	
	return 0;
}

The problem with your code are the nested for-loops.

Instead of taking the last element of the source array and placing it as the first element of the new array, your iterating over an array for every iteration of an array.

Try this instead:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int i,j;
	cout << "enter an array of 10 elements:\n";
	int arr[10], rr1[10];

	for(i = 0; i < 10; i++)
	{
		cout << i + 1 << ": ";
		cin >> arr[i];
		cin.ignore();
	}
	
	for(i = 0, j = 9; i < 10; i++, j--)
		rr1[i] = arr[j];
	
	for(int x = 0; x < 10; x++)
		cout << rr1[x] << endl;

	cin.get();
	
	return 0;
}

Notice I changed the type to int instead of char. That way, you will be able to input a number rather then a single character.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 18th, 2006, 12:10 PM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There are any number of ways to reverse the elements of an array or string. If you're not interested in preserving the original (if you are, you can copy it, of course), you can put a pointer at the end and another at the beginning. Exchange the two values. Increment the beginning pointer and decrement the ending pointer. When the pointers cross, you're done. Another way is shown here. Note that I have used strings and not char arrays. You're using C++, you may as well write C++ instead of writing what is essentially C and compiling it with a C++ compiler. Strings are much easier to use, more intuitive, and the code is more robust with less effort.
#include <iostream>
#include <string>

using namespace std;

int uhOh (string TroubleInRiverCity)
{
    cerr << TroubleInRiverCity << endl;
    return 1;
}
int main (int argc, char *argv [])
{
    string theString;
    string theReverse;

    cout << "Enter a line of text to reverse: ";
    getline (cin, theString, '\n');
    if (!cin.good ()) return uhOh ("Input failed");
    size_t length = theString.size ();
    theReverse = theString; // To set the appropriate length
    for (size_t i = 0; i < length; i++)
        theReverse [length-i-1] = theString [i];

    cout << theString << " => " << theReverse << endl;
    cout << "Press ENTER to exit the program";
    cin.sync ();
    cin.get ();
    return 0;
}
Quote:
Originally Posted by Output
Enter a line of text to reverse: This is the text
This is the text => txet eht si sihT
Press ENTER to exit the program
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 18th, 2006, 12:14 PM   #14
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Ah! I thought he wanted to reverse an array! That would explain it. *slaps head*
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 18th, 2006, 2:16 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
One could reverse an array the same way: use a vector.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 18th, 2006, 3:00 PM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Example:
    vector <int> theThangy;
    vector <int> theReverse;
    int holdingVar;

    cout << "Enter a few integers to reverse.  Terminate with ctrl-Z on a new line." << endl;
    cout << "Go: ";
    while (1)
    {
        cin >> holdingVar;
        if (!cin.good ()) break;
        theThangy.push_back (holdingVar);
    }
    if (!cin.eof ()) return uhOh ("Input failed");
    size_t length = theThangy.size ();
    for (int i = length-1; i >= 0; i--) 
        theReverse.push_back (theThangy [i]);

    cout << "The Thangy: ";
    for (size_t i = 0; i < length; i++)
        cout << theThangy [i] << " ";
    cout << endl;

    cout << "The Reverse: ";
    for (size_t i = 0; i < length; i++)
        cout << theReverse [i] << " ";
    cout << endl;

    return 0;
Quote:
Originally Posted by Output
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\David>cd desktop

C:\Documents and Settings\David\Desktop>student
Enter a few integers to reverse. Terminate with ctrl-Z on a new line.
Go: 1 10 25 40
^Z
The Thangy: 1 10 25 40
The Reverse: 40 25 10 1

C:\Documents and Settings\David\Desktop>student
Enter a few integers to reverse. Terminate with ctrl-Z on a new line.
Go: 1 4 a georgie
Input failed

C:\Documents and Settings\David\Desktop>
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 18th, 2006, 4:36 PM   #17
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by DaWei
Example:
    size_t length = theThangy.size ();
    for (int i = length-1; i >= 0; i--) 
        theReverse.push_back (theThangy [i]);
Could also be done with the vector constructor:
std::vector<std::string> theReverse( theThangy.rbegin(), theThangy.rend() );

And here is a pretty neat way to output the contents of a vector if anyone is interested (requires '#include <iterator>'):
copy( theReverse.begin(), theReverse.end(), std::ostream_iterator<std::string>(std::cout) );
Cache is offline   Reply With Quote
Old Mar 19th, 2006, 7:27 AM   #18
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
thanks da wei. the code was great.
biohazard is offline   Reply With Quote
Old Mar 19th, 2006, 4:50 PM   #19
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
Why not use the standard library algorithm provided for this purpose?
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string s("tnorf-ot-kcab");
    reverse(s.begin(), s.end());
    cout << s;
    cin.get();
}
Bench is offline   Reply With Quote
Old Mar 19th, 2006, 6:04 PM   #20
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Think of it this way: is he learning to use utilities and tools, or learning to code? 'reverse' didn't grow in the cottonpatch, someone wrote it. The answer to your question lies in the answer to that question.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC