Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phkarl
/
IAX0583
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
201ca2cc
authored
Nov 19, 2018
by
phkarl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
4d1e0c30
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
150 additions
and
0 deletions
homework2/HW2_184281MV_Philipp_Karlon_v2.c
homework2/HW2_184281MV_Philipp_Karlon_v2.c
0 → 100644
View file @
201ca2cc
/**
* 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\n
Boys 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\n
Girls without partner:"
);
for
(;
j
<
countGirls
;
j
++
)
{
printf
(
"%d, "
,
arrayGirls
[
j
]);
}
puts
(
"
\n
"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment