Go Back  DVD Talk Forum > General Discussions > Tech Talk
Reload this Page >

Need help, please....math/computer science...general help

Tech Talk Discuss PC Hardware, Software, Internet and Other Technology

Need help, please....math/computer science...general help

Old 09-26-01, 01:08 PM
  #1  
DVD Talk Legend
Thread Starter
 
Join Date: May 2000
Posts: 16,666
Likes: 0
Received 0 Likes on 0 Posts
Need help, please....math/computer science...general help

So I have this program due tomorrow. It's really basic computer program written in C in which the user inputs 4-digit integers and we isolate the individual digits and perform operations wth them.

It's easy enough...the only challenge is how to isolate the individual digits.

WE ARE NOT ALLOWED TO USE SUBTRACTION.

I came up with two ways of isolating the numbers...the first involved subtraction.

The second way does not use subtraction but it involves dividing the 4-digit integers by multiples of 10 (1000, 100, 10, etc.) and then dividing the remaining digits by the first digit*10.

For example, if someone enters 8445. If I wanted to isolate the 4, I'd divide 8445 by 100 which would give me 84. Then I'd divide 84 by the first digit times 10 and get the remainder - 4.

Only problem is if the first digit is 0. (that is, if it's a three digit number)...you can't divide by 0 and the entire program gets messed up.

Please help me think of the easiest way to isolate the digits withotu using subtraction and without having to divide by 0.

I'm counting on the Otter Farm...this is due tomorrow, and I really, really need your help. Please don't just ignore me for the twikoffing and the Jules-bashing...that's fun, but this is really important for me now.

THANKS SO MUCH!
Old 09-26-01, 01:12 PM
  #2  
X
Administrator
 
X's Avatar
 
Join Date: Oct 1987
Location: AA-
Posts: 10,764
Likes: 0
Received 4 Likes on 3 Posts
You can do a couple of things. Do a log function to determine the number of digits or use "if" statements to check for the 0 or the size of the number.

Oh, and moving to the Computer forum.
Old 09-26-01, 01:17 PM
  #3  
DVD Talk Hall of Fame
 
Duran's Avatar
 
Join Date: Jul 1999
Location: Columbia, MD
Posts: 8,177
Likes: 0
Received 0 Likes on 0 Posts
Can't you just div and mod them?

first digit = (x)div1000
second digit = ((x)mod1000)div100
third digit = ((x)mod100)div10
fourth digit = (x)mod10

Where x is the original number.
Old 09-26-01, 01:23 PM
  #4  
DVD Talk Legend
Thread Starter
 
Join Date: May 2000
Posts: 16,666
Likes: 0
Received 0 Likes on 0 Posts
Originally posted by Duran
Can't you just div and mod them?

first digit = (x)div1000
second digit = ((x)mod1000)div100
third digit = ((x)mod100)div10
fourth digit = (x)mod10

Where x is the original number.
What is this "mod" you speak of?
Old 09-26-01, 01:33 PM
  #5  
X
Administrator
 
X's Avatar
 
Join Date: Oct 1987
Location: AA-
Posts: 10,764
Likes: 0
Received 4 Likes on 3 Posts
Mod gives you the remainder of the divide. Look it up.
Old 09-26-01, 01:38 PM
  #6  
DVD Talk Hall of Fame
 
Duran's Avatar
 
Join Date: Jul 1999
Location: Columbia, MD
Posts: 8,177
Likes: 0
Received 0 Likes on 0 Posts
Additional hint, because I'm feeling generous today - div is the integer division function.
Old 09-26-01, 01:59 PM
  #7  
DVD Talk Legend
Thread Starter
 
Join Date: May 2000
Posts: 16,666
Likes: 0
Received 0 Likes on 0 Posts
Originally posted by X
Mod gives you the remainder of the divide. Look it up.
Oh, I know mod. Yes I had an idea like that. I do think that will work. Thanks, Duran.

And thanks for describing 'div'.
Old 09-26-01, 02:19 PM
  #8  
DVD Talk Hall of Fame
 
Duran's Avatar
 
Join Date: Jul 1999
Location: Columbia, MD
Posts: 8,177
Likes: 0
Received 0 Likes on 0 Posts
No sweat. Bill is in the mail.
Old 09-26-01, 03:18 PM
  #9  
DVD Talk Hero
 
Join Date: Aug 2000
Location: Bartertown due to it having a better economy than where I really live, Buffalo NY
Posts: 29,706
Likes: 0
Received 0 Likes on 0 Posts
input it as a 4 item char[]
and if you need to use it as an int use
atoi()
Old 09-26-01, 03:56 PM
  #10  
DVD Talk Legend
Thread Starter
 
Join Date: May 2000
Posts: 16,666
Likes: 0
Received 0 Likes on 0 Posts
Originally posted by mikehunt
input it as a 4 item char[]
and if you need to use it as an int use
atoi()
*WAY* too advanced for someone who has had like four classes on C. Thanks anyway though. Duran & Duran helped me out. Thanks, Duran.

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Thread Tools
Search this Thread

Archive - Advertising - Cookie Policy - Privacy Statement - Terms of Service - Do Not Sell My Personal Information

Copyright 2018 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.