Thread: Division in C programming. plz help

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    Unhappy Division in C programming. plz help

    Hi guys . i was working on given below question. and i try to solve it. can any one confirm it that is this correct solution for that question? if not then can u tell me whats the right code. Fix it if its not correct and also tell me reason thx
    Much appreciate that


    Exercise:
    Create a MikroC program to read in an 8-bit unsigned number on PORTB. If the number is even AND divisible by 3, PORTC.F0 = 1. Otherwise PORTC.F0 = 0.

    Solution


    Code:
    void main(){
    ADCON1=7; TRISB=0b11111111; TRISC=0;
    while (1)
    {
              else if (PORTB%3==0){PORTC.F0=1;}
              else {PORTC.F0=0;}
              }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you would only have to compile it to tell that it wasn't right.

    Before an else if, you need an if.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    void main() should actually be int main(void) and should return 0 on successful completion... Look at Salem's avatar above, that should give you a hint.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Since they're almost certainly on a free-standing implementation, MikroC is aimed at embedded microchip kind of systems, then main can be declared as void, if that is what the compiler docs say.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    "If the number is even AND divisible by 3"

    So the number must be divisible by 6.

  6. #6
    Registered User suda_51's Avatar
    Join Date
    Apr 2011
    Posts
    2
    I dont thınk ıt dıvıson by 3 ıf thus number ıs even .C wıll otomatıcly change your value and maybe there wıll be problem.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    confirm it that is this correct solution
    You tell us!

    One of the trickiest things with embedded code is getting data in and out of the micro. Until you fully understand how to do it, it can be quite frustrating.
    You need to set the ports up to be general purpose I/O, as outputs vs inputs, etc. Most of that is VERY device specific so only someone familiar with your device can comment on that part of the code.

    Can you debug? Why not create some temporary variables that you can "watch".

    Code:
    unsigned char uchar_port_B;
    
    uchar_port_B = PORTB;
    Now when you run you can look at uchar_port_B (you probably cannot look directly at PORTB, right?)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. true division in C ?
    By acidblue in forum C Programming
    Replies: 3
    Last Post: 12-03-2009, 11:22 PM
  2. Replies: 5
    Last Post: 10-14-2009, 05:47 AM
  3. Mod Division Question
    By GCNDoug in forum C Programming
    Replies: 3
    Last Post: 04-05-2007, 05:14 PM
  4. Division by 0
    By ajaxthegreater in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-08-2005, 04:21 PM
  5. Division Algorithm
    By author in forum C Programming
    Replies: 4
    Last Post: 04-15-2005, 12:02 PM