Thread: Need help understanding this code

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    45

    Need help understanding this code

    Its a code to convert any given year into its Roman equivalent.
    Code:
    #include<stdio.h>
    
    
    int roman(int year)
    {
    int i;
    printf("The year %d is converted to Roman------->",year);
    i=(year/1000);
    while(i)
    {
    printf("m");i--;
    }
    
    
    i=((');
    switch(i)
    {
    case 1:printf("c");
    break;
    
    
    case 2:printf("cc");
    break;
    
    
    case 3:printf("ccc");
    break;
    
    
    case 4:printf("cd");
    break;
    
    
    case 5:printf("d");
    break;
    
    
    case 6:printf("dc");
    break;
    
    
    case 7:printf("dcc");
    break;
    
    
    case 8:printf("dccc");
    break;
    
    
    case 9:printf("dcccc");
    break;
    }
    
    
    i=((year/10)%10);
    switch(i)
    {
    case 1:printf("x");
    break;
    
    
    case 2:printf("xx");
    break;
    
    
    case 3:printf("xxx");
    break;
    
    
    case 4:printf("xl");
    break;
    
    
    case 5:printf("l");
    break;
    
    
    case 6:printf("lx");
    break;
    
    
    case 7:printf("lxx");
    break;
    
    
    case 8:printf("lxxx");
    break;
    
    
    case 9:printf("xc");
    break;
    }
    
    
    i=(year%10);
    switch(i)
    {
    case 1:printf("i");
    break;
    
    
    case 2:printf("ii");
    break;
    
    
    case 3:printf("iii");
    break;
    
    
    case 4:printf("iv");
    break;
    
    
    case 5:printf("v");
    break;
    
    
    case 6:printf("vi");
    break;
    
    
    case 7:printf("vii");
    break;
    
    
    case 8:printf("viii");
    break;
    
    
    case 9:printf("ix");
    break;
    }
    printf("\n\n");
    
    
    } 
    
    
    main()
    {
    int year;
    printf("Enter any year:");
    scanf("%d",&year);
    
    
    roman(year);
    }
    Sorry,I took this code from the internet and its too big so I don't wanna waste time putting it into the right format.
    I am getting the output but couldn't understand the code.
    If I enter a year 1988,'m' would be printed one time,then the control will enter switch case,i=((year/100)%10) which means i will be 988 then how come 'dcccc' will be printed?Because 'dcccc' will only be printed when i will be 9 but i is 988 then how will 'dcccc' be printed?
    C enlightened

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    i=(('); should be i = year / 100.
    A C integer division produces an integer result. As does a C modulus. All the code is doing is exhaustively enumerating the cases for thousands, hundreds, tens and units, and building up the Roman string.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by Malcolm McLean View Post
    i=(('); should be i = year / 100.
    A C integer division produces an integer result. As does a C modulus. All the code is doing is exhaustively enumerating the cases for thousands, hundreds, tens and units, and building up the Roman string.
    oops,It was typo,actually its,
    i=((year/100)%10);
    So in that case i will be 988 but still it seems to print 'dcccc',how when i isn't 9?
    C enlightened

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > oops,It was typo,actually its,
    Yeah, and now we know that is likely to be not the only typo as well.

    Compile it, run it, then copy/paste it from your editor.
    Otherwise, you just waste everyone's time pointing out all your stupid mistakes.
    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 2012
    Posts
    45
    Quote Originally Posted by Salem View Post
    > oops,It was typo,actually its,
    Yeah, and now we know that is likely to be not the only typo as well.

    Compile it, run it, then copy/paste it from your editor.
    Otherwise, you just waste everyone's time pointing out all your stupid mistakes.
    I have already compiled it and ran it and I am getting the desired output.And the typo was when I copied it into this forum,it wasn't in the actual code.
    My question remains unanswered.
    C enlightened

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    1) you need to learn C
    2) you need to learn roman numerals[

    because if you
    Quote Originally Posted by sameertelkar View Post
    have already compiled it and ran it and I am getting the desired output.
    you would already know

    a) your NOT getting the desired output
    b) why your not getting the desired output
    and of course
    c) how to fix it...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I don't wanna waste time putting it into the right format
    My question remains unanswered.
    If you don't want to put the time in to format this basic code so it's more readable to others (which would take all of three minutes), what makes you think we would want to "waste time" reading through it to answer your question?

    Around here, you typically get back what you put in.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Quote Originally Posted by Matticus View Post
    Around here, you typically get back what you put in.
    and to add, so far, copying someone elses code, posting it here, and asking us to "fix this"....your not putting much into it!

    (thank you Matticus for your post )

  9. #9
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    sorry for disturbing,
    something are bothers me
    whats the difference code between
    Code:
    double i=10,x=25;
    while(i<25)
    {
    printf("%lf",i);i++;
    }
    with
    Code:
    double i=10,x=25;
    while(i<25)
    {
    printf("%lf",i);i--;
    }
    sorry for asking silly questions,

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Write a program to find out.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    ++ into positive
    -- into negative

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    here is how it goes....

    OMG everything i typed is gone!!!

    i give up...

    loser, put it in a program and run it and see, sorry but i am not retyping it all again!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help in Understanding this Code
    By Linked_List in forum C Programming
    Replies: 3
    Last Post: 09-04-2012, 09:32 PM
  2. Help understanding a Code
    By shiroaisu in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2010, 10:05 AM
  3. Help understanding a code
    By lolguy in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-15-2009, 04:13 PM
  4. help understanding code
    By volk in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 10:50 AM
  5. Code Understanding!
    By Jill n Jall in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 04:16 PM