about function in C++.....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nor farhana yaakub
    New Member
    • Sep 2006
    • 12

    about function in C++.....

    how r u..hope that u r fine..thanks again 4 ur and everybody's help..it really help...(*_*).
    By da way,again i have another question.hope u dun mind to teach me coz i need a guide to success and i have learn from a successful guy like u..

    1.write a top down design and a C++ program that reads a date in numeric form and prints in english..
    For example:
    enter a date in the form dd mm yy
    27 10 42
    twenty-seven October,ninetee n hundred forty two

    The program should work for any date in the twentieth centery and should print an error message for any invalid date such as 29 2 83(1983 was'nt a leap year).
    and i should read the date as one input.....

    i want to learn step by step then, i can send my homework to you,guys thus,i can learn from my mistakes coz i am still new in programming.... thanks for your guide.......
  • Nor farhana yaakub
    New Member
    • Sep 2006
    • 12

    #2
    [please can somebody guides me???i dun want the answer,i juz want you all guys,guide me...thanks.... .

    Comment

    • Nor farhana yaakub
      New Member
      • Sep 2006
      • 12

      #3
      please,if somebody knows...knowled ge is to share.......... .

      1.write a top down design and a C++ program that reads a date in numeric form and prints in english..
      For example:
      enter a date in the form dd mm yy
      27 10 42
      twenty-seven October,ninetee n hundred forty two

      The program should work for any date in the twentieth centery and should print an error message for any invalid date such as 29 2 83(1983 was'nt a leap year).
      and i should read the date as one input.....

      i want to learn step by step then, i can send my homework to you thus,you can tell me where i wrong,thus i can correct and learn from my mistakes coz i am still new in programming.... thanks for your guide.......

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        We aren't doing your home work for you, we will help you correct mistakes in it.

        Start by writing a program that asks the user to input the date in numeric format, the day, month and year.

        Once you can enter these values and successfully print them then consider how you might change that to a text format.

        Write some code and post it here.

        Comment

        • dush
          New Member
          • Sep 2006
          • 27

          #5
          Hi Nor farhana yaakub

          You are lucky guy. I have written whole program for you, but next time you do it yourself ok?

          Here is the code:

          Code:
          #include <iostream>
          using namespace std;
          
          char* int0_9ToStr(int i, bool ordinal) {
            char* n[] = {" ", "one", "two", "three" ,"four", "five", "six", "seven", "eight", "nine"};
            char* no[] = {" ", "first ", "second ", "thrird " ,"fourth ", "fifth ", "sixth ", "seventh ", "eight ", "ninth "};    
            return (ordinal ? no[i] : n[i]) ;
          }
          
          char* int10_19ToStr(int i, bool ordinal) {
            char* n[] = {"ten", "eleven", "twelve" ,"thirteen", "fourteen", "fiveteen", "sixteen", "seventeen", "eighteen", "nineteen"};  
            char* no[] = {"tenth ", "eleventh ", "twelfth " ,"thirteenth ", "fourteenth ", "fiveteenth ", "sixteenth", "seventeenth ", "eighteenth ", "nineteenth "};    
            return (ordinal ? no[i-10] : n[i-10]) ;
          }
          
          char* int0_90ToStr(int i, bool ordinal) {
            char* n[] = {"", "", "twenty", "thirty", "forty" ,"fifty", "sixty", "seventy", "eighty", "ninety"};    
            char* no[] = {"", "", "twentieth", "thirtieth"};    
            return (ordinal ? no[i] : n[i]) ;
          }
          
          void printDay(int d) {
            cout << (10<=d && d<=19 ? int10_19ToStr(d, 1) : (cout << (d%10 ? int0_90ToStr(d/10, 0) : int0_90ToStr(d/10, 1)), int0_9ToStr(d%10, 1)));
          }
          
          int main ()
          {
            int year, month, day;
            
            cout << "Enter valid twetieth century date in numeric form: ";
            cin >> day >> month >> year;  
            cout << endl;
            
            if (0<=year && year<100)  
            switch (month)
            {
              case 1: if (0<day && day<=31) {printDay(day); cout << "january";}
                      else {cout << "Error: January has 31 days.\n"; exit(1);}
                      break;
              case 2: if (0<day && ((day<=29 && !(year%4)) || (day<=28 && (year%4))))
                      {printDay(day); cout << "february";}
                      else if (!(year%4)) {cout << "Error: February has 29 days in year " << year+1900 << ".\n"; exit(1);}
                      else {cout << "Error: February has 28 days in year " << year+1900 << ".\n"; exit(1);}
                      break;
              case 3: if (0<day && day<=31) {printDay(day); cout << "march";}
                      else {cout << "Error: March has 31 days.\n"; exit(1);}
                      break;
              case 4: if (0<day && day<=30) {printDay(day); cout << "april";}
                      else {cout << "Error: April has 30 days.\n"; exit(1);}
                      break;
              case 5: if (0<day && day<=31) {printDay(day); cout << "may";}
                      else {cout << "Error: May has 31 days.\n"; exit(1);}
                      break;
              case 6: if (0<day && day<=30) {printDay(day); cout << "june";}
                      else {cout << "Error: June has 30 days.\n"; exit(1);}
                      break;
              case 7: if (0<day && day<=31) {printDay(day); cout << "july";}
                      else {cout << "Error: July has 31 days.\n"; exit(1);}
                      break;
              case 8: if (0<day && day<=31) {printDay(day); cout << "august";}
                      else {cout << "Error: August has 31 days.\n"; exit(1);}
                      break;
              case 9: if (0<day && day<=30) {printDay(day); cout << "september";}
                      else {cout << "Error: September has 30 days.\n"; exit(1);}
                      break;
              case 10: if (0<day && day<=31) {printDay(day); cout << "october";}
                      else {cout << "Error: October has 31 days.\n"; exit(1);}
                      break;
              case 11: if (0<day && day<=30) {printDay(day); cout << "november";}
                      else {cout << "Error: November has 30 days.\n"; exit(1);}
                      break;
              case 12: if (0<day && day<=31) {printDay(day); cout << "december";}
                      else {cout << "Error: December has 31 days.\n"; exit(1);}
                      break;
              default : cout << "Error: Entered month is invalid."; exit(1); break; 
            }
            else {cout << "Error: Entered year is out of twentieth century."; exit(1);}
            
            cout << " nineteen hundred ";
            cout << (10<=year && year<=19 ? int10_19ToStr(year, 0) : (cout << int0_90ToStr(year/10, 0), int0_9ToStr(year%10, 0))) << endl;
            
            return 0;
          }
          by the way, dont anybody know about job for C++ programmer?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Nice code but 1 small error in it.

            The year 1900 was not a leap year so February only had 28 days in it, unfortunately your code passes 29th February 1900 has a valid date which it is not.

            Comment

            • dush
              New Member
              • Sep 2006
              • 27

              #7
              I didnt know that. Why is not 1900 leap if 2000 leap is?

              anyway, the probability for user to enter such a date is 1:36500, however I believe you Banfa that 1900 is not leap and here is the correction:

              Code:
                 case 2: if (0<day && ((day<=29 && !(year%4) && year) || (day<=28 && (year%4 || !year))))
                          {printDay(day); cout << "february";}
                          else if (!(year%4) && year) {cout << "Error: February has 29 days in year " << year+1900 << ".\n"; exit(1);}
                          else {cout << "Error: February has 28 days in year " << year+1900 << ".\n"; exit(1);}
                          break;
              there is also mistake in word "twetieth" in prompt

              Comment

              • D_C
                Contributor
                • Jun 2006
                • 293

                #8
                Code:
                if((year % 4) == 0)
                  leap = true;
                if((year % 100) == 0)
                  leap = false;
                if((year % 400) == 0)
                  leap = true;
                That's because each year, on average, is about 365 + 1/4 - 1/100 + 1/400, or approximately 365.2425 days/year.

                Comment

                • Nor farhana yaakub
                  New Member
                  • Sep 2006
                  • 12

                  #9
                  thanks for your help..i dun need you to write the source code for me,coz it makes i become not satisfed with myself...i am juz learning programming within 3months.. by da.. way,thanxs.
                  i have found the internet by myself about the calculation of the leap year.....

                  ..Ordinal Dates
                  Ordinal Dates, popularly but incorrectly called Julian Dates (or Julian Calendar), provide a year and the number of days into that year. 2000-January-01 is 2000-001. 1999-December-31 is 1999-365.

                  To translate between standard Gregorian Dates and Ordinal Dates the following code fragment can provide a helpful model:


                  short OrdinalDays[] = {
                  /* days (0-364 or 0-365) BEFORE each month */
                  0,31,59,90,120, 151,181,212,243 ,273,304,334,36 5,
                  0,31,60,91,121, 152,182,213,244 ,274,305,335,36 6
                  };
                  short LeapSw; /* 0 if not a leap year, 13 if leap year */

                  LeapSw = ( Year%4 || (!(Year%100) && Year%400)) ? 0 : 13;To translate a Gregorian Month (1-12) and Day (1-31), into an Ordinal day number (0-364 or 0-365) for a given Year:

                  Ordinal = OrdinalDays[Month-1+LeapSw] + Day-1;Note how LeapSw directs the array references to the first or last half of OrdinalDays where the first half is for normal years and the second half is for leap years.

                  To determine the number of days in the current Year:

                  DaysInYear = OrdinalDays[12+LeapSw];Note that the OrdinalDays table has 13 month values in it for each type of year just to allow this subscript to work.

                  To determine the number of days in the current Gregorian Month:

                  DaysInMonth = OrdinalDays[Month+LeapSw]
                  - OrdinalDays[Month-1+LeapSw];

                  Comment

                  Working...