![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#12 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
#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! |
|
|
|
|
|
#13 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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:
__________________
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 |
|
|
|
|
|
|
#14 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#15 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#16 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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:
__________________
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 |
|
|
|
|
|
|
#17 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Quote:
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) ); |
|
|
|
|
|
|
#18 |
|
Newbie
Join Date: Feb 2006
Posts: 18
Rep Power: 0
![]() |
thanks da wei. the code was great.
|
|
|
|
|
|
#19 |
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
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();
} |
|
|
|
|
|
#20 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|