1. Write the function day_of_yrar(month,day,year) so that the day on which the function returns to be determined by these three parameters is the day of the year (integer between 1 and 365).
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int day(int mounth, int day,int year); int main() { int mounth, days, year,sum; printf("Input date: month/day/year"); scanf("%d / %d / %d", &mounth, &days, &year); printf("The day is %d days of %d\n", day(mounth,days,year), year); return 0; } int day(int mounth, int day,int year) { int num_days[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int day_count = 0, i; for (i = 1; i < mounth; i++) { day_count += num_days[i - 1]; } if (year % 4 == 0 && mounth > 2) { day_count++; } return day_count + day; }
2. /* Writes the function num_digits(n) so that the function returns the number of digits in the positive integer n. Tip: In order to determine the number of numbers in n, divide the number by 10 repeatedly. When n=0, the number of divisions indicates the number of numbers that n initially owned.
/*Write the function num_digits(n) so that the function returns the number of numbers in the positive integer n. Tip: For confirmation n When n=0, the number of divisions indicates the origin of n. Number of numbers owned.*/ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int function(int i); int main() { int i; printf("Enter a positive X Integer:"); scanf("%d", &i); return function(i); } int function(int i) { int n = 0; do { i /= 10; n++; } while (i != 0); return n; }
3. Write the function digit(n, k), so that the function returns the number k in the positive integer n (from the right). For example, digita (829, 1) returns 9, and digita (829, 3) returns 8. If K is greater than the number of numbers contained in n, return 0
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int function(int i ,int k); int main() { int i,k; printf("Enter a positive X Integer:"); scanf("%d %d", &i,&k); return function(i,k); } int function(int i,int k) { int n = 0,j=0, a[100] = { 0 }; do { a[j++] = i % 10; i /= 10; n++; } while (i != 0); if (k > n) { return 0; } return a[k-1]; }
4. Quick Sorting Algorithms
/* Sorts an arry of integers using Quicksort algorithm */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define N 10 void quicksort(int a[], int low, int high); int split(int a[], int low, int high); int main(void) { int a[N], i; printf("Enter %d numbers to be sorted: ", N); for (i = 0; i < N; i++) { scanf("%d", &a[i]); } quicksort(a, 0,N - 1); printf("In sorted order: "); for (i = 0; i < N; i++) { printf("%d ", a[i]); } printf("\n"); return 0; } void quicksort(int a[], int low, int high) { int middle; if (low >= high) { return;} middle = split(a, low, high); quicksort(a, low, middle-1); quicksort(a, middle + 1, high); } int split(int a[], int low, int high) { int part_element = a[low]; for (;;) { while (low < high && part_element <= a[high]) { high--; } if (low >= high) { break; } a[low++] = a[high]; while (low < high && a[low] <= part_element) { low++; } if (low >= high) { break; } a[high--] = a[low]; } a[high] = part_element; return high; }
5. Time in seconds
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void split_time(long total_sec, int* hr, int* min, int* sec); int main(void) { long total_sec; scanf("%d", &total_sec); int* hr, * min, * sec, hour = 0, minute = 0, second = 0; int day = 0; if (total_sec < 24 * 3600) { ; } else { day = total_sec/(24 * 3600); total_sec = total_sec - day * 24 * 3600; } split_time(total_sec, &hour, &minute, &second); printf("Output current corresponding time:%d day%2.2d:%2.2d:%2.2d\n", day,hour, minute, second); return 0; } void split_time(long total_sec, int* hr, int* min, int* sec) { *hr = total_sec / 3600; *min = (total_sec-*hr*3600)/60; *sec = total_sec - *hr * 3600 - *min * 60; }
6. Find the first and second largest numbers in the array and display them.
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define N 10 void find_largest(int a[N],int *largest,int *second_largest); int main(void) { int a[N], * largest, * second_largest, max, second_max; for (int i = 0; i < N; i++) { scanf("%d", &a[i]); } find_largest( a, &max, &second_max); printf("largesr==%d second==%d\n", max, second_max); return 0; } void find_largest( int a[N], int *largest, int *second_largest) { *largest = *second_largest = a[0]; for (int i = 1; i < N; i++) { if (a[i] > * largest) { *largest = a[i]; } } for (int i = 1; i < N; i++) { if (*second_largest < a[i] && a[i] < *largest) { *second_largest = a[i]; } } }
7. The Days of the Year
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void split_date(int day_of_year, int* year, int* month, int* day); int main(void) { int num_year, num_month, num_day, * year, * month, * day, day_of_year=0; printf("Enter date: mm/dd/yy "); scanf("%d/%d/%d", &num_year, &num_month, &num_day); year = & num_year; month = & num_month; day = & num_day; split_date(day_of_year, &num_year, &num_month, &num_day); return 0; } void split_date(int day_of_year, int *year, int* month, int* day) { int num_days[] = { 31, 28, 31, 30, 31, 30, \ 31, 31, 30, 31, 30, 31 }; int i; for (i = 1; i < *month; i++) { day_of_year += num_days[i-1]; } if (*year % 4 == 0) { day_of_year++; } printf("day_of_year:%d\n", day_of_year + *day); }
8. Pass in an array of length a and return the pointer to the largest element of the array*p
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int* find_largest(int a[], int n); int main(void) { int a[] = { 3, 28, 21, 32, 45, 55, 65, 32,5, 621, 80, 31 }; printf("%d\n", *find_largest(a, sizeof(a) / sizeof(a[0]))); return 0; } int* find_largest(int a[], int n) { int* p,max=a[0]; p = &max; for (int i = 0; i < n; i++) { if (max < a[i]) { max = a[i]; } else { ; } } return p; }