//************************************************************
// Name: Anthony Principe
// Class: C Programming, Fall 2025
// Assignment: Midterm Exam
// Date: October 26, 2025
//
// Description: Midterm covering functions, loops, conditionals,
// structures, and string manipulation. Includes
// examples of function documentation, arithmetic
// operations, decision making, and structure usage.
//************************************************************
#include <stdio.h>
#include <ctype.h> // used for tolower() in Question 6
//************************************************************
// Function Prototypes
//************************************************************
float calcAreaOfTriangle(float base, float height);
float calcAreaOfRectangle(float length, float width);
float calcAreaOfSquare(float side);
float calcAreaOfCircle(float radius);
float calcAreaOfParallelogram(float base, float height);
float calcAreaOfTrapezoid(float base1, float base2, float height);
int frequency(int theArray[], int n, int x);
int irishLicensePlateValidator(int year, int halfYear, char countyCode);
float calcDogToHumanYears(int dogYear);
char calcLetterGrade(int score);
struct countyTotals freqOfIrishCounties(char countyArrayList[], int size);
float toCelsius(int theFahrenheitTemp);
float toFahrenheit(int theCelsiusTemp);
// supporting structures for later questions
struct countyTotals {
int totalCorkCodes;
int totalDublinCodes;
int totalGalwayCodes;
int totalLimerickCodes;
int totalTiperaryCodes;
int totalWaterfordCodes;
int totalInvalidCountryCodes;
};
struct date {
int day;
int month;
int year;
};
struct dd2209 {
int id;
char officerName[25];
struct date trainingDate;
};
//************************************************************
// MAIN FUNCTION
//************************************************************
int main(void)
{
// Example for Question 1
float triArea = calcAreaOfTriangle(5.0f, 10.0f);
printf("Triangle Area: %.2f\n\n", triArea
);
// Example for Question 2
int numbers[5] = {1, 3, 3, 4, 3};
printf("Frequency of 3 = %d\n\n", frequency
(numbers
, 5, 3));
// Example for Question 3
printf("Valid Plate: %d\n\n", irishLicensePlateValidator
(2020, 1, 'D'));
// Example for Question 4
printf("Dog age 5 = %.1f human years\n\n", calcDogToHumanYears
(5));
// Example for Question 5
printf("Letter grade for 88 = %c\n\n", calcLetterGrade
(88));
// Example for Question 6
char counties[6] = {'C', 'd', 'g', 'w', 'x', 'L'};
struct countyTotals totals = freqOfIrishCounties(counties, 6);
printf("Cork: %d Dublin: %d Galway: %d Limerick: %d Invalid: %d\n\n", totals.totalCorkCodes, totals.totalDublinCodes,
totals.totalGalwayCodes, totals.totalLimerickCodes,
totals.totalInvalidCountryCodes);
// Example for Question 7: Conversion charts
printf("Celsius to Fahrenheit Chart:\n"); printf("Celsius\tFahrenheit\n"); for (int c = 0; c <= 100; c += 10)
printf("%d\t%.1f\n", c
, toFahrenheit
(c
));
printf("\nFahrenheit to Celsius Chart:\n"); printf("Fahrenheit\tCelsius\n"); for (int f = 32; f <= 212; f += 20)
printf("%d\t\t%.1f\n", f
, toCelsius
(f
));
return 0;
}
//************************************************************
// Question 1 - Shape Area Calculations
//************************************************************
// **************************************************
// Function: calcAreaOfTriangle
//
// Description: Calculates the area of a triangle.
//
// Parameters: base - base of the triangle
// height - height of the triangle
//
// Returns: area - area of triangle
// **************************************************
float calcAreaOfTriangle(float base, float height)
{
float area; // area of the triangle
area = (base * height) / 2.0f;
return area;
}
// **************************************************
// Function: calcAreaOfRectangle
//
// Description: Calculates the area of a rectangle.
//
// Parameters: length - length of rectangle
// width - width of rectangle
//
// Returns: area - area of rectangle
// **************************************************
float calcAreaOfRectangle(float length, float width)
{
float area = length * width;
return area;
}
// **************************************************
// Function: calcAreaOfSquare
//
// Description: Calculates area of a square.
//
// Parameters: side - length of one side
//
// Returns: area - area of square
// **************************************************
float calcAreaOfSquare(float side)
{
return (side * side);
}
// **************************************************
// Function: calcAreaOfCircle
//
// Description: Calculates area of a circle.
//
// Parameters: radius - radius of the circle
//
// Returns: area - area of circle
// **************************************************
float calcAreaOfCircle(float radius)
{
const float PI = 3.14159f;
return (PI * radius * radius);
}
// **************************************************
// Function: calcAreaOfParallelogram
//
// Description: Calculates area of a parallelogram.
//
// Parameters: base - base length
// height - height length
//
// Returns: area - area of parallelogram
// **************************************************
float calcAreaOfParallelogram(float base, float height)
{
return (base * height);
}
// **************************************************
// Function: calcAreaOfTrapezoid
//
// Description: Calculates area of a trapezoid.
//
// Parameters: base1, base2 - bases of trapezoid
// height - height of trapezoid
//
// Returns: area - area of trapezoid
// **************************************************
float calcAreaOfTrapezoid(float base1, float base2, float height)
{
return ((base1 + base2) * height) / 2.0f;
}
//************************************************************
// Question 2 - Frequency Function
//************************************************************
// **************************************************
// Function: frequency
//
// Description: Counts how many times a value appears
// in an array.
//
// Parameters: theArray - array of integers
// n - number of elements
// x - value to search for
//
// Returns: frequency - how many times x was found
// **************************************************
int frequency(int theArray[], int n, int x)
{
int frequency = 0; // initialize count
for (int i = 0; i < n; i++)
{
if (theArray[i] == x)
frequency++;
}
return frequency;
}
//************************************************************
// Question 3 - Irish License Plate Validator
//************************************************************
// **************************************************
// Function: irishLicensePlateValidator
//
// Description: Validates an Irish plate based on
// year, half year (1 or 2), and county.
//
// Parameters: year - year portion of plate
// halfYear - 1 or 2
// countyCode- county code letter
//
// Returns: 1 for valid, 0 for invalid
// **************************************************
int irishLicensePlateValidator(int year, int halfYear, char countyCode)
{
int valid = 1;
if (year < 1987 || year > 2025)
valid = 0;
else if (halfYear != 1 && halfYear != 2)
valid = 0;
valid = 0;
return valid;
}
//************************************************************
// Question 4 - Dog to Human Years
//************************************************************
// **************************************************
// Function: calcDogToHumanYears
//
// Description: Converts dog years to human years.
//
// Parameters: dogYear - age of dog in years
//
// Returns: humanYears - equivalent in human years
// **************************************************
float calcDogToHumanYears(int dogYear)
{
float humanYears;
if (dogYear <= 2)
humanYears = dogYear * 10.5f;
else
humanYears = 21 + (dogYear - 2) * 4.0f;
return humanYears;
}
//************************************************************
// Question 5 - Letter Grade
//************************************************************
// **************************************************
// Function: calcLetterGrade
//
// Description: Converts numeric score to letter grade.
//
// Parameters: score - numeric test score
//
// Returns: result - letter grade (A-F)
// **************************************************
char calcLetterGrade(int score)
{
char result;
if (score > 100 || score < 0)
result = 'X'; // invalid score
else if (score >= 90)
result = 'A';
else if (score >= 80)
result = 'B';
else if (score >= 70)
result = 'C';
else if (score >= 60)
result = 'D';
else
result = 'F';
return result;
}
//************************************************************
// Question 6 - County Frequency
//************************************************************
// **************************************************
// Function: freqOfIrishCounties
//
// Description: Counts how many times each county code
// appears in an array.
//
// Parameters: countyArrayList - array of county codes
// size - size of array
//
// Returns: myCountyTotals - struct with counts
// **************************************************
struct countyTotals freqOfIrishCounties(char countyArrayList[], int size)
{
struct countyTotals myCountyTotals = {0,0,0,0,0,0,0};
for (int i = 0; i < size; i++)
{
char code
= tolower(countyArrayList
[i
]);
switch (code)
{
case 'c': myCountyTotals.totalCorkCodes++; break;
case 'd': myCountyTotals.totalDublinCodes++; break;
case 'g': myCountyTotals.totalGalwayCodes++; break;
case 'l': myCountyTotals.totalLimerickCodes++; break;
case 't': myCountyTotals.totalTiperaryCodes++; break;
case 'w': myCountyTotals.totalWaterfordCodes++; break;
default: myCountyTotals.totalInvalidCountryCodes++; break;
}
}
return myCountyTotals;
}
//************************************************************
// Question 7 - Temperature Conversion
//************************************************************
// **************************************************
// Function: toCelsius
//
// Description: Converts Fahrenheit to Celsius.
//
// Parameters: theFahrenheitTemp - Fahrenheit temp
//
// Returns: Celsius temperature
// **************************************************
float toCelsius(int theFahrenheitTemp)
{
return (theFahrenheitTemp - 32) * 5.0f / 9.0f;
}
// **************************************************
// Function: toFahrenheit
//
// Description: Converts Celsius to Fahrenheit.
//
// Parameters: theCelsiusTemp - Celsius temp
//
// Returns: Fahrenheit temperature
// **************************************************
float toFahrenheit(int theCelsiusTemp)
{
return (theCelsiusTemp * 9.0f / 5.0f) + 32.0f;
}
//************************************************************
// Question 8 - Structures (Officer Example)
//************************************************************
// Example initialization:
void exampleStructUsage(void)
{
struct dd2209 officer1 = {1001, "John Murphy", {12, 10, 2025}};
printf("Officer ID: %d Name: %s Training Date: %d/%d/%d\n", officer1.id, officer1.officerName,
officer1.trainingDate.day,
officer1.trainingDate.month,
officer1.trainingDate.year);
}
//************************************************************
// Question 9 - Compiler Likes/Dislikes
//************************************************************
// 1) Like: Easy to test small programs quickly.
// 2) Like: Good error messages that point to line numbers.
// 3) Dislike: Some warnings are vague for beginners.
// 4) Dislike: Output window closes too fast on Windows.