PA3

main.c

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Programmer: Nathan Scott
//Class: CptS 121, Fall  2015; Section 01
//Programming Assignment: pa3
//Due: Sept. 23, 2015
//File Type: main function file.
//Purpose: ask user to input values and recall functions.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "header.h"

int main(void){
	//declare variables.
	FILE *fPtr, *fPtrOut = NULL; // NULL ptr
	int menu = 0;
	int fileClosed = 0;
	srand((unsigned)time(0));//generate seed for rand().
	unsigned sn = rand();//generate a unique number.
	double arr[19] = { 0.0 };//total 20 elements.
	int nor = 5;//number of records.
	double sumGpa = 0.0, gpa1 = 0.0, gpa2 = 0.0, gpa3 = 0.0, gpa4 = 0.0, gpa5 = 0.0;
	double sumStand = 0.0, stand1 = 0.0, stand2 = 0.0, stand3 = 0.0, stand4 = 0.0, stand5 = 0.0;
	double sumAge = 0.0, age1 = 0.0, age2 = 0.0, age3 = 0.0, age4 = 0.0, age5 = 0.0;
	double meanGpa = 0.0;
	double meanStand = 0.0;
	double meanAge = 0.0;
	double deviationGpa = 0.0, deviation1 = 0.0, deviation2 = 0.0, deviation3 = 0.0, deviation4 = 0.0, deviation5 = 0.0;
	double varianceGpa = 0.0;
	double sdGpa = 0.0;
	double maxGpa = 0.0;
	double minGpa = 0.0;

	//system("mode con cols=170 lines=40");//define window size.
	system("color 5f");//change window color.

	do{//do while loop start.
		printf("\n\nAuthor: Nathan Scott\nVerson: 3.25\n");
		printf("\n-------------------------------------MENU-------------------------------------\n");//print out the user menu.
		printf(" 1. Open the input and output file\n");
		printf(" 2. Calculates the sum of the GPAs / the Class Standings / the Ages\n");
		printf(" 3. Calculates the mean of the GPAs / the Class Standings / the Ages\n");
		printf(" 4. Calculates the deviation of each GPA from the mean\n");
		printf(" 5. Calculates the variance of the GPA\n");
		printf(" 6. Calculates the Standard Deviation / the Min / the Max of the GPAs\n");
		printf(" 7. Output data then closes the input and output files\n");
		printf(" 0. Save and Exit\n");
		printf("------------------------------------------------------------------------------\n\nEnter: ");

		fflush(stdin);//clear scanf buffer.
		if (scanf("%d", &menu) != 1 || menu > 7){//determine user enter is valid or not.
			system("cls");
			printf("\n***************************************\n");
			printf(" Error: please enter a valid choice!\n");
			printf("***************************************\n");
		}
		else//if user input is valid then enter else clause.
		{
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//1.  Open the input and output file
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			if (menu == 1)
			{
				system("cls");
				fPtr = fopen("input.dat", "r+");
				fPtrOut = fopen("output.dat", "w+");
				printf("\n1. Open the INPUT.DAT and output.dat file\n");
				if (fPtr == NULL){//relative path for filename, and determine file can be open or not.
					printf("\nError: file INPUT.DAT cannot be open!\n\n");
				}
				else//if user input is valid then enter else clause.
				{
					for (int i = 0; i <= sizeof(arr) / sizeof(arr[0]); i++){//all
						arr[i] = read_double(fPtr);//assign a return value to each element in array arr.
						//printf("%d    %.3lf\n", i, arr[i]);//print out each element's value.
					}
					for (int i = 1; i <= sizeof(arr) / sizeof(arr[0]); i = i + 4){//sum of student gpa
						if (i == 1){ gpa1 = arr[i]; }
						if (i == 5){ gpa2 = arr[i]; }
						if (i == 9){ gpa3 = arr[i]; }
						if (i == 13){ gpa4 = arr[i]; }
						if (i == 17){ gpa5 = arr[i]; }
						//sumGpa += arr[i];
						//printf(">> %d    %.3lf\n", i, arr[i]);//print out each element's value.
					}
					for (int i = 2; i <= sizeof(arr) / sizeof(arr[0]); i = i + 4){//sum of student class standing
						if (i == 2){ stand1 = arr[i]; }
						if (i == 6){ stand2 = arr[i]; }
						if (i == 10){ stand3 = arr[i]; }
						if (i == 14){ stand4 = arr[i]; }
						if (i == 18){ stand5 = arr[i]; }
						//sumStand += arr[i];
						//printf(">>> %d    %.3lf\n", i, arr[i]);//print out each element's value.
					}
					for (int i = 3; i <= sizeof(arr) / sizeof(arr[0]); i = i + 4){//sum of student age
						if (i == 3){ age1 = arr[i]; }
						if (i == 7){ age2 = arr[i]; }
						if (i == 11){ age3 = arr[i]; }
						if (i == 15){ age4 = arr[i]; }
						if (i == 19){ age5 = arr[i]; }
						//sumAge += arr[i];
						//printf(">>>> %d    %.3lf\n", i, arr[i]);//print out each element's value.
					}
					printf("File open succeed, you may proceed now!\n");
				}
			}
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//2.  Calculates the sum of the GPAs / the Class Standings / the Ages
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			if (menu == 2)
			{
				system("cls");
				printf("\n2. Calculates the sum of the GPAs / the Class Standings / the Ages\n");
				if (fPtr == NULL || fPtrOut == NULL){//determine file is already open or not.
					printf("\nError: Please open INPUT.DAT and OUTPUT.DAT files first! (Menu 1)\n\n");
				}
				else
				{
					sumGpa = calculate_sum(gpa1, gpa2, gpa3, gpa4, gpa5);
					sumStand = calculate_sum(stand1, stand2, stand3, stand4, stand5);
					sumAge = calculate_sum(age1, age2, age3, age4, age5);
					printf("Sum of all students' GPA is %.3lf\n", sumGpa);
					printf("Sum of all students' Class Standing is %.3lf\n", sumStand);
					printf("Sum of all students' Age is %.3lf\n", sumAge);
				}
			}
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//3. Calculates the mean of the GPAs / the Class Standings / the Ages
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			if (menu == 3)
			{
				system("cls");
				printf("\n3. Calculates the mean of the GPAs / the Class Standings / the Ages\n");
				if (fPtr == NULL || fPtrOut == NULL){//determine file is already open or not.
					printf("\nError: Please open INPUT.DAT and OUTPUT.DAT files first! (Menu 1)\n\n");
				}
				else if (sumGpa <= 0 || sumStand <= 0 || sumAge <= 0){
					printf("\nError: Please calculate Sum GPA, Sum Class Standing, and Sum Age first! (Menu 2)\n\n");
				}
				else if (nor <= 0){
					printf("\nError: Please make sure total number of student's records is not less equal to 0!\n\n");
				}
				else
				{
					meanGpa = calculate_mean(sumGpa, nor);
					meanStand = calculate_mean(sumStand, nor);
					meanAge = calculate_mean(sumAge, nor);
					printf("Mean of all students' GPA is %.3lf\n", meanGpa);
					printf("Mean of all students' Class Standing is %.3lf\n", meanStand);
					printf("Mean of all students' Age is %.3lf\n", meanAge);
				}
			}
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//4. Calculates the deviation of each GPA from the mean
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			if (menu == 4)
			{
				system("cls");
				printf("\n4. Calculates the deviation of each GPA from the mean\n");
				if (fPtr == NULL || fPtrOut == NULL){//determine file is already open or not.
					printf("\nError: Please open INPUT.DAT and OUTPUT.DAT files first! (Menu 1)\n\n");
				}
				else if (meanGpa <= 0){
					printf("\nError: Please calculate Mean GPA first! (Menu 3)\n\n");
				}
				else
				{
					deviation1 = calculate_deviation(gpa1, meanGpa);
					deviation2 = calculate_deviation(gpa2, meanGpa);
					deviation3 = calculate_deviation(gpa3, meanGpa);
					deviation4 = calculate_deviation(gpa4, meanGpa);
					deviation5 = calculate_deviation(gpa5, meanGpa);
					printf("Deviation1: %.3lf\nDeviation2: %.3lf\nDeviation3: %.3lf\nDeviation4: %.3lf\nDeviation5: %.3lf\n", deviation1, deviation2, deviation3, deviation4, deviation5);
				}
			}
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//5. Calculates the variance of the GPA
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			if (menu == 5)
			{
				system("cls");
				printf("\n5. Calculates the variance of the GPA\n");
				if (fPtr == NULL || fPtrOut == NULL){//determine file is already open or not.
					printf("\nError: Please open INPUT.DAT and OUTPUT.DAT files first! (Menu 1)\n\n");
				}
				else
				{
					varianceGpa = calculate_variance(deviation1, deviation2, deviation3, deviation4, deviation5, 5);
					printf("The Variance GPA is %.3lf\n", varianceGpa);
				}
			}
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//6. Calculates the Standard Deviation / the Min / the Max of the GPAs
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			if (menu == 6)
			{
				system("cls");
				printf("\n6. Calculates the Standard Deviation / the Min / the Max of the GPAs\n");
				if (fPtr == NULL || fPtrOut == NULL){//determine file is already open or not.
					printf("\nError: Please open INPUT.DAT and OUTPUT.DAT files first! (Menu 1)\n\n");
				}
				else if (varianceGpa <= 0){
					printf("\nError: Please calculate Variance GPA first! (Menu 5)\n\n");
				}
				else
				{
					sdGpa = calculate_standard_deviation(varianceGpa);
					maxGpa = find_max(gpa1, gpa2, gpa3, gpa4, gpa5);
					minGpa = find_min(gpa1, gpa2, gpa3, gpa4, gpa5);
					printf("The Standard Deviation GPA is %.3lf\n", sdGpa);
					printf("The Max GPA is %.3lf\n", maxGpa);
					printf("The Min GPA is %.3lf\n", minGpa);
				}
			}
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//7. Calculates the Standard Deviation / the Min / the Max of the GPAs
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			if (menu == 7)
			{
				system("cls");
				printf("\n7. Output data then closes the input and output files\n");
				if (fPtr == NULL || fPtrOut == NULL){//determine file is already open or not.
					printf("\nError: Please open INPUT.DAT and OUTPUT.DAT files first! (Menu 1)\n\n");
				}
				else if (sumGpa <= 0 || sumStand <= 0 || sumAge <= 0){
					printf("\nError: Please calculate Mean GPA, Mean Class Standing, Mean Age, Standard Deviation, Max GPA, and Min GPA first! (Menu 3~6)\n\n");
				}
				else
				{
					fprintf(fPtrOut, "SN: %d\n\n", sn);//writes sequence # into output.dat file to indicate file has been written.
					print_double(fPtrOut, meanGpa);
					print_double(fPtrOut, meanStand);
					print_double(fPtrOut, meanAge);
					print_double(fPtrOut, sdGpa);
					print_double(fPtrOut, maxGpa);
					print_double(fPtrOut, minGpa);

					printf("GPA Mean is %.3lf\n", meanGpa);
					printf("Class Standing Mean is %.3lf\n", meanStand);
					printf("Age Mean is %.3lf\n\n", meanAge);
					printf("GPA Standard Deviation is %.3lf\n", sdGpa);
					printf("GPA Max is %.3lf\n", maxGpa);
					printf("GPA Min is %.3lf\n\n", minGpa);

					printf("Output Succeed!\n\nWARNING! To save the output file, you have to select (Menu 0)");
				}
			}
		}
	} while (menu != 0);//end do while loop when menu == 0.
	fileClosed = fcloseall();//close all opened file.
	system("cls");
	printf("\n\n%d files were saved and closed!\n\nGood Bye!\n\n", fileClosed);//output how many open files has been closed.
	system("pause");
	return 0;
}

