News:

MFG's Character of the Month: October 2026 Nominations

Main Menu

Modulo calculation in C

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

Previous topic - Next topic

He-Man

Hey guys,

I have a question about programming in C.
I am trying to make a function, which takes two input parameters,
one for a 32-bit integer, and the second as amount of digits.

And when I call the method, it has to return the modulo calculation of them.
I am not really sure where to begin, nontheless, this method is very much
needed at the moment.

If any of you guys could help me, I'd be very grateful.

Best wishes!
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

you mean this?

http://en.wikipedia.org/wiki/Modular_arithmetic

I have no idea where you want to apply an "amount of digits" there.

If you mean the simple modulo operation as in rest of a division, try

int stuff(int a, int b) {
  return a%b;
}

though I guess you could think of that yourself so I'm not sure what you want here :P

He-Man

Quote from: Valodim on October 06, 2007, 01:45:09 PM
you mean this?

http://en.wikipedia.org/wiki/Modular_arithmetic

I have no idea where you want to apply an "amount of digits" there.

If you mean the simple modulo operation as in rest of a division, try

int stuff(int a, int b) {
  return a%b;
}

though I guess you could think of that yourself so I'm not sure what you want here :P

Yeah man, I am a bit confused by the task set before me as well. I am working on an LPC2000 Education Board. I am supposed to make a 32-bit integer show up on the prompt, with digits setting as modulo. But here's my code I have to work on. I have to re-use the printingArray method I have made, and instead of
characters, print the 32-bit integer. I hope I have explained well - I am not good at that.  :P
Anyway - this's a real annoyance at the moment, and hope I can get some help with it. Oh and btw, I don't know that much about C programming. Only JAVA a bit. So I don't know much about their differences.


*****************************************************************************
*
* Description:
* Initializes the UART channel #0 (38.4 kbps, 8N1)
*
****************************************************************************/
#include <lpc2xxx.h>
#include <general.h>
#include <printf_P.h>


//void printingArray(tU8 stringToSend); <-- used in previous lab to print out strings
void sendNumber(tU32 value, tU8 noOfDigits);

int main()
{

//Call of sendNumber method. An integer 300, to be calculated with modulo 3. The result should be printed.
sendNumber(300, 3);

//printingArray("Hello!"); <--- The printing of the string in the previous lab, using mah own method! :D
//printf("end"); <--- testing the printf method. Not allowed to used in labs. :(

}



void
initUart0(void)
{
//enable uart #0 pins (P0.0 = TxD0, P0.1 = RxD0)
PINSEL0 &= 0xFFFFFFF0;

//initialize bitrate (by first enable DL registers, DLAB-bit = 1)
U0LCR = 0x80;//The U0LCR determines the format of the data character that is to be transmitted or received.
            //In this case - we will have a 7 bit character length.

U0DLL = 0x0062; //least significant part of divisor
U0DLM = 0x0000; //most significant part of divisor
U0LCR = 0x00;

//initialize LCR: 8N1 - 8N1 = 8 char length, No-parity check, 1 stop bit.
U0LCR = 0x80; /*Chosen 7 bit char length. Bit 2 controls stop bits (here is set for 1 stop bit).
              *And bit 3 enables/disables parity check. Here, it is disabled.*/   

//disable FIFO
U0FCR = 0x00;

//clear interrupt bits, just in case (not used)
U0IER = 0x00;
}

/*****************************************************************************
*
* Description:
* Blocking output routine, i.e., the routine waits until the uart
* buffer is free and then sends the character.
*
* Params:
* [in] charToSend - The character to send/print | No longer used. Replaced with printingArray. By me! :D
*
****************************************************************************/
//Here's the printingArray method used in the previous lab
/*void
printingArray(tU8 stringToSend)
{
int i = 0;
//Wait until THR register is empty
while( U0LSR & 0x10 ); //Think bitwise


char c_array[] = "Hello!";
for (i = 0; i < 6 ; i++) {
   stringToSend = c_array;
   U0THR = stringToSend;
      }
   }
*/

   
/***************************************************************
*
* Description: Blocking function that sends a number (32-bit
* integer) to UART channel #0.
*
* Parameters: value – 32-bit unsigned value to be printed
* noOfDigits – number of digits to actaully print
* (valid input: 1-10)
*
***************************************************************/
int
sendNumber(tU32 value, tU8 noOfDigits)
{   

int value;
int noOfDigits;
int sum;

sum = value % noOfDigits;
return sum;

//PROBLEMS!! Gaaaargh!!! 
//1.) Find register to use for 32-bit integer
//2.) Find a way to calculate modulo
//3.) Execute teh code
}
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.

aokmaniac13

Depending on how the board handles arithmetic this may or may not work.

After dividing take the decimal portion of the quotient and multiply the divisor back into it.

Valodim

#4
urza: actually, since even printf works, I suppose the compiler provides enough of C to have all arithmetic operators working :P

* Parameters: value – 32-bit unsigned value to be printed
* noOfDigits – number of digits to actaully print
* (valid input: 1-10)

that makes sense. if noOfDigits is 3, it wants you to print only the first three digits of the integer

sendNumber(34252, 3) // should output 252.

achieving that is pretty easy actually. need hint or can you do that yourself?

He-Man

Quote from: Valodim on October 06, 2007, 06:59:54 PM
urza: actually, since even printf works, I suppose the compiler provides enough of C to have all arithmetic operators working :P

* Parameters: value – 32-bit unsigned value to be printed
* noOfDigits – number of digits to actaully print
* (valid input: 1-10)

that makes sense. if noOfDigits is 3, it wants you to print only the first three digits of the integer

sendNumber(34252, 3) // should output 252.

achieving that is pretty easy actually. need hint or can you do that yourself?

Hmmm - you  could give me a hint. Since I don't know really where to start or what register to use for it. :)
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.

Messatsu

The 32 int should be easy.  If you can't do that one, drop it. :P

The digits one, you just need to divide each one, left-to-right, and store the remainder.  I'm assuming digits part of the problem is a number containing potentially and infinite number of digits.

300%4...

3/4 = 0, rem 3 (must reuse 3 since undivisible)
30/4 = 7 rem 2
20/4 = 5 rem 0
return rem

Quote<--- testing the printf method. Not allowed to used in labs.
huh? ???

Quote from: Bastard Wolf on May 12, 2008, 05:02:05 PM
Many people risk their lives everyday by having Mugen.

Renzo F

Some teachers prohibit to use certain libraries or functions. string.h was banned in my college :P

Messatsu

But prohibiting printf is so incredibly inconvenient.

Quote from: Bastard Wolf on May 12, 2008, 05:02:05 PM
Many people risk their lives everyday by having Mugen.

Renzo F

I remember they also prohibited printf to me when we were learning about function pointers because they wanted us to implement our own print function.

Argh, just remembering stuff like working with the heap makes me want to puke.

Cybaster

Quote from: Dung Tzu LAN on October 07, 2007, 12:17:14 AM
But prohibiting printf is so incredibly inconvenient.
Yeah, using 500 putchar is the fun. 8)

Valodim


He-Man

Quote from: Renzo [a.k.a. Shin Orochi Sachavaca] on October 07, 2007, 12:42:15 AM
I remember they also prohibited printf to me when we were learning about function pointers because they wanted us to implement our own print function.

Argh, just remembering stuff like working with the heap makes me want to puke.

The same here! But I don't know how it was in your place - here, the teacher's are incompetent as hell! I mean - whatever you ask for, they have no clue. Leaving you completely on your own! The school's administrator, or whatever you called it, actually came to our class and asked us for forgiveness. I will never forget that one. One of the teachers in HTML, he attended to our class and lectured in HTML. So he showed up some code - later, he didn't understand it. So we waited for like 15 min, before one guy in the class told him how to do the task. And the teacher was like: "Ohhhh? I didn't know you could do that." And my programing teacher - he refused to help me at all - and finished 4 hour lectures in 1.5 hours. Also - last school period - we were supposed to read about operative systems. A book of MAAAANY pages - teacher told us we will have exam within 3 weeks!! I was like: wtf?! So I asked the teacher about how good my knowledge would be in operative systems, if all I read about them is in 3 weeks. He said: I don't know. So, I have no choice but to go and learn elsewhere on the net!  :(
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

switch schools man

either that or u will end up blowing the place up and getting sent to jail and then the electric chair
Latest Yoshi alpha ready for feedback Aug 15th check here for details...

He-Man

Quote from: DemonicDrPhil on October 07, 2007, 11:26:19 AM
switch schools man

either that or u will end up blowing the place up and getting sent to jail and then the electric chair

LoL.

Nah, I wouldn't do that. :)  (Though I thought about it a couple of times. :P) LoL.
The thing is - I can't do that either. This is my last year, and if I had to choose another school,
I would had to travel many miles for that. Or even to another country. Concidering my economic
situation, I couldn't afford doing something like that. But I have been thinking about it too... really.
Many, many times. :/
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.

Cybaster

Anyway, seeing what you said, I guess you'll just have to work at home alone with the help of internet and books. Bashing teachers is always fun, I love doing that in mathematics when the teacher is somehow incompetent from time to time. :)

You're in school to gain knowledge, but later on, it's experience that will be preponderent. Get your diploma and move on.

He-Man

Quote from: Demitri Maximoff on October 07, 2007, 11:41:08 AM
Anyway, seeing what you said, I guess you'll just have to work at home alone with the help of internet and books. Bashing teachers is always fun, I love doing that in mathematics when the teacher is somehow incompetent from time to time. :)

You're in school to gain knowledge, but later on, it's experience that will be preponderent. Get your diploma and move on.

Exactly. I completely agree. And that is how I do too. Only if I really don't understand something, or if the time is of the essence (like now - I have 2 exams next week), then I ask for help.

Haha - yeah. :D Bashing teachers in their lectures and proving them wrong. That's fun.


Thanks :)
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: Valodim on October 07, 2007, 01:43:55 AM
value%(10^noOfDigits)

Hmm - I did something now. The board does print - but not numbers. It prints a comma sign, and a heart. I suspect it prints out character positions, instead of actual numbers - or the board could just be saying it loves me. LoL. Hmm. I am unsure about this. How do I solve this problem?  :S
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.

Bastard Mami

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)
  }
}

Valodim