i wanna show a descending order of a variable not a array element..

Jan 22, 2013 at 4:01pm
i wanna show a descending order of a variable not a array element..like i wanna input 523 that shows me output like 325,,,,,,can anyone help me...
Jan 22, 2013 at 4:12pm
I can help you, but first help me see what you have tried / done / thought
Last edited on Jan 22, 2013 at 4:12pm
Jan 22, 2013 at 4:12pm
Maybe if someone will understand what you wrote then he help you.:)
Jan 22, 2013 at 4:15pm
@Santosh Reddy
I can help you, but first help me see what you have tried / done / thought


I think he is speaking about outputing an integer number in the reverse order of its digits.:)
Last edited on Jan 22, 2013 at 4:16pm
Jan 22, 2013 at 4:21pm
well you could do like an array/list/vector/stack that stores every digit. so when you input "523" as a string you can cut it up to "5", "2" and "3".

something like

stack<int> intStack;

then u pass into a function by refernce

1
2
3
4
5
6
7
8
9
void tokenise (string str, stack<int> &intStack) {
	stringstream token(str);     //a standard stringstream which parses 'str'
	string temp;                 //a temporary string

	while (token >> temp){    // while the string has not been completely copied
		int number = convertToInt(checking); // assign "number" a value by converting "checking" into an int
		intStack.push(number); // store "number" into the stack
	}
};
Jan 22, 2013 at 4:28pm
sorry i meant

1
2
3
4
5
6
7
8
9
10
void tokenise (string str, stack<int> &intStack) {
	stringstream token(str);     //a standard stringstream which parses 'str'
	string temp;                 //a temporary string
        int number

	while (token >> temp){    // while the string has not been completely copied
		number = convertToInt(temp); // assign "number" a value by converting "checking" into an int
		intStack.push(number); // store "number" into the stack
	}
};


"checking" in int number = convertToInt(checking); refers to the string in which user has typed
Jan 22, 2013 at 4:40pm
1
2
3
4
5
6
void print_reverse( unsigned int n )
{
    std::cout << n%10 ;
    if( n < 10 ) std::cout << '\n' ;
    else print_reverse( n/10 ) ;
}
Jan 22, 2013 at 5:31pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <functional>


int main()
{
	std::function<void( unsigned int )> reverse = [&]( unsigned int x ) 
	{ 
		std::cout << x % 10;
		( x /= 10 ) ? reverse( x ) : void( std::cout << std::endl );
	};


	while ( unsigned int x = ( std::cout << "\nEnter a non-negative number (0 - exit): ",
	                           std::cin >> x, x ) )
	{
		reverse( x );
	}

	return 0;
}


EDIT: The code was updated according to the remark of JLBorges
Last edited on Jan 22, 2013 at 7:04pm
Jan 22, 2013 at 6:00pm
1
2
3
4
5
6
std::function<void( unsigned int )> reverse = [&]( unsigned int x ) 
{ 
    std::cout << x % 10;
    // *** ( x /= 10 ) ? reverse( x ) : std::cout << std::endl;
    ( x /= 10 ) ? void( reverse( x ) ) : void( std::cout << std::endl ) ;
};
Jan 22, 2013 at 6:09pm
@JLBorges


I think that there is no need to casting the expressions to void.

In old C code I saw such constructions

( void )func();

But it is better to write simply

func();

EDIT: Ah, I am sorry. I think that if one of the expressions is of type void then the other expression in the conditional operator can be of any type.

Take into account that the call of reverse already has type void.
Last edited on Jan 22, 2013 at 6:14pm
Jan 22, 2013 at 6:26pm

@JLBorges


It seems that you are right. The third operand shall be casted to void. According to the C++ Standard

2 If either the second or the third operand has type void, then the lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed on the second and third operands, and one of the following shall hold:
— The second or the third operand (but not both) is a throw-expression (15.1); the result is of the type of the other and is a prvalue.
— Both the second and the third operands have type void; the result is of type void and is a prvalue.
[ Note: This includes the case where both operands are throw-expressions. —end note ]


So the expression shall be written as

( x /= 10 ) ? reverse( x ) : void( std::cout << std::endl ) ;

Thanks.
Last edited on Jan 22, 2013 at 6:36pm
Jan 22, 2013 at 6:37pm
> if one of the expressions is of type void then
> the other expression in the conditional operator can be of any type.

No.

It can either be an expression of of type void, or it can be a throw-expression.
http://liveworkspace.org/code/2WBB3q$2
Last edited on Jan 22, 2013 at 6:38pm
Jan 22, 2013 at 6:41pm
Thanks JLBorges. I have already realized this.
Topic archived. No new replies allowed.