News:

MFG's Character of the Month: October 2026 Voting

Main Menu

Modulo calculation in C

Started by He-Man, October 06, 2007, 12:01:49 PM

Previous topic - Next topic

He-Man

#20
Quote from: [E]arly Morning Poison on October 07, 2007, 04:09:39 PM
you want to check how your compiler stores int32's and no info was giving whether or not they are signed or unsigned.. this sounds a bit like our assembly code homework, except that you will use C instead of assembly.

can you try to explain what are you supossed to do again, as well as the limitations ?

from what i can understand about the problem, you need to print an int 32 from memory as an ascii sequence of characters, that might be what you need the mod operator for as well, ascii characters start at ascii 48 for 0, so, if you want to print a number you need to do something like.

void printNumber(int32 number)
{
  for(i = 1 ; i < N ; i*=10){
    putc( (number % (i*10) ) - (number % i) + 48)
  }
}

Yes! That's exactly what I need to do. (I figured it out later. LoL) BUT - I have to find the way to print the correct values. It prints mainly wierd characters. And I have to input so that it prints numbers. Man - this is complicated. XD   


NEWS:
Yaaargh! I almost solved it!! :D I have 2 working digits! The third one won't work well. I dunno why, though. I was wondering if you could help me with that. Plz? Here's my NEW code! XD


int main()
{

sendNumber(425, 3);

}




void
sendNumber(tU32 value, tU8 noOfDigits)
{   

//IT'S WORKING!! HALLELUJA!!!!!!
tU32 h=100;
tU32 x=0;
tU32 i;
tU32 r=0;
for(i=0; i < noOfDigits; i++)
{
   
   x = (((value/h)-(i*h))%h)+48;
   U0THR=x;
   if (r=0)
   {
   x=10;
   r=1;
   }
   x=x*10;
   h=h/10;
}

}


It prints 420. No matter if I put 423 or 429 - it prints 420. What is the problem and how can I solve it? Help lpz! Sooo clooooose....! :D
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.

DemonicDrPhil

it wants to smoke some pot give it a hit and it'll be fine

sorry 420 joke couldnt resist.

I got a Bachelor's degree in Computer science and I never had to do any of this stuff and my school has the longest accreditation record in the state by ABET and i dunno maybe its the country or the school.  but good luck with it.

Teachers suck I should know I am one for at least this semester  ;P
Latest Yoshi alpha ready for feedback Aug 15th check here for details...

Valodim

dude, your code looks horrible. if you just rewrote it more cleanly that would probably already do the trick :blank:

some hints:

  • comment your code
  • encapsulate functionality! this is one of the main things you need to learn to be a good coder (and it's THE concept of OOP). in this case:
  • make one function which simply outputs a number and nothing else -- void sendNumber(U32 number);
  • make another function, which makes use of the first one but with the noOfDigits parameter already applied --sendNumber(U32 number, tU8 noOfDigits)
  • check your maths
  • TOTALLY comment your code

He-Man

#23
Quote from: Valodim on October 08, 2007, 03:32:26 AM
...getting there...

I still can't get it right.. :(    :-\

PS: LoL - we sent mails to each other at exactly the same time.  :sugoi:
Ok. I'll do so as you said. I'll comment and make 2 different functions and try make it work.
Yeah - I bet it looks horrible. It reflects the current situation of my hair too. LoL
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.

Valodim

Code must reflect your soul, not your hair.

MTFBWY.

He-Man

Quote from: Valodim on October 09, 2007, 03:13:55 PM
Code must reflect your soul, not your hair.

MTFBWY.

I guess. What's MTFBWY anyway?
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.

Valodim

MTFBWY

acronym tag is totally underused.

He-Man

Hmm - maybe the problem lies in my knowledge or the way how I interpret what I read.
As far as I read from Wikipedia - the calculations for modulo are
made with %.

So -  a % b - should be-  a modulo b.
QuoteQUOTE WIKIPEDIA:
Some calculators have a mod() function button, and many programming languages have a mod() function or similar, expressed as mod(a,n), for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as

a % n
or

a mod n

So - IF I understood it correctly, I should write something like

x = (value%10)+48; //where x is the printed value

Because you have 10 numbers - 0 to 9. And if I use modulo 10, it would give me the remainder of the
value + 48. Which SHOULD give me the correct position in the ASCII table.
BUT - I haven't been able to print out the desired values just YET.

I have a strange feeling that I am also missing out something more...
Anyway. Tell me - is it getting hotter?  8)
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.

Valodim

let's see....

Quote from: Valodim on October 09, 2007, 02:41:53 PM

  • comment your code
  • encapsulate functionality! this is one of the main things you need to learn to be a good coder (and it's THE concept of OOP). in this case:
  • make one function which simply outputs a number and nothing else -- void sendNumber(U32 number);
  • make another function, which makes use of the first one but with the noOfDigits parameter already applied --sendNumber(U32 number, tU8 noOfDigits)
  • check your maths
  • TOTALLY comment your code

one scratched, four to go. top to bottom.

He-Man

Quote from: Valodim on October 09, 2007, 05:30:30 PM
let's see....

Quote from: Valodim on October 09, 2007, 02:41:53 PM

  • comment your code
  • encapsulate functionality! this is one of the main things you need to learn to be a good coder (and it's THE concept of OOP). in this case:
  • make one function which simply outputs a number and nothing else -- void sendNumber(U32 number);
  • make another function, which makes use of the first one but with the noOfDigits parameter already applied --sendNumber(U32 number, tU8 noOfDigits)
  • check your maths
  • TOTALLY comment your code

one scratched, four to go. top to bottom.

I checked my code with the teacher yesterday - this time, I bored him with my questions so much that in the end - we compared our codes. So I looked his answer.
The thing is - our code looked almost EXACTLY the same! My formula was identical to that of the teacher. I apparently had a small bug in the rest of the program
which I won't focus on at the moment. I am happy and satisfied that I solved the formula - with correct thinking.

Dude - I will remember what you have said, and next time I ask for help with coding, I will make sure I write it the way you said. :)
Thanks a lot. Tomorrow is my exam - so people - wish me luck!!  :sugoi:
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.

He-Man

Quote from: DemonicDrPhil on October 09, 2007, 06:02:05 AM
it wants to smoke some pot give it a hit and it'll be fine

sorry 420 joke couldnt resist.

I got a Bachelor's degree in Computer science and I never had to do any of this stuff and my school has the longest accreditation record in the state by ABET and i dunno maybe its the country or the school.  but good luck with it.

Teachers suck I should know I am one for at least this semester  ;P

LoL.

Lucky you damnit!! Just because you're Dr Phil, you needn't do it... :P
Thanks mate. :)
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.

Valodim


Valodim


He-Man

Quote from: Valodim on October 19, 2007, 03:47:53 PM
...so... how did it go?

Heya! Sorry for the late reply. I passed it!! :D Wooo!  :sugoi:
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.

Valodim