/* File: avgfile.c This program reads exam scores from a file and processes them to find the average, the maximum, and the minimum. */#include <stdio.h>#define MAX 100float proc_aray(int ex[], int lim, int *pmax, int *pmin);main(){ int max, min, n, lim = 0, exam_scores[MAX]; char infile[15]; float avg; FILE * inp; printf("***Exam Scores: Average, Maximum, Minimum***\n\n"); printf("Input File: "); scanf("%s", infile); inp = fopen(infile, "r"); if (!inp) { printf("Unable to open input file\n"); exit(0); } while (lim < MAX && fscanf(inp, "%d", &n) != EOF) exam_scores[lim++] = n; fclose(inp); if (lim == 0) exit(0); avg = proc_aray(exam_scores, lim, &max, &min); printf("Average = %f, Maximum = %d, Minimum = %d\n", avg, max, min);}/* This function computes the average of an array, the maximum and the minimum. Average is returned, the others are indirectly stored in the calling function. */float proc_aray(int ex[], int lim, int *pmax, int *pmin){ int i, max, min; float sum = 0.0; max = min = ex[0]; for (i = 0; i < lim; i++) { sum += ex[i]; max = ex[i] > max ? ex[i] : max; min = ex[i] < min ? ex[i] : min; } *pmax = max; *pmin = min; return sum / lim;} /* File: card.c Program draws a card each time from a full deck of 52 cards. */ #include <stdio.h> #include <math.h> #define CLUB 0 #define DIAMOND 1 #define HEART 2 #define SPADE 3 #define ACE 1 #define JACK 11 #define QUEEN 12 #define KING 13 main() { int i, d1, card, suit; printf("***Single Card Draw Program***\n\n"); printf("Type a random unsigned integer to start: "); scanf("%d", &i); srand(i); /* seed the random number generator */ for (i = 0; i < 5; i++) { d1 = rand() % 52; /* draw a card */ suit = d1 / 13; /* find the suit 0,1,2,3 */ card = d1 % 13 + 1; /* find the card 1, 2, ..., 13 */ switch (suit) { /* print suit */ case CLUB: printf("Club "); break; case DIAMOND: printf("Diamond "); break; case HEART: printf("Heart "); break; case SPADE: printf("Spade "); break; } switch (card) { /* print the card within a suit */ case ACE: printf("Ace"); break; case JACK: printf("Jack"); break; case QUEEN: printf("Queen"); break; case KING: printf("King"); break; default: printf("%d", card); } printf("\n"); } } /* File: ccopy.c This program copies an input file to an output file one character at a time. Standard files are not allowed. */ #include <stdio.h> main() { FILE *input, *output; char infile[15], outfile[15]; signed char ch; printf("***File Copy Program - Character I/O***\n\n"); printf("Input file : "); scanf("%s", infile); printf("Output file : "); scanf("%s", outfile); input = fopen(infile, "r"); if (input == NULL) { printf("*** Can't open input file ***\n"); exit(0); } output = fopen(outfile, "w"); if (output == NULL) { printf("*** Can't open output file ***\n"); exit(0); } while ((ch = getc(input)) != EOF) putc(ch, output); fclose(input); fclose(output); printf("File copy completed\n"); } /* File: cntdigits.c This program reads characters from a file stream and counts the number of occurrences of each digit. */ #define MAX 10 #include <stdio.h> #include <ctype.h> /* for isdigit() */ main() { int digit_freq[MAX],i; signed char ch; FILE * fin; printf("***Digit Occurrence Counter***\n\n"); /* initialize the array */ for (i = 0; i < MAX; i++) digit_freq[i] = 0; fin = fopen("test.doc", "r"); /* open input file */ if (!fin) { /* if fin is a NULL pointer */ printf("Unable to open input file: test.doc\n"); exit(0); /* exit program */ } while ((ch = getc(fin)) != EOF) { /* read a character into ch */ if (isdigit(ch)) /* if ch is a digit */ digit_freq[ch - '0']++; /* increment count for digit ch */ } fclose(fin); /* summarize */ for (i = 0; i < MAX; i++) printf("There are %d occurrences of %d in the input\n", digit_freq[i],i); } /* File: dice.c Program throws a single dice repeatedly. */ #include <stdio.h> #include <math.h> main() { int i, d1; printf("***Single Dice Throw Program***\n\n"); printf("Type a random unsigned integer to start: "); scanf("%d", &i); srand(i); for (i = 0; i < 5; i++) { d1 = rand() % 6 + 1; printf("throw = %d\n", d1); } }/* File: ident.c Program reads characters one at a time until EOF. It prints out each identifier in the input text and counts the total number of identifiers. It ignores white space except as a delimiter for an identifier. An identifier starts with an alphabetic letter and may be followed by any number of letters or digits. All other characters are considered illegal.*/#include <stdio.h>#include <ctype.h>main(){ int cnt = 0; signed char c; printf("***Print Identifiers***\n\n"); printf("Type text, terminate with EOF ^Z or ^D)\n"); c = getchar(); while (c != EOF) { while (isspace(c)) /* skip leading white space */ c = getchar(); if (isalpha(c)) { /* if a word starts with a letter */ while (isalnum(c)) { /* while c is letter or digit */ putchar(c); /* print c */ c = getchar(); /* read next char */ } putchar('\n'); /* end identifier with a newline */ cnt++; /* increment cnt */ } else if (c == EOF) /* if end of file */ break; /* break out of loop */ else { /* otherwise, it is an illegal char */ printf("Illegal character %c\n", c); c = getchar(); } } printf("Number of Identifiers = %d\n", cnt);} /* File: rand.c Program uses random number generator to print some random numbers. */ #include <stdio.h> #include <math.h> main() { int i; int x; for (i = 0; i < 5; i++) { x = rand(); printf("%d\n", x); } } /* File sqrt3.c Program computes and prints square roots of numbers randomly generated. */ #include <stdio.h> #include <math.h> main() { int i; double x; printf("***Square Root Program - Random Numbers***\n\n"); for (i = 0; i < 5; i++) { x = rand(); printf("Sq.Rt. of %f is %f\n", x, sqrt(x)); } }