Thursday, March 31, 2005

Day Number of a date

#include
using namespace std;

//PURPOSE: Calculate the number of days in February for a given year
//PRECONDITION: Check whether the year is a leap or not
//POSTCONDTION: return the correct number of days in February for that year
int febdays(int year)
{

if(year%100 == 0 && year%400==0){
return 29;

}

else if(year%4 == 0){
return 29;
}

else{
return 28;
}
}

//PURPOSE: Calculate the number of days for a given date within that year
//PRECONDITION: Check whether the year is a leap or not
//POSTCONDTION: return the corresponding number of days for that year
int daynum(int month, int day, int year)
{
int yeardays = 0;//declare and initialize variable to hold the day number of the year

if(month == 1){
yeardays = day;
return yeardays;
}

if(month == 2){
yeardays = 31 + day;
return yeardays;
}

if(month == 3){
yeardays = 31 + febdays(year) + day ;
return yeardays;
}

if(month == 4){
yeardays = (31 * 2) + febdays(year) + day;
return yeardays;
}

if(month == 5){
yeardays = (31 * 2) + 30 + febdays(year) + day;
return yeardays;
}

if(month == 6){
yeardays = (31 * 3) + 30 + febdays(year) + day;
return yeardays;
}

if(month == 7){
yeardays = (31 * 3) + (30 * 2) + febdays(year) + day;
return yeardays;
}

if(month == 8){
yeardays = (31 * 4) + (30 * 2) + febdays(year) + day;
return yeardays;
}

if(month == 9){
yeardays = (31 * 5) + (30 * 2) + febdays(year) + day;
return yeardays;
}
if(month == 10){
yeardays = (31 * 5) + (30 * 3) + febdays(year) + day;
return yeardays;
}
if(month == 11){
yeardays = (31 * 6) + (30 * 3) + febdays(year) + day;
return yeardays;
}

if(month == 12){
yeardays = (31 * 6) + (30 * 4) + febdays(year) + day;
return yeardays;
}
return 0;
}

//PURPOSE: Check whether the month entered by the user is valid
//PRECONDITION: Month should be between 1 and 12
//POSTCONDTION: Display error message if the month is invalid
int CheckMonth(int month)
{
if(month <> 12){
cout << ">> Invalid Month <<\n";
cout << "Month should be between 1 and 12 \n";
return 1;
}
return 0;
}


//PURPOSE: Check whether the day entered by the user is valid
//PRECONDITION: Day should be in agreement with the month. e.g September has 30 days not 31
//POSTCONDTION: Display error message if the day is invalid
int CheckDay(int month, int day)
{
if(day <>
cout << ">>Invalid Day<<\n";
cout << "Day has to be a positive number\n";
return 1;
}
if(month == 2){
if(day > 29){
cout << ">> Invalid Day <<\n";
cout << "February has 28 days in a regular year and 29 days in a leap year\n";
return 1;
}
}
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
if(day > 31){
cout << ">> Invalid Day <<\n";
cout << "Day should be between 1 and 31 \n";
return 1;
}

}
else{
if(day > 30){
cout << ">> Invalid Day <<\n";
cout << "Day should be between 1 and 30\n";
return 1;
}
}
return 0;
}

//PURPOSE: Check whether the year entered by the user is valid
//PRECONDITION: Year should be greater than zero
//POSTCONDTION: Display error message if the year is invalid
int CheckYear(int day, int year)
{
if(year <>
cout << ">> Invalid Year <<\n";
cout << "Year has to be positive\n";
return 1;
}
if(day == 29 && febdays(year)!=29){
cout << ">> Invalid Year <<\n";
cout <<>
cout << "Therefore Februaury cannot have 29 days\n";
return 1;
}
return 0;

}



//PURPOSE: Prompt user for the date in month/day/year format
//PRECONDITION: Check whether the entered date is valid, display error messages if it is not correct
//POSTCONDTION: return 0 to indicate success
int main()
{
int month = 0, day = 0, year = 0;
cout << "Enter the month: ";
cin >> month;
if(CheckMonth(month) == 1){
return 0;
}
cout << "Enter the day: ";
cin >> day;
if (CheckDay(month, day) == 1){
return 0;
}
cout << "Enter the year: ";
cin >> year;
if (CheckYear(day,year) == 1){
return 0;
}
cout <<>
return 0;

}

Reverend Zeller's Algorithm

Zeller's algorithm is used to compute the day of the week on which a given date will fall (or fell).

Let A, B, C and D denote integer variables that contain the following values:

  • A = the month of the year, with March having the value 1, April the value 2, . . . , December the value 10, and with January and February being counted as months 11 and 12 of the preceding year (in which case, subtract 1 from C).

  • B = the day of the month (1, 2, 3, . . . , 28, 29, 30, 31).

  • C = the year of the century (this would be 95 for the year 1895).

  • D = the century (this would be 18 for the year 1895).

Next let W, X, Y, Z and R denote integer variables, and compute the following values in this order, using integer arithmetic.

     W = (13A - 1) / 5
