Get number from pattern (a,b)?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 646 Views - Last Post: 30 May 2012 - 11:27 AM Rate Topic: -----

#1 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Get number from pattern (a,b)?

Posted 30 May 2012 - 01:37 AM

The input should be (a,b) a and b are numbers! I should get those numbers from the pattern to an integer variable...
Example:
---------------------------
User input is: (2,3)
Int a = 2
Int b = 3
---------------------------
Is it possible???
Is This A Good Question/Topic? 0
  • +

Replies To: Get number from pattern (a,b)?

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 01:42 AM

Yup. You can go about it in two ways :
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.
Was This Post Helpful? 0
  • +
  • -

#3 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 01:54 AM

Thank for Reply:
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...
Was This Post Helpful? 0
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 02:01 AM

Actually, the code which I sent requires a little editing to make it a C code. Here is a C equivalent
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

Was This Post Helpful? 0
  • +
  • -

#5 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 02:14 AM

im not familiar with C++ syntex.
didnt think about that!
nice thank u!
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 06:57 AM

I'd avoid the dummy vars. This will work:
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 );


Was This Post Helpful? 0
  • +
  • -

#7 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 07:09 AM

the user input should be:
(2,3), i need to check if the pattern is right like:
( next a next , next b next )?????
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 07:48 AM

View Posttomas7470, on 30 May 2012 - 04:54 AM, said:

Thank for Reply:
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.


View Posttomas7470, on 30 May 2012 - 04:54 AM, said:

... but then how can i get the numbers, they are not integer they are Chars...

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.
Was This Post Helpful? 0
  • +
  • -

#9 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 08:18 AM

how can i convert char to int?
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 08:29 AM

A char is "like" an int, except that:
(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.
Was This Post Helpful? 0
  • +
  • -

#11 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 09:00 AM

i didnt get that part from char 48 to int 1...? :helpsmilie:
thank u
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 09:06 AM

You must realize that everything -- every kind of data -- is represented in the computer as numbers.

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.
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 09:39 AM

View Posttomas7470, on 30 May 2012 - 04:54 AM, said:

1. im using C Not C++.
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.
Was This Post Helpful? 0
  • +
  • -

#14 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 09:46 AM

As r.stiltskin said, all characters are stored as their ASCII values inside your computer. So, 'A' is stored as 65, 'a' is stored as 97, and similarly '1' (character 1, not integer 1) is stored as 49. So, to convert it to integer, subtract 48 from it (49-48=1) or subtract '0' (having ASCII value 48) from '1'.
Was This Post Helpful? 0
  • +
  • -

#15 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: Get number from pattern (a,b)?

Posted 30 May 2012 - 10:24 AM

so if i want that a for example will be 2 i need to do:
printf("%d", a-48);
and it will give me: 2..
??
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2