NOTE: DO NOT USE ARRAYS/POINTERS/STRING (C PROGRAMMING) MUST USE USER DEFINED FU
ID: 3538917 • Letter: N
Question
NOTE: DO NOT USE ARRAYS/POINTERS/STRING
(C PROGRAMMING)
MUST USE USER DEFINED FUNCTION, CAN USE LOOPS/STATEMENTS/SWITCH/IF/ELSE/LOGICAL OPERATORS AND RELATIONAL OPERATORS ONLY!
I NEED THE CODE, WILL AWARD FULL POINTS AND STARS.
Problem: Given a year, determine when the next presidential election will take place. In the United States, presidential elections take place every four years. The last election day took place on November 6th, 2012. An election day will take place on the first Tuesday following the first Monday in November.
Example Execution #1:
Example Execution #2:
Example Execution #3:
Example Execution #4:
Example Execution #5:
Explanation / Answer
#include<stdio.h>
int findNextElectionYear(int year){
if(year%4 == 0) return year;
int temp = 4 - (year % 4);
return temp + year;
}
int getDayOn6November(int year){
int difference = year - 2012;
int temp;
int day;
if(difference > 0){
temp = difference/4;
temp += difference;
day = (temp + 2) % 7;
}
else if(difference < 0){
difference = -1 * difference;
temp = difference/4;
if(difference % 4 != 0) temp++;
temp += difference;
day = (((2 - temp)%7) + 7) % 7;
}
else {
day = 2;
}
return day;
}
int getFirstTuesday(int dayOn6Nov){
int dayOn1Nov = (dayOn6Nov + 2) % 7;
int temp = (9 - dayOn1Nov)% 7;
temp = 1 + temp;
return temp;
}
int main(){
int year;
printf("Enter the year: ");
scanf("%d",&year);
if(year < 1788 || year > 2100){
printf("Error! Year must be in the range [1788 - 2100]!");
exit(0);
}
int nextElectionYear = findNextElectionYear(year);
int dayOn6Nov = getDayOn6November(nextElectionYear);
int nextElectionDate = getFirstTuesday(dayOn6Nov);
printf("The next presidential election day will be Tuesday 11/%d/%d",nextElectionDate,nextElectionYear);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.