Ok the bubblesort for age works. The part I am having trouble with now is the bubblesort for the fullname which needs to happen first. I thought about temporarily storing the sorted ages in an array but I guess that's cheating. I need to data entry, print unsorted name and age, sort name, print sorted name unsorted age, sort age, and print sorted name and age...
How can I?
Print the sorted strings without sorting the ages and strings at the same time?
#define SIZE 5
#include <stdio.h>
#include <string.h>
#include <stdio.h>
void input(char fullname[][25], int age[]);
void output(char fullname[][25], int age[]);
//int compare(int x, int y);
void bubbleSortage(int * const array,const int size);
int main(int argc, char *argv[])
{
char fullname[SIZE][25];
int age[SIZE];
int unneccessayalternateagearraybecausewehavetoprintthesortedvaluestwice[SIZE];
// prompt user for names and ages
input(fullname, age);
//output unsorted names and ages
output(fullname, age);
bubblesortname(fullname,SIZE);
output(fullname, age);
//sorts age
bubbleSortage(age,SIZE);
//
output(fullname, age);
return 0;
}
void input(char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
{
fflush(stdin);
printf("Enter a full name\n");
//scanf("%[\^n]\n", fullname[i]);
fgets (fullname[i],40, stdin);
printf("Enter the age\n");
scanf("%d", &age[i]);
}
}
void output(char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
printf("%s, %d\n", fullname[i], age[i]);
}//end function
void bubblesortname(int * const array, const int size)
{
int i, j;
for (j = 0; j < size -1; j++)
{
for (i = 0; i < size -1; i++)
{
if (0<strcmp(fullname[i + 1], fullname[i]))
{
char *temp = fullname[i];
fullname[i]= fullname[i+1];
fullname[i+1]= tmp;
}//end if
}//end inner for
}//end for
}//end function
void bubbleSortage(int * const array, const int size)
{
void swap(int *element1Ptr, int *element2Ptr );
int pass; //pass counter
int j; // comparison counter
//loop to control passes
for(pass = 0;pass < size -1; pass++)
{
//loop to control comparison each pass
for(j=0; j<size - 1;j++)
{
//swap elements if they are not in order
if(array[j]>array[j+1])
{
swap(&array[j], &array[j+1]);
}// end if
}// end inner for
}// end outer for
}// end function
//swap values at memory locations to 1Ptr and 2 Ptr
void swap(int *element1Ptr, int *element2Ptr)
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}// end swap function
The full will be stored in an array of strings, a two dimensioned array of characters. The ages will be stored in an array of integers. Manage the arrays as parallel arrays. Data entry will be from the keyboard and will read the full name followed by the age. Data entry will terminate when the arrays are full or when nothing is entered for the full name. Use a subroutine to enter the data and pass the arrays to the subroutine, do not use global arrays. Once the data is completely entered, use another subroutine to print out the arrays to the screen. Then use a subroutine to sort the data on the full names, ascending order. Reuse the print subroutine and print the sorted data out to the screen. Write another subroutine which sorts the data on age as the primary sort and full name as the secondary sort. Finally reuse the print subroutine to print the data to the screen. The main program will call the data entry function, followed by the print function, the name sort function, the print function, the age sort function and finally the print function. All data will be passed to the functions, no global data
**** UPDATED CODE ************
#define SIZE 5
#include <stdio.h>
#include <string.h>
#include <stdio.h>
void input(char fullname[][25], int age[]);
void output(char fullname[][25], int age[]);
void bubblesortname(char *fullname[], int *age, SIZE size);
bubblesortage(char *fullname[], int *age, SIZE size);
int main(int argc, char *argv[])
{
char fullname[SIZE][25];
int age[SIZE];
char *tmp;
// promt user for names and ages
input(fullname, age);
//output unsorted names and ages
output(fullname, age);
bubblesortname(fullname,age,SIZE);
output(fullname, age);
//sorts age
bubbleSortage(fullname,age,SIZE);
//
output(fullname, age);
return 0;
}
void input(char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
{
fflush(stdin);
printf("Enter a full name\n");
//scanf("%[\^n]\n", fullname[i]);
fgets (fullname[i],40, stdin);
printf("Enter the age\n");
scanf("%d", &age[i]);
}
}
void output(char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
printf("%s, %d\n", fullname[i], age[i]);
}//end function
void bubblesortname(char *fullname[], int *age, SIZE size)
{
int temp_age;
char* temp_name;
int n;
for (SIZE pass = 0; pass < size - 1; ++pass)
{
for (SIZE n = 0; n < len - 1; ++n)
{
if (strcmp(fullname[n], fullname[n + 1]) > 0)
{
temp_age = age[n];
age[n] = age[n + 1];
age[n + 1] = temp_age;
temp_name = fullname[n];
fullname[n] = fullname[n + 1];
fullname[n + 1] = temp_name;
}//end if
}//end inner for
}//end for
}//end function
bubblesortage(char *fullname[], int *ages, SIZE size)
{
int n;
int temp_age;
char* temp_name;
for (SIZE pass = 0; pass < size - 1; ++pass)
{
for (SIZE n = 0; n < size - 1; ++n)
{
if (age[n] > age[n + 1])
{
temp_age = age[n];
age[n] = age[n + 1];
age[n + 1] = temp_age;
temp_name = fullname[n];
fullname[n] = fullname[n + 1];
fullname[n + 1] = temp_name;
}// end inner for
}// end outer for
}// end function
You're required to manage the arrays in parallel, apparently. It would be much better to use a struct, as per my original answer.
But you can't. So, you have to have two arrays. The principles are still the same, it's just a bit messier. Use strcmp
to compare name strings, and compare the ages manually.
Now, technically your specification asks for two sorting subroutines. This means you'll have one for ages, which sorts an array of int
, and another for names, which sorts an array of char*
.
Sorting integers (note that this follows your sorting algorithm, which isn't actually a bubble sort):
void bubble_sort_age(int *arr, size_t len) {
int temp;
for (size_t pass = 0; pass < len - 1; ++pass) {
for (size_t n = 0; n < len - 1; ++n) {
if (arr[n] > arr[n + 1]) {
// write a swap function if you really want to
temp = arr[n];
arr[n] = arr[n + 1];
arr[n + 1] = temp;
}
}
}
}
Note that I'm using the size_t
type for array index counters because it is guaranteed to be large enough for any array.
Personally, I would probably use an unsigned int
type to represent age, since a person with a negative age doesn't make much sense.
And here's the same thing, but using strcmp
on strings:
void bubble_sort_name(char *arr[], size_t len) {
char* temp;
for (size_t pass = 0; pass < len - 1; ++pass) {
for (size_t n = 0; n < len - 1; ++n) {
if (strcmp(arr[n], arr[n + 1]) > 0) {
temp = arr[n];
arr[n] = arr[n + 1];
arr[n + 1] = temp;
}
}
}
}
This isn't quite enough, because we need to ensure that the pairs of names and ages are kept together when we're sorting... so we pass in both arrays whenever we sort, and whenever we swap, we apply the swap to... both arrays.
Now, if we put it all together, it will look something like this:
// swap BOTH name and age to keep the arrays in sync =)
void bubble_sort_name(char *names[], int *ages, size_t len) {
int temp_age;
char* temp_name;
for (size_t pass = 0; pass < len - 1; ++pass) {
for (size_t n = 0; n < len - 1; ++n) {
if (strcmp(names[n], names[n + 1]) > 0) {
temp_age = ages[n];
ages[n] = ages[n + 1];
ages[n + 1] = temp_age;
temp_name = names[n];
names[n] = names[n + 1];
names[n + 1] = temp_name;
}
}
}
}
void bubble_sort_age(char *names[], int *ages, size_t len) {
int temp_age;
char* temp_name;
for (size_t pass = 0; pass < len - 1; ++pass) {
for (size_t n = 0; n < len - 1; ++n) {
if (ages[n] > ages[n + 1]) {
// write a swap function if you really want to
temp_age = ages[n];
ages[n] = ages[n + 1];
ages[n + 1] = temp_age;
temp_name = names[n];
names[n] = names[n + 1];
names[n + 1] = temp_name;
}
}
}
}
void print(char *names[], const int *ages, size_t len) {
for (size_t n = 0; n < len; ++n) {
printf("%s %d\n", names[n], ages[n]);
}
}
int main(void) {
// Input &c omitted.
// If you don't know how to malloc/realloc and read input,
// there should be plenty of other SO questions showing how
int ages[N_ITEMS] = { -10, 2, -1, -10, 0xDEADBEEF };
char *names[] = { "one", "two", "-1", "onf", "foo" };
print(names, ages, N_ITEMS);
printf("\n");
bubble_sort_name(names, ages, N_ITEMS);
print(names, ages, N_ITEMS);
printf("\n");
bubble_sort_age(names, ages, N_ITEMS);
print(names, ages, N_ITEMS);
return 0;
}
Since you are required to sort on name, print, and then primary sort on age but secondary sort on age, we can take advantage of a feature of bubblesort.
It is a stable sort, so when we resort an array on a different criterion, an elements that are equal (under the new sort order) will be kept in the same order (relative to each other) that the old sort sorted them in.
This means we can simply sort on age the second time, and so long as we remember that name and age both have to be rearranged, that's all we need to do :)
Extra for experts: you can actually rewrite the bubble sort method so that you can reuse it for both strings and int
. You can do that using void *
and casts. It would be much better to use a struct type, though.