Can Chat GPT write C code?

Wanted to check if Chat GPT can write C code. So I thought let me choose , what I considered to be a not so commonly used question in programming interviews. Here is my experience. Chat GPT would cleared the interview with flying colors!

Below my questions to Chat GPT are prefixed with VS>. The rest is Chat GPT’s response.

VS> Write a c program to find the day given the date

Sure! Here is a simple program that takes a date as input and prints the day of the week: 

				
					#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
  int day, month, year;
  printf("Enter a date (dd mm yyyy): ");
  scanf("%d %d %d", &day, &month, &year);
  struct tm t = {0}; // Initialize the tm structure
  t.tm_mday = day;
  t.tm_mon = month - 1; // tm_mon is the month index, from 0 to 11
  t.tm_year = year - 1900; // tm_year is the year since 1900
  time_t time = mktime(&t); // Convert the tm structure to a time_t value
  if (time == -1) {
    perror("mktime");
    exit(EXIT_FAILURE);
  }
  // Use the localtime function to convert the time_t value to a tm structure
  struct tm *tm = localtime(&time);
  if (!tm) {
    perror("localtime");
    exit(EXIT_FAILURE);
  }
  printf("The day of the week is %s.\n", buffer);

  return 0;
}
				
			

This program reads the day, month, and year from the user, constructs a tm structure with the input date, converts it to a time_t value using the mktime function, and then converts it back to a tm structure using the localtime function. Finally, it uses the strftime function to format the day of the week as a string and prints it.

I hope this helps! Let me know if you have any questions.

VS> Can you write above without using the time library

				
					#include <stdio.h>
#include <stdlib.h>

// Returns the day of the week (0-6) for the given date
int day_of_week(int day, int month, int year) {
  static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
  year -= month < 3;
  return (year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;
}

int main() {
  int day, month, year;

  printf("Enter a date (dd mm yyyy): ");
  scanf("%d %d %d", &day, &month, &year);

  const char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  printf("The day of the week is %s.\n", days[day_of_week(day, month, year)]);

  return 0;
}
				
			

This program uses a simple formula to calculate the day of the week for a given date. The formula is based on the Zeller’s congruence algorithm, which is a well-known method for finding the day of the week for any date on the Gregorian calendar.

The day_of_week function takes the day, month, and year as input and returns an integer between 0 and 6, representing the day of the week (0 for Sunday, 1 for Monday, and so on). The main function reads the input date from the user and prints the corresponding day of the week using an array of strings.

I hope this helps! Let me know if you have any questions.

VS> What is Zellers congruence?

Zeller’s congruence is an algorithm for calculating the day of the week for any given date on the Gregorian calendar. It is based on the fact that the number of days between a given date and a known reference date (such as March 1, 1900) is a multiple of 7, and therefore the remainder of this number when divided by 7 determines the day of the week.

The formula for Zeller’s congruence is as follows:

Copy code

				
					day_of_week = (q + ((13 * (m + 1)) / 5) + k + (k / 4) + (j / 4) — (2 * j)) % 7
				
			

where:

  • q is the day of the month (1–31)
  • m is the month (1=March, 2=April, …, 10=December, 11=January, 12=February)
  • j is the century (for example, 19 for the 20th century)
  • k is the year of the century (for example, 99 for the year 1999).

The result of the formula is an integer between 0 and 6, representing the day of the week (0 for Sunday, 1 for Monday, and so on).

Zeller’s congruence is a simple and efficient way to find the day of the week for a given date, and it is widely used in programming and other applications. However, it is not as accurate as more complex algorithms, and it may produce incorrect results for dates before the adoption of the Gregorian calendar in different countries.

VS> Can you write above without using Zeller’s congruence ?

Sure! Here is a version of the program that does not use Zeller’s congruence or any other formula to calculate the day of the week:

				
					#include <stdio.h>
#include <stdlib.h>

// Returns the day of the week (0-6) for the given date
int day_of_week(int day, int month, int year) {
  static int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  // Check for leap years
  if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {
    days_in_month[1] = 29;
  }

  // Calculate the number of days since the start of the year
  int days = day;
  for (int i = 0; i < month - 1; i++) {
    days += days_in_month[i];
  }

  // Calculate the number of days since the start of the reference year
  int reference_year = 1900;
  int reference_days = 0;
  for (int i = reference_year; i < year; i++) {
    reference_days += 365;
    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
      reference_days++;  // Add an extra day for leap years
    }
  }

  // Calculate the number of days between the given date and the reference date
  int total_days = reference_days + days;

  // Return the day of the week (0-6)
  return total_days % 7;
}

int main() {
  int day, month, year;

  printf("Enter a date (dd mm yyyy): ");
  scanf("%d %d %d", &day, &month, &year);

  const char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  printf("The day of the week is %s.\n", days[day_of_week(day, month, year)]);

  return 0;
}
				
			

This program uses a brute-force approach to calculate the day of the week for a given date. It first calculates the number of days since the start of the year for the given date, and then calculates the number of days since the start of the reference year (1900 in this case). Finally, it calculates the number of days between the given date and the reference date, and returns the remainder when this number is divided by 7.

This approach is not as efficient as other methods, such as Zeller’s congruence, but it is more accurate and works for any date on the Gregorian calendar.

I hope this helps! Let me know if you have any questions.

Facebook
Twitter
LinkedIn
Email

Recent Posts

Leave a Reply

Your email address will not be published. Required fields are marked *