Thread: explanation needed!

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    explanation needed!

    hello, I found one of my brothers old college assignments and I would like a quick explanation of the code. I know what it does, I compiled the code and the program asks you to enter a year between 2000 and 2099 and prints out the calendar for that year.
    if somebody could explain it to me because iam a huge noob when it come to programming..thanx in advance.
    Code:
    
    
    Code:
    #include<stdio.h>
    #include<windows.h>
    COORD coord={0,0}; void gotoxy(int x,int y)
    { coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); }
    main()
    {
    int year,Day_of_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    char
    Month[12][50]={" January"," February"," March"," April"," May"," June"," July"," August","September"," October"," November"," December"};
    int month,day,first_day_of_the_year; printf("\nEnter the year "); scanf("%d",&year);
    system("cls"); while(year<2000||year>2099)
    {
    printf("wrong input\nplease enter again "); scanf("%d",&year);
    system("cls");
    }
    if(year%4==0)
    Day_of_month[1]=29; first_day_of_the_year=(((year+3-2000)/4)+(year-2000))%7; for(month=0;month<12;month++)
    {
    gotoxy((3*(month%3)+1)*8+(month%3)*3,12*(month/3)); printf("%s",Month[month]);
    gotoxy( (27*(month%3)+3),12*(month/3)+1);
    printf("Sa Su Mo Tu We Th Fr");
    for(day=1;day<=Day_of_month[month];day++)
    {
    gotoxy(27*(month%3)+3+((first_day_of_the_year+day- 1)%7)*3,2+12*(month/3)+(first_day_of_the_year+day-1)/7);
    printf("%2d",day);
    } first_day_of_the_year=(first_day_of_the_year+Day_of_month[month])%7; }
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    personally you being a noob, and you do not possess the comprehend of what the code is doing just by looking at it yourself.

    I'd say it'd be better for you to take a few steps backwards and look for some more of your brothers code that was say his first week in programming class. but that is my option.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You could start by formatting this mess a little better, maybe then you'd be able to understand it a little more:
    Code:
    #include<stdio.h>
    #include<windows.h>
    
    COORD coord= {0,0};
    void gotoxy(int x,int y)
    {
        coord.X=x;
        coord.Y=y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
    }
    
    main()
    {
        int year,Day_of_month[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
        char Month[12][50]= {
        	" January"," February"," March"," April"," May"," June",
        	" July"," August","September"," October"," November"," December"
        };
        int month,day,first_day_of_the_year;
        
        printf("\nEnter the year ");
        scanf("%d",&year);
        system("cls");
        while(year<2000||year>2099) {
            printf("wrong input\nplease enter again ");
            scanf("%d",&year);
            system("cls");
        }
       
        if(year%4==0) {
            Day_of_month[1]=29;
        }
        first_day_of_the_year=(((year+3-2000)/4)+(year-2000))%7;
    
        for(month=0; month<12; month++) {
            gotoxy((3*(month%3)+1)*8+(month%3)*3,12*(month/3));
            printf("%s",Month[month]);
            gotoxy( (27*(month%3)+3),12*(month/3)+1);
            printf("Sa Su Mo Tu We Th Fr");
    
            for(day=1; day<=Day_of_month[month]; day++) {
                gotoxy(27*(month%3)+3+((first_day_of_the_year+day- 1)%7)*3,
                	   2+12*(month/3)+(first_day_of_the_year+day-1)/7);
                printf("%2d",day);
            }
            first_day_of_the_year=(first_day_of_the_year+Day_of_month[month])%7;
        }
    }
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Your question is too general. What part don't you understand?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #ifdef _WIN32
    
    #include <windows>
    
    #define CLEAR_COMMAND "cls"
    
    void gotoxy(int x, int y)
    {
      COORD coord = (COORD){x, y};
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    #else
    
    #define CLEAR_COMMAND "clear"
    
    void gotoxy(int x, int y)
    {
        printf("\033[%d;%dH", y + 1, x + 1);
    }
    
    #endif
    
    
    int main()
    {
      int year, Day_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
      char Month[12][15] = {
        "January", "February", "March",     "April",   "May",      "June",
        "July",    "August",   "September", "October", "November", "December"};
      int month, day, first_day_of_the_year;
    
      while (1)
      {
        printf("Enter the year ");
        scanf("%d", &year);
        if (year >= 2000 && year <= 2099)
            break;
        printf("Year must be from 2000 to 2099\n");
      }
    
      system(CLEAR_COMMAND);
    
      if (year % 4 == 0) // works because years are restricted to 2000 to 2099
        Day_of_month[1] = 29;
    
      first_day_of_the_year = (((year + 3 - 2000) / 4) + (year - 2000)) % 7;
    
      for (month = 0; month < 12; month++)
      {
        gotoxy((3 * (month % 3) + 1) * 8 + (month % 3) * 3, 8 * (month / 3));
        printf("%s", Month[month]);
        gotoxy((27 * (month % 3) + 3), 8 * (month / 3) + 1);
        printf("Sa Su Mo Tu We Th Fr");
        for (day = 1; day <= Day_of_month[month]; day++)
        {
          gotoxy(27 * (month % 3) + 3 + ((first_day_of_the_year + day - 1) % 7) * 3,
                 2 + 8 * (month / 3) + (first_day_of_the_year + day - 1) / 7);
          printf("%2d", day);
        }
        first_day_of_the_year = (first_day_of_the_year + Day_of_month[month]) % 7;
      }
      
      printf("\n\n");
    
      return 0;
    }
    It's not a very good algorithm, anyway, since you don't really need the positioning code (gotoxy). It can be done line-by-line easily enough.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    thank you.

    Quote Originally Posted by john.c View Post
    Your question is too general. What part don't you understand?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #ifdef _WIN32
    
    #include <windows>
    
    #define CLEAR_COMMAND "cls"
    
    void gotoxy(int x, int y)
    {
      COORD coord = (COORD){x, y};
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    #else
    
    #define CLEAR_COMMAND "clear"
    
    void gotoxy(int x, int y)
    {
        printf("\033[%d;%dH", y + 1, x + 1);
    }
    
    #endif
    
    
    int main()
    {
      int year, Day_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
      char Month[12][15] = {
        "January", "February", "March",     "April",   "May",      "June",
        "July",    "August",   "September", "October", "November", "December"};
      int month, day, first_day_of_the_year;
    
      while (1)
      {
        printf("Enter the year ");
        scanf("%d", &year);
        if (year >= 2000 && year <= 2099)
            break;
        printf("Year must be from 2000 to 2099\n");
      }
    
      system(CLEAR_COMMAND);
    
      if (year % 4 == 0) // works because years are restricted to 2000 to 2099
        Day_of_month[1] = 29;
    
      first_day_of_the_year = (((year + 3 - 2000) / 4) + (year - 2000)) % 7;
    
      for (month = 0; month < 12; month++)
      {
        gotoxy((3 * (month % 3) + 1) * 8 + (month % 3) * 3, 8 * (month / 3));
        printf("%s", Month[month]);
        gotoxy((27 * (month % 3) + 3), 8 * (month / 3) + 1);
        printf("Sa Su Mo Tu We Th Fr");
        for (day = 1; day <= Day_of_month[month]; day++)
        {
          gotoxy(27 * (month % 3) + 3 + ((first_day_of_the_year + day - 1) % 7) * 3,
                 2 + 8 * (month / 3) + (first_day_of_the_year + day - 1) / 7);
          printf("%2d", day);
        }
        first_day_of_the_year = (first_day_of_the_year + Day_of_month[month]) % 7;
      }
      
      printf("\n\n");
    
      return 0;
    }
    It's not a very good algorithm, anyway, since you don't really need the positioning code (gotoxy). It can be done line-by-line easily enough.
    what I dont get is the system(cls) function, what does it do specifically ??

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I suppose you could comment it out, then learn something through observing cause and effect.
    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.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I found one of my brothers old college assignments and I would like a quick explanation of the code.
    I recommend you ask your brother.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    system(cls)

    programming acronyms

    you might find it in here

    List of computing and IT abbreviations - Wikipedia

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by jimblumberg View Post
    I recommend you ask your brother.
    This makes me miss the "like" feature...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. evaluation of (std::cin >> var) explanation needed.
    By Subsonics in forum C++ Programming
    Replies: 10
    Last Post: 09-05-2010, 11:48 AM
  2. Tic Tac Toe Explanation Needed
    By almister in forum C Programming
    Replies: 0
    Last Post: 08-28-2009, 12:10 PM
  3. A bit of explanation needed.
    By JacobN in forum C Programming
    Replies: 3
    Last Post: 11-24-2007, 05:41 PM
  4. strswap. Explanation needed.
    By kantze in forum C Programming
    Replies: 5
    Last Post: 10-21-2006, 10:53 AM
  5. Explanation needed, what is this?
    By CAP in forum C Programming
    Replies: 8
    Last Post: 06-25-2002, 04:07 AM

Tags for this Thread