func.h

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Programmer: Nathan Scott
//Class: CptS 121, Fall  2015; Section 01
//Programming Assignment: pa2
//Due: Sept. 16, 2015
//File Type: header file.
//Purpose: include lib and pre-define functions.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#ifndef _HEADERNAME_H 
#define _HEADERNAME_H
#define _CRT_SECURE_NO_WARNINGS

double read_double(FILE *infile);
int read_integer(FILE *infile);
double calculate_sum(double number1, double number2, double number3, double number4, double number5);
double calculate_mean(double sum, int number);
double calculate_deviation(double number, double mean);
double calculate_variance(double deviation1, double deviation2, double deviation3, double deviation4, double deviation5, int number);
double calculate_standard_deviation(double variance);
double find_max(double number1, double number2, double number3, double number4, double number5);
double find_min(double number1, double number2, double number3, double number4, double number5);
void print_double(FILE *outfile, double number);

#endif

func.c

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Programmer: Nathan Scott
//Class: CptS 121, Fall  2015; Section 01
//Programming Assignment: pa3
//Due: Sept. 23, 2015
//File Type: source file.
//Purpose: define functions and logics.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "header.h"//include the header file.


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//1. Reads one double precision number from the input file.
//Note: You may assume that the file only contains real numbers.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double read_double(FILE *infile){
	double result = 0.0;
	if (fscanf(infile, "%lf", &result) == -1){
		return printf("\nError! Data cannot be read!\n");
	}
	else
	{
		return result;
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//2. Reads one integer number from the input file.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int read_integer(FILE *infile){
	int result = 0;
	if (fscanf(infile, "%d", &result) == -1){
		return printf("\nError! Data cannot be read!\n");
	}
	else
	{
		return result;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//3. Finds the sum of number1, number2, number3, number4, and number5 and returns the result.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double calculate_sum(double number1, double number2, double number3, double number4, double number5){
	double sum = number1 + number2 + number3 + number4 + number5;
	//printf("Sum of all students' GPA is %lf\n", number1 + number2 + number3 + number4 + number5);
	return sum;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//4. Determines the mean through the calculation sum / number and returns the result.
//You need to check to make sure that number is not 0. If it is 0 the function returns -1.0
//(we will assume that we are calculating the mean of positive numbers), otherwise it returns the mean.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double calculate_mean(double sum, int number){
	double mean = sum / number;
	//printf("Mean of all students' GPA is %lf\n", mean);
	return mean;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//5.1. Determines the deviation of number from the mean and returns the result.
//The deviation may be calculated as number - mean.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double calculate_deviation(double number, double mean){
	double result = number - mean;
	//printf("The deviation of the number is %.3lf", result);
	return result;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//5.2. Determines the variance through the calculation:
//((deviation1) ^ 2 + (deviation2) ^ 2 + (deviation3) ^ 2 + (deviation4) ^ 2 + (deviation5) ^ 2) / number and returns the result.
//Hint: you may call your calculate_mean() function to determine the result!
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double calculate_variance(double deviation1, double deviation2, double deviation3, double deviation4, double deviation5, int number){
	double result = ((pow(deviation1, 2) + pow(deviation2, 2) + pow(deviation3, 2) + pow(deviation4, 2) + pow(deviation5, 2)) / number);
	//printf("The variance is %.3lf", result);
	return result;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//6.1. Calculates the standard deviation as sqrt (variance) and returns the result.
//Recall that you may use the sqrt ( ) function that is found in math.h.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double calculate_standard_deviation(double variance){
	double result = sqrt(variance);
	//printf("The standard deviation is ", result);
	return result;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//6.2. Determines the maximum number out of the five input parameters passed into the function, returning the max.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double find_max(double number1, double number2, double number3, double number4, double number5){
	double arr[] = { number1, number2, number3, number4, number5 };
	double max = 0.0;

	max = arr[0];
	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (max < arr[i])//if arr[i] is > max, then assign max = arr[i].
		{
			max = arr[i];
		}
	}
	return max;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//6.3. Determines the minimum number out of the five input parameters passed into the function, returning the min.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double find_min(double number1, double number2, double number3, double number4, double number5){
	double arr[] = { number1, number2, number3, number4, number5 };
	double min = 0.0;

	min = arr[0];
	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (min > arr[i])//if arr[i] is < max, then assign max = arr[i].
		{
			min = arr[i];
		}
	}
	return min;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//7. Prints a double precision number (to the hundredths place) to an output file.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void print_double(FILE *outfile, double number){//void type return nothing.
	fprintf(outfile, "%.2lf\n", number);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//THE END
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////