X = C / 4
Y = D / 4
Z = W + X + Y + B + C - 2D
R = the remainder obtained when Z is divided by 7.
The value of R represents the day of the week, where r = 0 represents Sunday, r = 1 represents Monday,. . . , and r = 6 represents Saturday. If R turns out to have a negative value such as -4, add 7 to it to get a nonnegative value between 0 and 6.

This above was obtained from http://www.cs.niu.edu/~t90hch1/csci210/zeller.htm

include <>

iostream, string and iomanip should be within <>. When the mentioned words are posted within <> they are not displayed on the screen. No idea why!!!

Calender Generation in Visual C++

#include iostream
using namespace std;
#include string
#include iomanip

void DayList()
{
//PURPOSE: display the initials of the days of the week
//PRECONDITION: week should start from Friday
//POSTCONDTION: the initials should be displayed horizontally with constant width between the initials
cout << setw(3) << "F";
cout << setw(3) << "S";
cout << setw(3) << "S";
cout << setw(3) << "M";
cout << setw(3) << "T";
cout << setw(3) << "W";
cout << setw(3) << "T" << "\n";
}


int febdays(int year)
{
//PURPOSE: Calculate the number of days in February for a given year
//PRECONDITION: Check whether the year is a leap or not
//POSTCONDTION: return the correct number of days in February for that year
if(year%100 == 0)
{
if(year%400 == 0)
{
return 29;
}
else
return 28;
}
else if(year%4 == 0)
return 29;
else
return 28;
}



int daysInMonth(int month, int year)
{
//PURPOSE: Assign number of days for a particular month
//PRECONDITION: Check for leap years
//POSTCONDITION: Return the number of days in a month for that year
int MonthDays = 0;
switch(month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
MonthDays = 31;
return MonthDays;
break;
case 4: case 6: case 9: case 11:
MonthDays = 30;
return MonthDays;
break;
case 2:
MonthDays = febdays(year);
return MonthDays;
break;
}
return 0;
}

int dayOfWeek(int month, int day, int year)
//PURPOSE: compute the day of the week for a date
//PRECONDITION: month, year, and day
//POSTCONDITION:the day number of the date (0=Sunday)
{
int century; // century
int EffectOfMonth; // days caused by month
int LeapYears; // days caused by leap years
int EffectOfCentury;// days caused by century
int TotalDays; // total days in date
int WeekDay; // day of week for given date

century = year/100;
year = year - century * 100;

//Apply Reverend Zeller's Algorithm
//Step 1: Adjust year so 00 becomes 100
if (year == 00) {
year = 100;
century = century - 1;
}

//Step 2: Adjust month so March == 1
if ((month < 3) && (year==1)) {
century=century - 1;
year=100;
}
else if (month < 3) {
year=year - 1;
}

//Step 3: Count the days
month=(month + 9) % 12 + 1;
EffectOfMonth=(13*month - 1) / 5;
LeapYears=year / 4;
EffectOfCentury=century / 4;
TotalDays=EffectOfMonth + LeapYears +
EffectOfCentury + day +
year + 5*century;

//Step 4: Compute and return day of week
WeekDay=TotalDays % 7;

return WeekDay;
}


string monthName(int month)
//PURPOSE: Determine the name of the month
//PRECONDITION: Match the number of the month to its name
//POSTCONDITION: Return the name of the respective month
{
switch(month)
{
case 1:
return " January";
break;
case 2:
return " February";
break;
case 3:
return " March";
break;
case 4:
return " April";
break;
case 5:
return " May";
break;
case 6:
return " June";
break;
case 7:
return " July";
break;
case 8:
return " August";
break;
case 9:
return " September";
break;
case 10:
return " October";
break;
case 11:
return " November";
break;
case 12:
return " December";
break;
}
}

