19 Replies - 348 Views - Last Post: 30 May 2012 - 11:27 AM
#1
Get number from pattern (a,b)?
Posted 30 May 2012 - 01:37 AM
Example:
---------------------------
User input is: (2,3)
Int a = 2
Int b = 3
---------------------------
Is it possible???
Replies To: Get number from pattern (a,b)?
#2
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 01:42 AM
1. Take the input as string and then get the required components.
2. Use a simpler method
int a , b ; char c ; cin >> c >> a >> c >> b >> c ; cout << endl << a << endl << b ;
cin will do all the work for you, while you just have to sit back and enjoy.
#3
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 01:54 AM
1. im using C Not C++.
2. cant use any Loops or existing Functions.
3. i can use only recursion.
Im thinking if i will gets(somestring); and send it to a function that check if it is good (no problem doin that) but then how can i get the numbers, they are not integer they are Chars...
#4
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 02:01 AM
int a , b ;
char c ;
scanf("%c %d %c %d %c",&c,&a,&c,&b,&c);
printf("\nThe numbers are %d and %d\n",a,B)/>;
Are you not even allowed to use scanf and printf ??!!??!! Recursion will make the program unnecessarily complicated.
This post has been edited by aresh: 30 May 2012 - 02:02 AM
#5
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 02:14 AM
didnt think about that!
nice thank u!
#6
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 06:57 AM
char *userInput = "(2,3)";
int a, b ;
sscanf(userInput, "(%d,%d)", &a, &b );
printf("User input is: %s\nInt a = %d\nInt b = %d\n", userInput, a, b );
#7
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 07:09 AM
(2,3), i need to check if the pattern is right like:
( next a next , next b next )?????
#8
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 07:48 AM
tomas7470, on 30 May 2012 - 04:54 AM, said:
3. i can use only recursion.
Since the point of the assignment seems to be to learn recursion, using scanf or ssanf will not meet that requirement.
tomas7470, on 30 May 2012 - 04:54 AM, said:
If a and be are limited to single digits it's easy -- all that requires is one simple arithmetic operation based on the ascii values of the chars.
If they can be multi-digit numbers it's a bit more complicated but you can design your function to convert the chars to int one decimal place at a time (this can be done recursively) or you can use the stdlib function atoi if your assignment allows this.
#9
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 08:18 AM
#10
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 08:29 AM
(1) it is only 1 byte, whereas an int is generally 4 bytes (depending on your computer and OS, but 4 is most common these days).
(2) and therefore a char is limited to values in the range -128 to 127.
Look at the ascii chart and you will see that each char has a decimal number associated with it. That's the numerical value that 'represents' that character. So for example, the digit '1' has the value 49. So if you subtract 48 from the char '1', the result is the integer 1.
Or to make it even simpler, you can subtract the char '0' from the char '1' and the result is the integer 1.
#11
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 09:00 AM
thank u
#12
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 09:06 AM
So the number 48 is understood by your program to "mean" the character '0' when you tell it to interpret it as a char.
And the number 49 is understood by your program to "mean" the character '1' when you tell it to interpret it as a char.
And so on ...
The ascii chart tells you the numerical values that are used under the ascii system to represent the various printed (and control) characters.
#13
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 09:39 AM
tomas7470, on 30 May 2012 - 04:54 AM, said:
2. cant use any Loops or existing Functions.
3. i can use only recursion.
Yeah, didn't see that last one.
Step one, print a string using recursion. That will at least give you an idea of what you're dealing with. Your sig should looks something like:
void showStr(char *);
Know that every c-string ends in 0, so you stop calling yourself when you hit it.
If you can write that, then you're halfway there. Or one quarter, if your numbers can be greater than one digit. Still, it's a start.
Good luck.
#14
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 09:46 AM
#15
Re: Get number from pattern (a,b)?
Posted 30 May 2012 - 10:24 AM
printf("%d", a-48);
and it will give me: 2..
??
|
|

New Topic/Question
Reply




MultiQuote





|