Question

In: Computer Science

C Programming Please write a single function and not the whole program Can you please provide...

C Programming Please write a single function and not the whole program

Can you please provide comments and explanations for all questions. Thank you

Write a single function for the following:

void split_date(int day_of_year, int year, int *month, int *day);

            day_of_year is an integer between 1 and 366, specifying a particular day within the year

designated by the parameter year. Month and day point to variables in which the function
will store the equivalent month (1 – 12) and day within the month (1 – 31)

The question is trying to ask you to write a single function in C that takes a day of the year (1-366) and calculates which month and which day of the month corresponds to that day of in the month. So a day of the year of let's 15 would return 1 (for the month - January) and 15 for the day of the month. The second parameter (year) is needed because leap years have 366 days and non-leap years have 365 days.

Solutions

Expert Solution

int check_Leap(int year)
{
if( (year%4 == 0 && year%100 != 0) || (year%400 == 0) )
return 1;       //is a leap year
else
return 0;       //is not a leap year
}

void split_date(int day_of_year, int year, int *month, int *day)
{
   //At first check whether the given year is a leap year or not
   //We are storing 0 in is_Leap_Year variable if it is not a leap year
   //else 1 for a leap year using function check_Leap()
   int is_Leap_Year = check_Leap(year);
  
   if (day_of_year <1 || day_of_year>366)
       printf("Invalid Day of Year\n");  
       //if user enters day_of_year out of the range
  
   else if(year<1000 || year>9999)
       printf("Invalid year\n");
       //if user enters year out of the range
  
   else
   {
       //initializing total number of days in every month in an array
       int month_days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
      
       //changing total number of days in February as 29
       //if year is a leap year
       month_days[1] += is_Leap_Year;      
       //e.g. for 2016 as leap year, month_days[1] = 28 + 1
      
       int i, d = day_of_year;
      
       for(i=0; d > month_days[i]; i++)
           d -= month_days[i];
       //here in this for loop, total number of days of every month is
       //being subtracted from day_of_year until day_of_year becomes
       //less than total number of days in a particular month.
       //after loop ends, i stores the actual value of month
       //so we store the month's answer
      
       *month = ++i;
       //since the array month_days[] has 0 index counting,
       //so February has indexing 1. We incremented i to store 2 for
       //February in month.
      
       *day = d;
       //the remaining value of d holds the actual answer for date
       //when d becomes less than total number of days in a month
       //then d is the date we are looking for.
      
   }
}


INPUT: day_of_year: 234       year: 2008
OUTPUT: *day = 21 *month = 8

Explanation:
Since 2008 is a leap year, month_days[] becomes
month_days[] = {31,29,31,30,31,30,31,31,30,31,30,31}

Now checking for loop:
d = 234;
for(i=0; d > month_days[i]; i++)
   d -= month_days[i];

#Pass 0:
234 > month_days[0] ----> 234>31 ---> YES
Subtract: d = 234-31 = 203
i++;

#Pass 1:
203 > month_days[1] ----> 203>29 ---> YES
Subtract: d = 203-29 = 174
i++;

#Pass 2:
174 > month_days[2] ----> 174>31 ---> YES
Subtract: d = 174-31 = 143
i++;

#Pass 3:
143 > month_days[3] ----> 143>30 ---> YES
Subtract: d = 143-30 = 113
i++;

#Pass 4:
113 > month_days[4] ----> 113>31 ---> YES
Subtract: d = 113-31 = 82
i++;

#Pass 5:
82 > month_days[5] ----> 82>30 ---> YES
Subtract: d = 82-30 = 52

#Pass 6:
52 > month_days[6] ----> 52>31 ---> YES
Subtract: d = 52-31 = 21
i++;

#Pass 7:
21 > month_days[7] ----> 21>31 ---> NO
EXIT from the Loop

So day becomes 21 and month becomes ++i = 8.


Related Solutions

Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Please write program in c++ with using pointer. create a function that can find the number...
Please write program in c++ with using pointer. create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer. example 8 -1 2 2 1 9 2 4 0 output: 2
Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
use linux or c program. please provide the answer in details. Write a program that will...
use linux or c program. please provide the answer in details. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally...
Please make a C program named: rotate.c This code will contain a single function (and the...
Please make a C program named: rotate.c This code will contain a single function (and the include directive to your header file) to perform the rotation operation using the two integer operands passed to this function. The first integer operand is the number to be rotated. The second integer operand is the number of bit positions that the first operand must be rotated by. EXAMPLE: Enter a 32-bit number (>= 1 and <= 4294967295, inclusively): 1 Enter the number of...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT