Thread: New member just saying hi, along with my first question :)

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    New member just saying hi, along with my first question :)

    Hi, new to the boards and in fact im new to programming as well. right now im taking my first C programming class and its going fairly well. its fun and interesting but tends to eat up a bunch of my time...

    but anyways here is my first question...

    im working on a group assignment right now, the assignment was split into 4 separate programs. each program builds on itself in a way that the prior program becomes a function for the next. the problem i am having is trying to understand one of my group member's program. apparently he got his older brother to help him so my partner cant really explain whats happening very well. the purpose of his program was for a user to input a start date and end date then the program to compute the numbers of days in between them.

    the 3 main things im having trouble with is...

    1. when in the program it state "diff = diff + DAYSOF(startyear); " can we use the "++" function for this? and how?

    2. what exactly are these 3 lines of code for?
    Code:
     
     tempday=startday; startday=endday; endday=tempday;
    
      tempmonth=startmonth; startmonth=endmonth; endmonth=tempmonth;
    
      tempyear=startyear; startyear=endyear; endyear=tempyear;
    3.and this macro "#define DAYSOF(x) (is_leap(x)?DAYSINLEAPAYSINYEAR)" is this the same as a if then statement?

    Code:
    #include <stdio.h>
    #include "tfdef.h"
    #include "leap.h"
    #include "days.h"
    #include "julian.h"
    
    #define DAYSINLEAP 366
    #define DAYSINYEAR 365
    
    #define DAYSOF(x) (is_leap(x)?DAYSINLEAP:DAYSINYEAR)
    
    main()
    { /* start function main */
    
            /* variable declarations */
            int flag;
            int startday, startmonth, startyear;
            int endday, endmonth, endyear;
            int tempday, tempmonth, tempyear;
            int diff;
    
            /* prompt user for initial start date */
            printf("EOF to quit\n");
            printf("Enter integer start date <month> <day> <year>: ");
            flag = scanf("%d %d %d",&startmonth,&startday,&startyear);
    
            while (flag!=EOF)
            { /* start while loop */
    
                    /* prompt user for end date */
                    printf("Enter integer end date <month> <day> <year>: ");
                    scanf("%d %d %d",&endmonth,&endday,&endyear);
    
                    /* exchange start and end dates for circumstance */
                    if (startyear>endyear)
                    { /* start if statement */
    
                            tempday=startday; startday=endday; endday=tempday;
                            tempmonth=startmonth; startmonth=endmonth; endmonth=tempmonth;
                            tempyear=startyear; startyear=endyear; endyear=tempyear;
    
                    } /* END IF STATEMENT */
    
                    if ( (startyear==endyear)&&(startmonth>endmonth) )
                    { /* START IF STATEMENT */
    
                            tempday=startday; startday=endday; endday=tempday;
                            tempmonth=startmonth; startmonth=endmonth; endmonth=tempmonth;
                            tempyear=startyear; startyear=endyear; endyear=tempyear;
    
                    } /* end if statement */
    
                    if ( (startyear==endyear)&&(startmonth==endmonth)&&(startday>endday) ) 
                    { /* start if statement */
    
                            tempday=startday; startday=endday; endday=tempday;
                            tempmonth=startmonth; startmonth=endmonth; endmonth=tempmonth;
                            tempyear=startyear; startyear=endyear; endyear=tempyear;
    
                    } /* end if statement */
    
                    if (startyear==endyear)
                            diff = julian_date(endday, endmonth, endyear)-
                                   julian_date(startday, startmonth, startyear);
    
                    else
                    { /* start if statement */
                            /* calculate from start to end of its year */
                            /* and from end to beginning of its year */
                            diff = julian_date(endday, endmonth, endyear)+
                                       DAYSOF(startyear)-
                                       julian_date(startday, startmonth, startyear);
    
                            /* add all days from the 'between' years */
                            startyear+=1;
            
                            while(startyear<endyear)
                            { /* start while loop */
            
                                    diff = diff + DAYSOF(startyear);
                                    startyear++;
    
                            } /* end while loop */
    
                    } /* end else statement */
    
                    printf("The date difference is %d days.\n",diff);
    
                    /* prompt user for new start and end dates */
    
                    printf("Enter integer start date <month> <day> <year>: ");
                    flag=scanf("%d %d %d",&startmonth,&startday,&startyear);
    
            } /* end while loop */
    
    } /* end function main */
    i know, kinda long.........sorry.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    ++ always adds one, which is not what you're adding. You can use +=, though.

    You should be able to step through those three lines (once you've got one line, you've got them all). Say startday is 4 and endday is 7. What are they afterwards?

    Yes.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    1.im not sure what the += does.

    2.not to sure if i follow, his program looks all confusing to me.

    3.i thought so.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    nvm i figured out #1, diff+=DAYSOF(startyear);

    number two is still weird to me.

    thanks for your help!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's three statements -- and they're assignment statements too, which are the easiest of them all. Current: startday == 4 endday == 7 tempday == ?
    Code:
    tempday=startday;
    Current: startday == 4 endday == 7 tempday == 4
    Code:
    startday=endday;
    Current: startday == 7 endday == 7 tempday == 4
    Code:
    endday=tempday;
    Current: startday == 7 endday == 4 tempday == 4
    So what happened here to startday and endday?

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    #2

    Look at it line by line. It's for switching variables. You can't switch two variables without having a temporary variable in the middle to hold one of the values, or you overwrite one and can't the original back.

    tempday=startday; startday=endday; endday=tempday;

    c=a;
    a=b;
    b=c;

    b is now a, a is now b, and c is the temp who made it happen.

    I think the reason he did it is because it isn't guaranteed the dates entered will be from biggest to smallest (for substraction). For example the user will enter 2008, then enter 1970, which is 38. Which is good, you can work with that. If instead they enter 1970, then 2008, you get -38. So those three if statements just take care of that situation by switching the start and end dates.

    #3

    Code:
    #define DAYSINLEAP 366
    #define DAYSINYEAR 365
    
    #define DAYSOF(x) (is_leap(x)?DAYSINLEAP:DAYSINYEAR)
    That's a macro (preprocessor), and a ternary (a?x:y). It's just a shorthand/efficiency. It might be to avoid conversions, disregarding that it would be similar to this:

    Code:
    int DaysInYear(int year)
    {
      if(is_leap(year))
        return 366;
      else
        return 365;
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    wow, ok i get it now. thanks for the explanations guys/gals!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, some help on the way. Implicit main is frowned upon, so make sure to avoid it!
    http://cpwiki.sourceforge.net/Implicit_main
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    ^i didnt know about that, my professor never explained that to us.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Welcome along, and might I say, you're asking exactly the kinds of questions that you should be asking. I have no doubt you'll do well enough in learning programming and I expect you'll get along with others alright here too.
    We often get people wanting us to write their programs for them which is a no go. But this kind of thing is exactly what we are here to help with.

    Seeing as how everyone has explained it all already, I'll simply wish you good luck with your assignment.

    Edit: Oh one thing I'll mention though is that it would be a good idea to create a 'swap' function that swaps two ints. Your program is doing 9 swaps already and although the code has been made to put the bits of each swap all on one line, it's 27 statements in total for those swaps alone. Writing a swap function would make those 9 swaps really only be one line of code each, and it would be much tidier and easier to spot a bug if there was one. It would mean you'd need some knowledge of pointers and functions though, so it's up to you whether you attempt that now or a bit later on. We're here to help if you need it.
    Last edited by iMalc; 10-31-2008 at 02:49 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Wow... iMalc's edit covered about the only things I was going to contribute to this thread. Are you yet familiiar with structures, youareafever?

    (Oh, and thank you ever so much for properly intending, using a reasonable coding convention, and using code tags)

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    wow i feel really welcomed here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member member function pointers
    By Bigbio2002 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2005, 05:14 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  4. Template question
    By grscot in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2003, 03:12 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM