-
why won't this work??
i'm trying to have this thing reaarange the numbers in a string into descending order and then print out the new number , but something's the matter and i don't know what... help!! :confused:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <iomanip>
#include <string>
void ascendingdigit(int initialdigit [], int n);
int main()
{
char initialdigit [5];
bool done = false;
cout<<"Enter a non-zero 4 digit number..."<<endl;
cin>>initialdigit;
do
{
void ascendingdigit(int initialdigit [], int n);
}
while (!done);
system("PAUSE");
return 0;
}
void ascendingdigit(int initialdigit[], int n)
{
bool exchanges;
int temp;
int i;
do {
exchanges = false;
for (i=0; i<initialdigit [0] -1; i++)
{
if (initialdigit[i] > initialdigit[i+1])
{
temp = initialdigit[i];
initialdigit[i] = initialdigit[i+1];
initialdigit[i+1] = temp;
exchanges = true;
cout<<temp<<endl;
}
}
} while (exchanges);
}
-
Code:
do {
// void ascendingdigit(int initialdigit [], int n);
ascendingdigit( initialdigit, n ) // !!Warning, n is not initialized!!
}
while (!done);
-
this didn't work, i'm lost... i hate C++, lol... i need someone to look at this code and help me figure out why it won't work...
-
My guess is that you are trying to make a Kaprekar's number program where you have to rearrange the digits of a 4 digit number in least to greatest and greatest to least order, and then subtract them. My suggestion would be to make two functions: one to isolate a given digit of a given number( digit 0 = ones place ), and one to reconstruct an integer value from a vector of digits. Put your number into a vector with four elements, one for each digit, using that function. This way, the sorting becomes very easy.
Code:
#include <vector>
#include <algorithm>
using namespace std;
//...
vector<int> foo; //vector that holds your digits
//To sort from greatest to least
sort( foo.begin(), foo.end() );
//Then call your function to construct your greatest-least number
//And from least to greatest, use reverse iterators
sort( foo.rbegin(), foo.rend() );
//And call the function again to get your least-greatest number from it
If you're not familiar with the STL or standard algorithms, I suggest you learn to use them because they are obviously very handy.
-
i'm not allowed to use those commands yet for the class that i'm in, it would be nice if i could though. i have to use the code that i posted in the first post. if anyone could help me figure out why the code i posted isnt working would be awesome, plus this assignment is due tomorrow! and i've been trying to figure it out for over a week now... HELP!!!
-
Look my first post above. Did you get it? If not, *ask*
Also could you tell me what this loop is supposed to do?
Code:
for ( i = 0; i < initialdigit [ 0 ] - 1; i++ )
{
......
}