Commit 201ca2cc by phkarl

Upload New File

parent 4d1e0c30
Showing with 150 additions and 0 deletions
/**
* Author: Philipp Karlon
* Created: 13.11.2018
* Modified: 14.11.2018
* Description: The program creates dance pairs according to
* defined rules.
*/
#include <stdio.h>
// constants
#define ARRAYLEN 20
// function prototypes
int GetCount(void);
void GetHeights(int count, int array[ARRAYLEN]);
void DisplayingHeights(int countBoys, int countGirls,
int arrayBoys[ARRAYLEN],int arrayGirls[ARRAYLEN]);
void SortingHeights(int count, int array[ARRAYLEN]);
void DisplayingPairs(int countBoys, int countGirls,
int arrayBoys[ARRAYLEN],int arrayGirls[ARRAYLEN]);
int main()
{
int countBoys;
int countGirls;
int arrayBoys[ARRAYLEN];
int arrayGirls[ARRAYLEN];
puts("Enter number of boys:");
countBoys = GetCount();
GetHeights(countBoys, arrayBoys);
puts("Enter number of girls:");
countGirls = GetCount();
GetHeights(countGirls, arrayGirls);
DisplayingHeights(countBoys, countGirls, arrayBoys, arrayGirls);
SortingHeights(countBoys, arrayBoys);
SortingHeights(countGirls, arrayGirls);
DisplayingPairs(countBoys, countGirls, arrayBoys, arrayGirls);
return 0;
}
/**
* Function to get the number of boys or girls respectively.
*/
int GetCount()
{
int swap;
scanf("%d", &swap);
return swap;
}
/**
* Function to fill in the heights of boys or girls, respectively, in
* the corresponding array. User will be prompted for a new value until
* he entered as many values as in count.
*/
void GetHeights(int count, int array[ARRAYLEN])
{
int i;
for (i = 0; i < count; i++)
{
do
{
printf("Enter Size %d:\n", i);
scanf("%d", &array[i]);
}
while(array[i] < 0);
}
}
/**
* Function prints out all heigts for boys and girls.
*/
void DisplayingHeights(int countBoys, int countGirls,
int arrayBoys[ARRAYLEN],int arrayGirls[ARRAYLEN])
{
int i;
puts("\n\n");
puts("Boys: ");
for (i = 0; i < countBoys; i++)
{
printf("%d, ", arrayBoys[i]);
}
puts("\n");
puts("Girls: ");
for (i = 0; i < countGirls; i++)
{
printf("%d, ", arrayGirls[i]);
}
puts("\n");
}
/**
* The function sorts the transferred array with the help of
* an insertion sort algorithm.
*/
void SortingHeights(int count, int array[ARRAYLEN])
{
int i, j, key;
for ( j = 0; j < count; j++ )
{
key = array[j];
i = j - 1;
while ( i >= 0 && array[i] > key ) {
array[i + 1] = array[i];
i = i - 1;
}
array[i + 1] = key;
}
}
/**
* The function creates dance couples, whereby the largest boy forms
* a dance couple with the largest girl. If there is an unequal number
* of boys and girls, the largest remaining heights of the boys or
* the smallest remaining heights of the girls are printed out.
*/
void DisplayingPairs(int countBoys, int countGirls,
int arrayBoys[ARRAYLEN],int arrayGirls[ARRAYLEN])
{
int j;
puts("Pairs (boy, girl):");
if(countBoys > countGirls)
{
for (j = 0; j < countGirls; j++)
{
printf("(%d, %d) ", arrayBoys[j], arrayGirls[j]);
}
puts("\n\nBoys without partner:");
for(;j < countBoys; j++)
{
printf("%d, ",arrayBoys[j]);
}
puts("\n");
}
if(countBoys < countGirls)
{
for (j = 0; j < countBoys; j++)
{
printf("(%d, %d) ", arrayBoys[j], arrayGirls[j]);
}
puts("\n\nGirls without partner:");
for(;j < countGirls; j++)
{
printf("%d, ",arrayGirls[j]);
}
puts("\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