int displayMonth(int year, int month, int day)
//PURPOSE: Display calender for the month
//PRECONDITION: Dates should correspond to the correct days
//POSTCONDITION: return 0 if successful
{
int a = 0;
for(month = 1; month <=12; month++){
cout << monthName(month);
cout << "\n" << "\n";
DayList();
day = 1;
if((dayOfWeek(month, day, year))== 0){
cout << setw(9) << day;
day = day + 1;
a = 9;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 1){
cout << setw(12) << day;
day = day + 1;
a = 12;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 2){
cout << setw(15) << day;
day = day + 1;
a = 15;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 3){
cout << setw(18) << day;
day = day + 1;
a = 18;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 4){
cout << setw(21) << day;
day = day + 1;
a = 21;
for(day = 2; day <= daysInMonth(month, year); day++){
if(a == 21){
a = 0;
cout << "\n";
}
cout << setw(3) << day;
a = a + 3;

}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 5){
cout << setw(3) << day;
day = day + 1;
a = 3;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 6){
cout << setw(6) << day;
day = day + 1;
a = 6;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
}
return 0;
}

int main()
//PURPOSE: Prompt user for an year
//PRECONDITION: Check whether the entered date is valid, display error messages if it is not correct
//POSTCONDTION: return 0 to indicate success
{
int month = 0 , year = 0, day = 0;
cout << "Enter an year between 1582 and 4092: ";
cin >> year;
if((year <> 4092)){
cout << ">>Invalid Year<<\n";
cout << ">>Year should be between 1582 and 4092<<\n";
}
while((year >= 1582)&&(year <= 4092)){
displayMonth(year, month, day);
cout << "Enter an year between 1582 and 4092: ";
cin >> year;
cout << "\n" << "\n";
}
return 0;
}

Friday, December 17, 2004

Imaginality!!

What man imagines eventually becomes reality!!!!

http://www.gizmodo.com/gadgets/robots/index.php

Tuesday, December 07, 2004

Origin of the universe

Is the Big Bang is proof of existence of God if you believe in religion? Of course there is a whole lot of mathematics involved in proving these philosophical thoughts…. And there are those that believe in other theories. Also what is accepted today as the answer is scoffed at tomorrow. So would we ever really find out the answer to the mother of all questions?
How did the universe originate? It was created from nothing.. A Big Bang that started it all. How did this Big Bang occur? It occurred because God willed it to occur!!! Is the answer that simple?

http://www.leaderu.com/offices/billcraig/docs/theism-origin.html

http://www.closertotruth.com/topics/universemeaning/105/105transcript.html

Tuesday, November 30, 2004

Life

Our mortal lives will one day come to an end. Why live then; what is the purpose? Career, family, partying, living life as today is your last? are the people who believe in God wasting their time by making unwanted sacrifices or are they right? Or are the atheist right? Are we just animals with higher intelligence that developed the idea of God? But then who created the animals...us? Why didnt God send the right message the first time? why so many messengers (Mosses, Isa, Muhammed). The message that was revealed last is right.... evidently being the quran and thus Islam. What happens to the people before Islam or whatever the religion you follow was revealed? Are they judged on their religion which existed in their time? What about the babies and the children that die at an early age before they can commmit a sin? Do they get eternal paradise? I can go on and on..so can any literate human!!! I guess life is unfair and it does not look like we are all equal... as related to God and the right religion... Wouldn't you want the answer at any cost? so that you can acheive eternal paradise and understand life? What if you some how get arrested and put into prison somewhere.. a rather remote area..your family and friends have no idea where you are and you have no way of contacting them. There is no way to trace where you are as your there are no phones around and your mobile is switched off and taken from you. You are all alone in this place...prison. You live your first few years as a scoundrel. Fighting, Swearing, sinning and almost had it with life. Then you turn to God and study his path. Yes you are in prison but you devote to God assume you can get the Holy books. You study the major religions of the world, you decide which religion is right and you follow his way perfectly for the rest of your life as you have nothing else to do in prison. You are in a solitary cell. You and your holy book and God. You have no family, no friends..just people around you that are like or unlike you. So for years you follow your chosen path..Gods path..the right path. I know the chances of this happening is nearly impossible as your family should be able to track you down!!! But say this happens.... is it a blessing from God? A lot of us say that there are too many distractions in the modern world to prevent us from devoting to God. But if this happens all your distractions are removed and you have nothing left. You life as you know it will vanish to be replaced with nothing but the Holy book and God. Your are no longer in contact with your family or friends.. they all think you are dead...so you have nothing..no distractions..your life as you know it is gone...stop reading and seriously imagine it for a while..imagine your family and friends do not know where you are and assume you are dead and have given up looking for you. You are alone with strangers and have nothing...but God. You eventually follow his path...if this is the price to pay for eternal paradise would take it? Would you have the guts to wish...to pray.... for this to happen to you in order to live the right path? Your family, friends everyone who cares about will be heart broken. The people who care about you most and you care most about would be devastated, their lives would be changed forever. But you are gauranteed a ticket to paradise at this price. Think about it long and hard.... would you want to follow the right path at this price..in the senario described above? If you choose yes, then do you really truly believe in God....or are you being selfish because you are afflicting your family with immense pain in return for eternal paradise? And what about them? would they join you at paradise?

What is your answer...
YES i wish for the above to happen and pay the price for the right path......
or...
NO i cannot/will not accept the right path at that price......

Friday, November 26, 2004

Parallel Universes

Parallel Universes...do they exist or not? Modern physics support that it does exist. These sites gives a starting point.
http://www.manyuniverses.com/indexH.htm

http://www.bbc.co.uk/science/horizon/2001/parallelunitrans.shtml

From the earliest of mankind the question why and how the universe is created has been a nagging mystery that many human beings ponder sometime or another. At the moment the human brain is not at a stage to fully understand the mechanism of the universe and its constituents as we use only 10-12%. or something like that. but scientists are striving for answers and making headway. The above links are quite interesting to the simple minds that ponder about the universe, its mechanism and constituents.

Wednesday, October 20, 2004

WAAZZZUUUPPPP!!!!!!

This is my first post. Just cking how this works