Commit fb7223ef by chazog

Homework2

parent 75dbf9f8
Showing with 138 additions and 0 deletions
/**
* Author: Azogu Chibuzo Desmond
* Created: 13.11.2018
* Modified: 11.12.2018
* Description: This snippet of code helps make pairing the boys and girls and girls at a school dance conveniently.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//declaring constants
#define MAXSIZE 15
#define FILEPROCESSING_H_
//function prototypes
void readHeights(int num, int arr[MAXSIZE]);
void displayHeights(int numBoys, int numGirls, int arrBoys[MAXSIZE], int arrGirls[MAXSIZE]);
void sortHeights(int num, int arr[MAXSIZE]);
void displayPairs(int numBoys, int numGirls, int arrBoys[MAXSIZE], int arrGirls[MAXSIZE]);
int main()
{
int numBoys;
int numGirls;
int arrBoys[MAXSIZE];
int arrGirls[MAXSIZE];
printf("Enter number of boys: ");
scanf("%d", &numBoys);
if (numBoys > 15)
{
/* if condition is true then print the following */
printf("Number of Boys must be less than or equal to 15\n");
return 0;
}
readHeights(numBoys, arrBoys);
printf("Enter number of girls: ");
scanf("%d", &numGirls);
if (numGirls > 15)
{
/* if condition is true then print the following */
printf("Number of Girls must be less than or equal to 15\n");
return 0;
}
readHeights(numGirls, arrGirls);
displayHeights(numBoys, numGirls, arrBoys, arrGirls);
sortHeights(numBoys, arrBoys);
sortHeights(numGirls, arrGirls);
displayPairs(numBoys, numGirls, arrBoys, arrGirls);
return 0;
}
//Function to read the heights of boys and girls
void readHeights(int count, int array[MAXSIZE])
{
int i;
for (i = 0; i < count; i++)
{
do
{
printf("Enter height %d: ", i + 1);
scanf("%d", &array[i]);
} while (array[i] < 0);
}
}
//Function displays out the heights for the tallest boys and girls.
void displayHeights(int numBoys, int numGirls, int arrBoys[MAXSIZE], int arrGirls[MAXSIZE])
{
int i;
printf("\nHeight of the tallest Boys in the grouping: ");
for (i = 0; i < numBoys; i++)
{
printf("|%d|", arrBoys[i]);
}
printf("\nHeight of the tallest Girls in the grouping: ");
for (i = 0; i < numGirls; i++)
{
printf("|%d|", arrGirls[i]);
}
}
//function for sorting the heights
void sortHeights(int count, int arr[MAXSIZE])
{
int i, j, temp;
for (j = 0; j < count; j++)
{
temp = arr[j];
i = j - 1;
while (i >= 0 && arr[i] > temp)
{
arr[i + 1] = arr[i];
i = i - 1;
}
arr[i + 1] = temp;
}
printf("\n");
}
//function for pairing the tallest boy with the tallest girl
//function also prints out the unpaired tallest boy/girl for when the ratio of boys to girls are not equal
void displayPairs(int numBoys, int numGirls, int arrBoys[MAXSIZE], int arrGirls[MAXSIZE])
{
int i;
printf("\nPairs (boy, girl): ");
if (numBoys > numGirls)
{
for (i = 0; i < numGirls; i++)
{
printf("(%d, %d) ", arrBoys[i], arrGirls[i]);
}
printf("\nBoys without partner: ");
for (; i < numBoys; i++)
{
printf("|%d|", arrBoys[i]);
}
}
if (numBoys < numGirls)
{
for (i = 0; i < numBoys; i++)
{
printf("(%d, %d) ", arrBoys[i], arrGirls[i]);
}
printf("\nGirls without partner:");
for (; i < numGirls; i++)
{
printf("|%d|", arrGirls[i]);
}
}
printf("\n");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment