/* File: ar2ptr.c
Other Source Files: ar2util.c
Header Files: ar2def.h, ar2util.h
Program shows relations between arrays and pointers for 2 dimensional
arrays.
*/
#include <stdio.h>
#define ROW 2
#define COL 3
print2aray(int x[][COL], int r, int c);
main()
{ int i, j, *intptr, a[ROW][COL] =
{ {12, 24, 29}, {23, 57, 19} };
printf("***2D Arrays, Pointers ***\n\n");
intptr = *a;
printf("array (row) pointer a = %u, *a = %u\n", a, *a);
for (i = 0; i < ROW; i++) {
printf("a + %d = %u\n", i, a + i);
for (j = 0; j < COL; j++) {
printf("*a + COL * %d + %d = %u; intptr = %u\n",
i, j, *a + COL * i + j, intptr);
printf("&a[%d][%d] = %u\n", i, j, &a[i][j]);
printf("a[%d][%d] = %d; *(*(a + %d) + %d) = %d\n",
i, j, a[i][j],
i, j, *(*(a + i) + j));
intptr++;
}
}
print2aray(a, ROW, COL);
}
/* This Function prints a two dimensional integer array. */
print2aray(int x[][COL], int r, int c)
{ int i, j;
printf("\nThe two dimensional array is:\n\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
printf("%d ", x[i][j]);
printf("\n");
}
}
/* File: gauss.c */
#include <stdio.h>
#include "gauss.h"
/* File: gauss.c
Header Files: gauss.h
This program solves a number of simultaneous linear algebraic
equations using the Gauss elimination method. The process repeats
itself until number of equations is zero.
*/
main()
{ double a[MAX][MAX + 1]; /* coefficients and right hand side */
double x[MAX]; /* solution */
int n; /* number of equations */
status soln; /* status of solution, OK or ERROR */
printf("***Simultaneous Equations***\n\n");
while (n = getcoeffs(a)) {
printf("\nOriginal equations are:\n");
#ifdef DEBUG
pr2adbl(a, n);
#endif
soln = gauss(a, x, n);
#ifdef DEBUG
printf("\nEquations after Gauss Transformation are:\n");
pr2adbl(a, n);
#endif
if (soln == OK) {
printf("\nSolution is:\n");
pr1adbl(x, n);
}
else printf("Equations cannot be solved\n");
}
}
/* Implements the Gauss method to transform equations to
an upper triangular form.
*/
status uptriangle(double a[][MAX + 1], int n)
{ int i, j, k;
for (k = 0; k < n; k++) {
if (findpivot(a, k, n) == OK)
process_col(a, k, n);
else
return ERROR;
}
return OK;
}
/* Zeros out coefficients in column k below the main diagonal. */
void process_col(double a[][MAX + 1], int k, int n)
{ int i, j;
double m;
for (i = k + 1; i < n; i++) {
m = -a[i][k] / a[k][k];
for (j = k; j <= n; j++)
a[i][j] += m * a[k][j];
#ifdef DEBUG
printf("Multiplier for row %d is %6.2f\n", i, m);
pr2adbl(a, n);
#endif
}
}
/* Finds a non-zero pivot element in column k and row with
index >= k.
*/
status findpivot(double a[][MAX + 1], int k, int n)
{ int j;
void swaprows();
if (a[k][k] == 0) {
j = findnonzero(a, k, n);
if (j < 0)
return ERROR;
else
swaprows(a, k, j, n);
#ifdef DEBUG
printf("Rows %d and %d swapped\n", k, j);
#endif
}
return OK;
}
/* Scans the rows with index >= k for the first non-zero element
in the kth column of the array 'a' of size n.
*/
int findnonzero(double a[][MAX + 1], int k, int n)
{ int i;
for (i = k; i < n; i++)
if (a[i][k])
return(i);
return(-1);
}
/* Swaps the kth and the jth rows in the array 'a' with n rows. */
void swaprows(double a[][MAX + 1], int k, int j, int n)
{ int i;
double temp;
for (i = k; i <= n; i++) {
temp = a[k][i];
a[k][i] = a[j][i];
a[j][i] = temp;
}
}
/* File: gauss.c - continued */
/* Transforms equations to upper triangular form using Gauss
method. Then, solves equations, one at a time.
*/
status gauss(double a[][MAX + 1], double x[], int n)
{ int i, j;
double sum;
if (uptriangle(a, n) == ERROR) {
printf("Dependent equations - cannot be solved\n");
return ERROR;
}
for (i = n - 1; i >= 0; i--) {
sum = 0;
for (j = i + 1; j <= n - 1; j++)
sum += a[i][j] * x[j];
if (a[i][i])
x[i] = (a[i][n] - sum) / a[i][i];
else
return ERROR;
}
return OK;
}
/* File: gauss.c - continued */
/* Function gets the coefficients and the right hand side of equations.
*/
int getcoeffs(double a[][MAX + 1])
{ int i, j, n;
printf("Number of equations, zero to quit: ");
scanf("%d", &n);
if (n)
printf("Type coefficients and right side of each row\n");
for (i = 0; i < n; i++) {
printf("Row %d: ", i);
for (j = 0; j <= n; j++)
scanf("%lf", &a[i][j]);
}
return n;
}
/* Prints coefficients and right side of equations */
void pr2adbl(double a[][MAX + 1], int n)
{ int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j <= n; j++)
printf("%10.2f ", a[i][j]);
printf("\n");
}
}
/* Prints the solution array */
void pr1adbl(double x[], int n)
{ int i;
for (i = 0; i < n; i++)
printf("%10.2f\n", x[i]);
}
/* File: gauss.h */
typedef enum {ERROR, OK} status;
#define DEBUG
#define MAX 10 /* maximum number of equations */
status uptriangle(double a[][MAX + 1], int n);
void process_col(double a[][MAX + 1], int k, int n);
status findpivot(double a[][MAX + 1], int k, int n);
int findnonzero(double a[][MAX + 1], int k, int n);
void swaprows(double a[][MAX + 1], int k, int j, int n);
status gauss(double a[][MAX + 1], double x[], int n);
int getcoeffs(double a[][MAX + 1]);
void pr2adbl(double a[][MAX + 1], int n);
void pr1adbl(double x[], int n);
/* File: pay2rec.c
Program calculates and stores payroll data for a number of id's.
The program uses a one dimensional array for id's, and a two
dimensional array for the rest of the pay record. The first column
is hours, the second is rate, the third is regular pay, the fourth
is overtime pay, and the fifth is total pay.
*/
#include <stdio.h>
#define MAX 10
#define REG_LIMIT 40
#define OT_FACTOR 1.5
#define HRS 0
#define RATE 1
#define REG 2
#define OVER 3
#define TOT 4
int get2data(int id[], float payrec[][TOT + 1], int lim);
void calc2pay(float payrec[][TOT + 1], int n);
void print2data(int id[], float payrec[][TOT + 1], int n);
main()
{ int n = 0, id[MAX];
float payrec[MAX][TOT + 1];
printf("***Payroll Program - Records in 2 D arrays***\n\n");
n = get2data(id, payrec, MAX);
calc2pay(payrec, n);
print2data(id, payrec, n);
}
/* File: pay2rec.c - continued */
/* Gets id's in one array, and the rest of input data records
in a two dimensional array.
*/
int get2data(int id[], float payrec[][TOT + 1], int lim)
{ int n = 0;
float x;
while (n < lim) {
printf("ID <zero to quit>: ");
scanf("%d", &id[n]);
if (id[n] <= 0)
return n;
printf("Hours Worked: ");
scanf("%f", &x);
payrec[n][HRS] = x;
printf("Rate of Pay: ");
scanf("%f", &x);
payrec[n][RATE] = x;
n++;
}
if (n == lim) {
printf("Table full, processing data\n");
return n;
}
}
/* Calculates pay for each id record in a two dimensional array. */
void calc2pay(float payrec[][TOT + 1], int n)
{ int i;
for (i = 0; i < n; i++) {
if (payrec[i][HRS] <= REG_LIMIT) {
payrec[i][REG] = payrec[i][HRS] * payrec[i][RATE];
payrec[i][OVER] = 0;
}
else {
payrec[i][REG] = REG_LIMIT * payrec[i][RATE];
payrec[i][OVER] = (payrec[i][HRS] - REG_LIMIT) *
OT_FACTOR * payrec[i][RATE];
}
payrec[i][TOT] = payrec[i][REG] + payrec[i][OVER];
}
}
/* Prints a table of payroll data for all id's. Id's in one array,
and the rest of the records in a two dim. array.
*/
void print2data(int id[], float payrec[][TOT + 1], int n)
{ int i, j;
printf("***PAYROLL: FINAL REPORT***\n\n");
printf("%10s%10s%10s%10s%10s%10s\n", "ID", "HRS",
"RATE", "REG", "OVER", "TOT");
for (i = 0; i < n; i++) {
printf("%10d", id[i]);
for (j = 0; j <= TOT; j++)
printf("%10.2f", payrec[i][j]);
printf("\n");
}
}
/* File: ptraray.c
This program uses an array of pointers. Elements of the array
point to strings. The pointers are reordered so that they
point to the strings in sorted order. Unsorted and sorted
strings are printed out.
*/
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define MAX 10 /* max number of names */
#define SIZE 31 /* size of names plus one for NULL */
void sortptrs(char * nameptr[], int n);
main()
{ int i; /* counter */
int n; /* number of names read */
char names[MAX][SIZE]; /* 2-d array of names */
char *nameptr[MAX]; /* array of ptrs - used to point to names */
printf("***Arrays of Pointers - Sorting by Pointers***\n\n");
/* read the names into the 2-d array */
printf("Enter one name per line, ");
printf("EOF to terminate\n");
for (n = 0; gets(names[n]) && n < MAX; n++)
nameptr[n] = names[n]; /* assign string pointer */
/* to a char pointer in the */
/* array of pointers. */
if (n == MAX)
printf("\n***Only %d names allowed***\n", MAX);
printf("The unsorted names are:\n");
/* print the names */
for (i = 0; i < n; i++)
puts(names[i]); /* access names in stored array.*/
sortptrs(nameptr, n); /* sort pointers */
printf("The sorted names are:\n");
for (i = 0; i < n; i++) /* print sorted names, */
puts(nameptr[i]); /* accessed via array of pointers. */
}
/* File: ptraray.c - continued */
/* The elements of the array of pointers nameptr[] point to
strings. The pointer array is reordered so the pointers
point to strings in sorted order. The function uses selection
sort algorithm.
*/
void sortptrs(char * nameptr[], int n)
{ int i, eff_size, maxpos = 0;
char *tmpptr;
for (eff_size = n; eff_size > 1; eff_size--) {
for (i = 0; i < eff_size; i++)
if (strcmp(nameptr[i],nameptr[maxpos]) > 0)
maxpos = i;
tmpptr = nameptr[maxpos];
nameptr[maxpos] = nameptr[eff_size-1];
nameptr[eff_size-1] = tmpptr;
}
}
/* File: strsort.c
Other Source Files: strtab.c
Header Files: strtab.h
This program sorts a set of strings in a two dimensional array.
It prints the unsorted and the sorted set of strings.
*/
#include <stdio.h>
#define MAX 10
#include "strtab.h"
main()
{ int n;
char names[MAX][SIZE] = { "John Jones", "Sheila Smith",
"John Smith", "Helen Kent"};
printf("***String Array - unsorted and sorted***\n\n");
printf("Unsorted ");
printstrtab(names, 4);
sortstrtab(names, 4);
printf("Sorted ");
printstrtab(names, 4);
}
/* File: strsrch.c
Other Source Files: strtab.c
Header Files: strtab.h
This program searches for a string (key) in a set of strings
in a two dimensional array. It prints the index where key is found.
It prints -1, if the string is not found.
*/
#include <stdio.h>
#define MAX 10
#include "strtab.h"
main()
{ int k;
char names[MAX][SIZE] = { "John Jones", "Sheila Smith",
"John Smith", "Helen Kent"};
printf("***String Search for John Smith***\n\n");
k = srchstrtab(names, 4, "John Smith");
printf("John Smith found at index %d\n", k);
}
/* File: strtab.h */
#define SIZE 31 /* maximum size of a name plus a NULL */
void printstrtab(char strtab[][SIZE], int n);
/* File: names.c
Other Source Files: strtab.c
Header Files: strtab.h
This program reads a set of names or strings into a two
dimensional array. It then prints out the names.
*/
#include <stdio.h>
#define MAX 10
#include "strtab.h"
main()
{ int n; /* number of names */
char names[MAX][SIZE]; /* 2-d array of names */
printf("***Table of Strings - Names***\n\n");
printf("Enter one name per line, EOF to terminate\n");
for (n = 0; (n < MAX) && gets(names[n]); n++)
;
if (n == MAX)
printf("\n**Table full - input terminated\n");
printstrtab(names, n);
}
/* File: strtab.c */
#include <stdio.h>
#include "strtab.h"
/* Prints n strings in the array strtab[][]. */
void printstrtab(char strtab[][SIZE], int n)
{ int k;
printf("Names are:\n");
for (k = 0; k < n; k++)
puts(strtab[k]);
}
/* File: strtab.c - continued */
#include <string.h>
/* Searches a string table strtab[][] of size n for a string key. */
int srchstrtab(char strtab[][SIZE], int n, char key[])
{ int i;
for (i = 0; i < n; i++)
if (strcmp(strtab[i], key) == 0)
return i;
return -1;
}
/* File: strtab.c - continued */
/* Sorts an array of strings. The number of strings in the
array is lim.
*/
void sortstrtab(char strtab[][SIZE], int lim)
{ int i, eff_size, maxpos = 0;
char tmp[SIZE];
for (eff_size = lim; eff_size > 1; eff_size--) {
for (i = 0; i < eff_size; i++)
if (strcmp(strtab[i], strtab[maxpos]) > 0)
maxpos = i;
strcpy(tmp, strtab[maxpos]);
strcpy(strtab[maxpos], strtab[eff_size-1]);
strcpy(strtab[eff_size - 1], tmp);
}
}
/* File: wtdavg.c
Other Source Files: avg.c
Header Files: avg.h
This program computes weighted averages for a set of exam scores for
several individuals. The program reads scores from a file, computes
weighted averages for each individual, prints out a table of scores,
and prints averages for each of the exams and for the weighted average.
*/
#include <stdio.h>
#define MAX 20
#define COLS 5
int getwts(float wts[]);
FILE *openinfile(void);
int read_scores(int ex[][COLS], int lim, int nexs);
void wtd_avg(int ex[][COLS], int lim, int nexs, float wts[]);
void avg_scores(int ex[][COLS], int avg[], int lim, int nexs);
void print_scores(int ex[][COLS], int lim, int nexs);
void print_avgs(int avg[], int nexs);
main()
{ int no_of_stds, no_of_exams;
int avg[COLS];
int scores[MAX][COLS];
float wts[COLS];
printf("***Weighted Average of Scores***\n\n");
no_of_exams = getwts(wts);
no_of_stds = read_scores(scores, MAX, no_of_exams);
wtd_avg(scores, no_of_stds, no_of_exams, wts);
print_scores(scores, no_of_stds, no_of_exams);
avg_scores(scores, avg, no_of_stds, no_of_exams);
print_avgs(avg, no_of_exams);
}
/* File: wtdavg.c - continued */
/* Gets the number of exams and weights for the exams; flushes
the input buffer and returns the number of exams.
*/
int getwts(float wts[])
{ int i, n;
printf("Number of exams: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("Percent Weight for Exam %d: ", i);
scanf("%f", &wts[i]);
}
while (getchar() != '\n')
;
return n;
}
/* File: wtdavg.c - continued */
/* Opens the input file and returns the file pointer. */
FILE *openinfile(void)
{ FILE *fp;
char infile[25];
printf("Input File, RETURN to quit: ");
while (gets(infile)) {
if (!*infile) exit(0); /* empty string, exit */
fp = fopen(infile, "r");
if (!fp) { /* no such file, continue */
printf("Unable to open input file - retype\n");
continue;
}
else return fp; /* file opened, return fp */
}
exit(0); /* end of file typed, exit */
}
/* Opens the input file and reads scores for nexs exams; returns
the number of individual student records.
*/
int read_scores(int ex[][COLS], int stds, int nexs)
{ int row, col, n, x;
FILE * fp;
fp = openinfile();
for (row = 0; row < stds; row++)
for (col = 0; col <= nexs; col++) {
x = fscanf(fp, "%d", &n);
if (x == EOF) {
fclose(fp);
return row;
}
ex[row][col] = n;
}
fclose(fp);
return row;
}
/* File: wtdavg.c - continued */
/* Computes the weighted average of the exam scores in ex[][] for
lim individuals, nexs number of exams, and weights given by wts[].
*/
void wtd_avg(int ex[][COLS], int lim, int nexs, float wts[])
{ int i, j;
float wtdavg;
for (i = 0; i < lim; i++) {
wtdavg = 0.0;
for (j = 1; j <= nexs; j++)
wtdavg += ex[i][j] * wts[j] / 100.0;
ex[i][nexs + 1] = (int) (wtdavg + 0.5);
}
}
/* Averages exam and weighted average scores. */
void avg_scores(int ex[][COLS], int avg[], int lim, int nexs)
{ int i, j;
for (j = 1; j <= nexs + 1; j++) {
avg[j] = 0;
for (i = 0; i < lim; i++)
avg[j] += ex[i][j];
avg[j] = (int) (0.5 + (float) avg[j] / (float) lim);
}
}
/* File: wtdavg.c - continued */
/* Prints the scores for exams and the weighted average. */
void print_scores(int ex[][COLS], int lim, int nexs)
{ int i, j;
printf("ID #\t");
for (j = 1; j <= nexs; j++)
printf("Ex%d\t", j); /* print the headings */
printf("WtdAvg\n");
for (i = 0; i < lim; i++) { /* print the scores and wtd avg */
for (j = 0; j <= nexs + 1; j++)
printf("%d\t", ex[i][j]);
printf("\n");
}
}
/* Prints the averages of exams and the average of the weighted average
of exams.
*/
void print_avgs(int avg[], int nexs)
{ int i;
for (i = 1; i <= nexs; i++)
printf("Average for Exam %d = %d\n", i, avg[i]);
printf("Average of the weighted average = %d\n", avg[nexs + 1]);
}