1.5
Project 8:
#include <stdio.h>
#define MAX 10
main()
{
char string[MAX+1];
int count;
int words;
count = MAX;
words = 0;
for (count = 0; count < MAX; count++)
string[count] = getchar();
puts(string);
for (count = 0; count < MAX; count++)
{
if ((string[count] == '.') || (string[count] == '!') || (string[count] == '?') || (string[count] == ',') || (string[count] == ' '))
{
words++;
}
}
printf("%d", words);
}
Project 9:
#include <stdio.h>
#define ROW 2
#define COLUMN 2
main()
{
int matrix1[2][2];
int matrix2[2][2];
int sum[2][2];
int row;
int column;
for (row = 0; row < ROW; row++)
{
for (column = 0; column < COLUMN; column++)
{
scanf("%d", &matrix1[row][column]);
printf("%d", matrix1[row][column]);
}
}
for (row = 0; row < ROW; row++)
{
for (column = 0; column < COLUMN; column++)
{
scanf("%d", &matrix2[row][column]);
printf("%d", matrix1[row][column]);
}
}
puts("the sum matrix is");
for (row = 0; row < ROW; row++)
{
for (column = 0; column < COLUMN; column++)
{
sum[row][column] = (matrix1[row][column] + matrix2[row][column]);
printf("%d", sum[row][column]);
}
}
}
Project 10:
#include <stdio.h>
/* This program will encrypt a line of text*/
void main( void )
{
char line[81];
int key[]={'k','e','y'};
int length=2;
int k=0,i;
printf( ``Please type in a line of text.\n'' );
printf( ``When you are done, press 'return'.\n\n'' );
gets( line );
printf( ``\nThe line entered was: %s\n\n'', line );
for (i=0; line[i]!='\0'; ++i)
{
line[i]=line[i] + key[k];
if (line[i] > 127)
line[i] = line[i] - 127;
++k;
if (k>length)
k=0;
}
printf( ``The cipher line is: %s\n'',line );
}
/* Ascii code based on the existing equilants a97,
b98,c99,d100,e101,f102,g103,
h104,i105,j106, k107, l108, m109, n110, o111, p112, q113,
r114,s115, t116,
u117,v118,w119,x120,y121,z122 */
Project 11:
/* This Program will read in a file to calculate taxes. */
#include <stdio.h>
/* Function to calculate taxes. */
double calculate_tax (double income)
{
double result;
if (income <=0.0)
result=0.0;
else if (income <= 3000.0)
result = income * .11;
else if (income <= 28000.0)
result = 330.0 + (( income - 3000.0) * .15 );
else if (income <= 45000.0)
result = 4080.0 + (( income - 28000.0)* .28 );
else if (income <= 90000.0)
result = 8840.0 + (( income - 45000.0) * .35 );
if (income >90000)
result = 24590.0 + (( income - 90000.0) * .385 );
return (result);
}
main()
{
double calculate_tax (double income);
FILE *in, *out;
/* char s = to the personal ID; int t = to the type of return*/
char s;
int t;
double income;
double taxes;
if ( (in = fopen (``input.txt'', ``r'')) == NULL )
printf (``Could not open input file for reading!!\n\n'');
else if ( (out = fopen (``output.txt'', ``w'')) == NULL )
printf (``Could not open output file for writing!!\n\n'');
else
{
while ( !feof(in) )
{
fscanf(in, ``%c %i %lf\n'',&s,&t,&income);
if (t==1)
{
taxes = calculate_tax (income);
fprintf( out, ``%c %i %9.2lf 9.2lf\n'',s,t,income,taxes);
}
else
fprintf( out, ``%c %i no calculation for this
return type.\n'',s,t)\\n'',s,t);
}
}
return(0);
}
Project 12:
/* This Program will read in student grades and calculate the
average, median, highest and lowest grade. */
#include <stdio.h>
#include <malloc.h>
/* Function to find the minimum in an array */
int minimum (int records,int* values)
{
int minimum_value = 0;
int i;
minimum_value = values[0];
for ( i = 0; i < records; ++i )
if (values[i] < minimum_value )
minimum_value = values[i];
return (minimum_value);
}
/* Function to find the highest in an array */
int highest (int records,int* values)
{
int highest_value = 0;
int i;
highest_value = values[0];
for ( i = 0; i < records; ++i )
if (values[i] > highest_value )
highest_value = values[i];
return (highest_value);
}
/* Function to find the average in an array */
int average (int records,int* values)
{
int average_value = 0;
int i;
for (i = 0; i < records; ++i)
average_value = average_value + values[i];
average_value = average_value/records;
return (average_value);
}
/* Function to find the median in an array */
int median (int records,int* values)
{
int median_value = 0;
int i;
int subscript = 0;
int s1 = 0;
int s2 = 0;
for (i = 0; i < records; ++i)
{
s1 = s1 + (values[i] * i);
s2 = s2 + values[i];
}
subscript = s1/s2;
return (values[subscript]);
}
main()
{
int minimum (int records,int* values);
int highest (int records, int* values);
int average (int records, int* values);
int median (int records, int* values);
FILE *gradin, *gradout;
int i,grade,records;
int minimum_value, highest_value, average_value, median_value;
int * total_grades;
char s;
total_grades = (int*)malloc(records * sizeof(int));
if ( (gradin = fopen (``gradin.txt'', ``r'')) == NULL )
printf (``Could not open input file for reading!!\n\n'');
else if ( (gradout = fopen (``gradout.txt'', ``w'')) == NULL )
printf (``Could not open output file for writing!!\n\n'');
else
{
i=0;
records=1;
while ( !feof(gradin) )
{
fscanf(gradin, ``%c %d'',&s,&grade);
total_grades[i] = grade;
++i;
++records;
}
records = records - 1;
}
minimum_value = minimum (records,total_grades);
highest_value = highest (records,total_grades);
average_value = average (records,total_grades);
median_value = median (records,total_grades);
fprintf( gradout, ``The minimum grade is %d.\n'',minimum_value);
fprintf( gradout, ``The highest grade is %d.\n'',highest_value);
fprintf( gradout, ``The average grade is %d.\n'',average_value);
fprintf( gradout, ``The median grade is %d.\n'',median_value);
return(0);
}
input.txt
a 1 23000a 1 23000a 1 23000a 1 23000a 1 23000a 1 23000a 1 23000a
1 23000a 1 23000a 1 23000
gradin.txt
a 43b 23c 87d 45a 43b 23c 87d 45a 43b 23c 87d 45
gradout.txt
Average=59.400002
High grade=87.000000
Low grade=23.000000
Median=44.000000