HW2_182451MVEB_Desmond_Azogu.c
3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* 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");
}