/*
Author: Don Yessick
Date: Mar 4, 2010
Class: CSCI140L-03 Dr. Yessick
Details of the file.
Reads numbers from a file. Computes average, min and max.
illustrates
* reading numbers from a file,
* counting loop
* computation of sum, average, min, max of a series
*/
//preprocessor directives
#include <stdio.h>
#include <stdlib.h>
void error(char * msg);
void fAvgMinMax (char * filename, double * average, double * min, double * max);
//function main
int
main ( void )
{
double average, min, max;
fAvgMinMax("data.txt",&average,&min,&max);
system("PAUSE");
return 0;
}
/*
input:
error message
description:
print a message, pause,
and stop execution with an error indicating status code
*/
void
error(char * msg){
puts(msg);
system("PAUSE");
exit(1);
}
/*
input:
file name
output:
average, min and max values from file
description:
reads N from a file and then reads N numbers from that file.
Computes average, finds min, finds max
*/
void
fAvgMinMax (char * filename, double * average, double * min, double * max)
{
int N; //first number in file indicates number of entries
double sum; //for computing average
double num; //will be used to hold each number read
int i; //for loop counter
FILE *fp; //file pointeer
fp = fopen(filename, "r"); //open for read access
if (fp == NULL) //verify open succeeded
error("No file"); //exit if open failed
if (fscanf(fp, "%d", &N) != 1) //read N and check for read success
error("No data"); //exit if unable to read N
if (N <= 0) //check for valid N
error("N must be positive"); //exit if bad N
if (fscanf(fp, "%lf", &num) != 1)//read first num, checking for success
error("Format error"); //exit if file is corrupted
*min = *max = sum = num; //init sum, min, max with first read number
for (i=0; i < N-1; i++){ //loop to read remaining N-1 numbers
if (fscanf(fp, "%lf", &num) != 1)//read next num, checking for success
error("Format error"); //exit if file is corrupted
sum += num; //add num to sum
if (num < *min) //can we improve min?
*min = num; //num is best min so far
if (num > *max) //can we improve max?
*max = num; //num is current max
} //for i in 0..n-1
*average = sum / N; //compute average
} //fAvgMinMax
/*
File: YessickDonQuiz.c
Author: Don Yessick
Date: Mar 9, 2010
Class: CSCI140-03 Dr. Yessick
Details of the file:
Quiz program for modulus and int math
*/
//preprocessor directives
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926536
//prototypes go here
int Menu();
void ModulusQuiz();
void MathQuiz();
double getDouble(char * prompt); //returns double from stdin
int getInt(char * prompt); //returns int from stdin
void advance();
//function main
int
main ( void )
{
srand(time(NULL));
while (Menu());
return 0;
}
//implementations go here
/*
Show user a menu and process selection
*/
int Menu(){
char userChoice;
puts("\t\tMenu\n");
puts("Enter Selection");
puts("A\tModulusQuiz");
puts("B\tMathQuiz");
puts("Q\tQuit");
if (scanf("%c",&userChoice)!=1){
return 0;
}
if (feof(stdin))
return 0;
advance();
printf("Read %c.\n",userChoice);
switch (userChoice){
case('a'):
case('A'):
ModulusQuiz();
break;
case('b'):
case('B'):
MathQuiz();
break;
case('q'):
case('Q'):
return 0;
break;
default:
printf("I didn't understand %c, try again.",userChoice);
}
return 1;
}
/*
Quiz user with series of modulus functions
*/
void ModulusQuiz(){
int i;
int a;
int b;
int solution;
int answer;
int pointval;
int score = 0;
puts("Modulus Quiz\n");
for (i = 0; i < 10; i++){
a = rand() % 50 +1;
b = rand() % 20 +1;
solution = a % b;
printf("remainder = a %% b;\nAssume a is %d and b is %d.\n",a,b);
answer = getInt("What is the remainder?");
pointval = 10;
while (answer != solution){
pointval = 0;
if (answer > solution)
answer = getInt("try again, lower");
else
answer = getInt("try again, higher");
}
score += pointval;
puts("Correct");
}
printf("Score = %d%%",score);
}
/*
Quiz user with series of integer math questions
*/
void MathQuiz(){
int a;
int b;
double x;
int solution;
int answer;
int which;
int pointval;
int score = 0;
int i;
puts("Math Quiz\n");
for (i = 0; i < 10; i++){
a = rand() % 12 +1;
b = rand() % 12 +1;
x = rand() % 12 +1;
which = rand() % 55;
printf("Given that int a is %d, int b is %d, double x is %.1f\nand ",a,b,x);
printf("int ");
switch(which){
case(0):
solution = a / b / x;
puts("y = a / b / x;");
break;
case(1):
solution = a % b / x;
puts("y = a % b / x;");
break;
case(2):
solution = a * b / x;
puts("y = a * b / x;");
break;
case(3):
solution = a + b / x;
puts("y = a + b / x;");
break;
case(4):
solution = a - b / x;
puts("y = a - b / x;");
break;
case(5):
solution = a / b * x;
puts("y = a / b * x;");
break;
case(6):
solution = a % b * x;
puts("y = a % b * x;");
break;
case(7):
solution = a * b * x;
puts("y = a * b * x;");
break;
case(8):
solution = a + b * x;
puts("y = a + b * x;");
break;
case(9):
solution = a - b * x;
puts("y = a - b * x;");
break;
case(10):
solution = a / b - x;
puts("y = a / b - x;");
break;
case(11):
solution = a % b - x;
puts("y = a % b - x;");
break;
case(12):
solution = a * b - x;
puts("y = a * b - x;");
break;
case(13):
solution = a + b - x;
puts("y = a + b - x;");
break;
case(14):
solution = a - b - x;
puts("y = a - b - x;");
break;
case(15):
solution = a / b + x;
puts("y = a / b + x;");
break;
case(16):
solution = a % b + x;
puts("y = a % b + x;");
break;
case(17):
solution = a * b + x;
puts("y = a * b + x;");
break;
case(18):
solution = a + b + x;
puts("y = a + b + x;");
break;
case(19):
solution = a - b + x;
puts("y = a - b + x;");
break;
case(20):
solution = a / x * b;
puts("y = a / x * b;");
break;
case(21):
solution = a / x / b;
puts("y = a / x / b;");
break;
case(22):
solution = a / x + b;
puts("y = a / x + b;");
break;
case(23):
solution = a / x - b;
puts("y = a / x - b;");
break;
case(24):
solution = a * x * b;
puts("y = a * x * b;");
break;
case(25):
solution = a * x / b;
puts("y = a * x / b;");
break;
case(26):
solution = a * x + b;
puts("y = a * x + b;");
break;
case(27):
solution = a * x - b;
puts("y = a * x - b;");
break;
case(28):
solution = a + x * b;
puts("y = a + x * b;");
break;
case(29):
solution = a + x / b;
puts("y = a + x / b;");
break;
case(30):
solution = a + x * b;
puts("y = a + x * b;");
break;
case(31):
solution = a + x / b;
puts("y = a + x / b;");
break;
case(32):
solution = a - x * b;
puts("y = a - x * b;");
break;
case(33):
solution = a - x / b;
puts("y = a - x / b;");
break;
case(34):
solution = a - x * b;
puts("y = a - x * b;");
break;
case(35):
solution = a - x / b;
puts("y = a - x / b;");
break;
case(36):
solution = x + a % b;
puts("y = x + a % b;");
break;
case(37):
solution = x + a * b;
puts("y = x + a * b;");
break;
case(38):
solution = x + a / b;
puts("y = x + a / b;");
break;
case(39):
solution = x + a + b;
puts("y = x + a + b;");
break;
case(40):
solution = x + a - b;
puts("y = x + a - b;");
break;
case(41):
solution = x - a % b;
puts("y = x - a % b;");
break;
case(42):
solution = x - a * b;
puts("y = x - a * b;");
break;
case(43):
solution = x - a / b;
puts("y = x - a / b;");
break;
case(44):
solution = x - a + b;
puts("y = x - a + b;");
break;
case(45):
solution = x - a - b;
puts("y = x - a - b;");
break;
case(46):
solution = x * a * b;
puts("y = x * a * b;");
break;
case(47):
solution = x * a / b;
puts("y = x * a / b;");
break;
case(48):
solution = x * a + b;
puts("y = x * a + b;");
break;
case(49):
solution = x * a - b;
puts("y = x * a - b;");
break;
case(50):
solution = x / a * b;
puts("y = x / a * b;");
break;
case(51):
solution = x / a / b;
puts("y = x / a / b;");
break;
case(52):
solution = x / a + b;
puts("y = x / a + b;");
break;
case(53):
solution = x / a - b;
puts("y = x / a - b;");
break;
default:
solution = -x - a % b;
puts("y = -x - a % b;");
break;
}
do {
answer = getInt("what is y?");
if (answer == solution)
puts("correct");
else
if (solution < answer)
puts("no, lower\n");
else
puts("no, higher\n");
} while (answer != solution);
score += pointval;
puts("Correct");
}
printf("Score = %d%%",score);
}
/*
skips past EOLN
*/
void advance(){
scanf("%*[^\n]");
scanf("%*1[\n]");
}
/*
input arguments:
string prompt to be shown to user
returns:
double value read from stdin
*/
double
getDouble(char * prompt)
{
double inputNum;
printf("%s: ",prompt);
scanf("%lf",&inputNum);
advance();
printf("You entered %f\n",inputNum);
return inputNum;
}
/*
input arguments:
string prompt to be shown to user
returns:
int value read from stdin
*/
int
getInt(char * prompt)
{
int inputNum;
printf("%s: ",prompt);
scanf("%d",&inputNum);
advance();
printf("You entered %d\n",inputNum);
return inputNum;
}
/*
File: YessickDonCH456.c
Author: Don Yessick
Date: March 6, 2010
Class: CSCI140-03 Dr. Yessick
Details of the file.
Demoonstration of concepts from chapters 4, 5, 6
*/
//preprocessor directives
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926536
//prototypes go here
double getDouble(char * prompt); //returns double from stdin
int getInt(char * prompt); //returns int from stdin
void advance();//advances input past \n
void print(int x);
void newline();
//demonstrations
void CountingLoops();
void SentinalLoops();
void FlagLoops();
void swap(int *a, int *b);
void order(int *a, int *b);
void MinMedMax(int a, int b, int c, int *min, int *med, int *max);
//function main
int
main ( void )
{
int a, b, c;
int min, med, max;
CountingLoops();
SentinalLoops();
FlagLoops();
swap(&a,&b);
order(&a,&b);
MinMedMax(a,b,c, &min, &med, &max);
system("PAUSE");
return 0;
}
//implementations go here
/*
input arguments:
int x : the value to print
details:
prints x followed by a tab
*/
void
print(int x)
{
printf("%d\t",x);
}
/*
input arguments:
none
details:
prints a new line character
*/
void
newline()
{
printf("\n");
}
/*
input arguments:
none
details:
illustrates counting loops
*/
void
CountingLoops()
{
int i;
for (i = 0; i < 10; i++)
print(i);
newline();
i=0;
while (i<10)
print(i++);
newline();
i==0;
do
print(i);
while (++i<10);
newline();
}
/*
input arguments:
none
details:
illustrates Sentinal controlled loops
*/
void
SentinalLoops()
{
int i;
i = getInt("-999 to quit");
while (i!= -999){
print(i);
i = getInt("-999 to quit");
}
newline();
}
/*
input arguments:
none
details:
illustrates loops controlled using a falg
*/
void
FlagLoops()
{
int i;
int sum = 0;
int done = 0;
do {
i = getInt("enter number");
print(i);
sum += i;
if (i == 99)
done = 1;
if (sum > 100)
done = 1;
if (sum < 0)
done = 1;
if (sum % 25 == 0)
done = 1;
} while (!done);
newline();
}
/*
input arguments:
2 int references
details:
exchanges the values of a with b and b with a's original value
*/
void swap(int *a, int *b)
{
int temphold = *a;
*a = *b;
*b = temphold;
}
/*
input arguments:
2 int references
details:
ensures the first argument is smaller or equal to than the second argument
*/
void order(int *a, int *b)
{
if (*b < *a)
swap(a,b);
}
/*
input arguments:
3 values
3 references
details:
computes minimum, median, and maximum of three values
*/
void MinMedMax(int a, int b, int c, int *min, int *med, int *max)
{
*min = a;
*med = b;
*max = c;
order(min,max);//now at leat min < max
order(min,med); //now med may be min
order(med,max);//or max
/* an alternate solution
order(min,med);
order(med,max);
order(min,med);
*/
}
/*
input arguments:
string prompt to be shown to user
returns:
double value read from stdin
*/
double
getDouble(char * prompt)
{
double inputNum;
printf("%s: ",prompt);
scanf("%lf",&inputNum);
advance();
printf("You entered %f\n",inputNum);
return inputNum;
}
/*
input arguments:
string prompt to be shown to user
returns:
int value read from stdin
*/
int
getInt(char * prompt)
{
int inputNum;
printf("%s: ",prompt);
scanf("%d",&inputNum);
advance();
printf("You entered %d\n",inputNum);
return inputNum;
}
/*
moves input buffer past \n
*/
void
advance(){
scanf("%*[^\n]");
scanf("%*1[\n]");
}
/*
File: YessickDonTrace.c
Author: Don Yessick
Date: March 6, 2010
Class: CSCI140-03 Dr. Yessick
Details of the file.
Demoonstration of concepts from chapters 4, 5, 6
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void one(int a, int b, int c);
void two(int a, int b, int c);
void three(int a, int b, int c);
void four();
void foo(int * a, int b, int * c);
void bar(int a, int * b, int * c);
int can(int * a, int * b);
int wet(int * a, int * b);
int dry(int * a, int * b);
int bus(int * a, int * b);
int
main ( void )
{
printf("ONE\n");
one(1,3,2);
printf("TWO\n");
two(2,6,4);
printf("THREE\n");
three(3, 6, 9);
printf("FOUR\n");
four();
return 0;
}
void
one(int a, int b, int c)
{
int d = 0;
d = can(&a,&c);
printf("%2d %2d %2d %2d\n", d, a, b, c);
d = can(&c,&b);
printf("%2d %2d %2d %2d\n", d, a, b, c);
}
void
two(int a, int b, int c)
{
int d = 0;
d = wet(&a,&c);
printf("%2d %2d %2d %2d\n", d, a, b, c);
d = wet(&b,&a);
printf("%2d %2d %2d %2d\n", d, a, b, c);
}
void
three(int a, int b, int c)
{
int d = 0;
d = dry(&c,&b);
printf("%2d %2d %2d %2d\n", d, a, b, c);
d = dry(&b,&a);
printf("%2d %2d %2d %2d\n", d, a, b, c);
}
void
four()
{
int a = 0;
a = low(4,9);
printf("%2d\n", a);
}
int
can(int * a, int * b)
{
int c = *a;
*a = *b;
*b = c;
return c;
}
int
wet(int * a, int * b)
{
int c;
if (*a > *b){
c = *a;
*a = *b;
*b = c;
return 1;
}
return 0;
}
int
dry(int * a, int * b)
{
if (*a % 2){
*b = *b+1;
return *a;
}
if (*b % 3){
*a = *a+2;
return *b;
}
return 0;
}
int low(int a, int b)
{
printf("%2d %2d\n", a, b);
bus(&a,&b);
printf("%2d %2d\n", a, b);
return bus(&b,&a);
}
int bus(int * a, int * b)
{
int * c;
c = a;
a = b;
b = c;
printf("%2d %2d %2d\n", *a, *b, *c);
return * c;
}