So I am stuck on how to debug this program correctly and have it run. Can anyone shed some insight. It's suppose to sort an array or names then an array of ages.
prog.c: In function 'main':
prog.c:24:22: warning: passing argument 1 of 'bubblesortname' from incompatible pointer type
bubblesortname(fullname,age,SIZE);
^
prog.c:9:8: note: expected 'char **' but argument is of type 'char (*)[25]'
void bubblesortname(char *fullname[], int *age, int size);
Free cookies to anyone who can get this to run :) **
#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[][25], int *age, int size);
void bubblesortage(char fullname[], int *age, int size);
int main(int argc, char *argv[])
{
char fullname[SIZE][25];
int age[SIZE];
// 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, int size)
{
int temp_age;
char* temp_name;
int j,i;
for (i = 0; i < SIZE - 1; ++i)
{
for (j = 0; i < SIZE - 1; ++j)
{
if (strcmp(fullname[j], fullname[j + 1]) > 0)
{
temp_age = age[i];
age[j] = age[j + 1];
age[j + 1] = temp_age;
temp_name = fullname[j];
fullname[j] = fullname[j + 1];
fullname[j + 1] = temp_name;
}//end if
}//end inner for
}//end for
}//end function
bubblesortage(char fullname[][], int *age, int size)
{
int j,i;
int temp_age;
char* temp_name;
char temp[25];
for (i = 0; i < size - 1; ++i)
{
for (j = 0; j < size - 1; ++j)
{
if (age[j] > age[j + 1])
{
strcpy(temp, fullname[<index1>]);
strcpy(fullname[index1], fullname[index2]);
strcpy(fullname[index2], temp);
temp_age = age[j];
age[j] = age[j + 1];
age[j + 1] = temp_age;
temp_name = fullname[j];
fullname[j] = fullname[j + 1];
fullname[j + 1] = temp_name;
}// end inner for
}// end outer for
}// end function
Gimme my damn cookies :)
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#define SIZE 5
void input (char fullname[][25], int age[]);
void output (char fullname[][25], int age[]);
void bubblesortname (char fullname[][25], int *age, int size);
void bubblesortage (char fullname[][25], int *age, int size);
void fflush_stdin();
int main (void)
{
char fullname[SIZE][25];
int age[SIZE];
// promt user for names and ages
input (fullname, age);
//output unsorted names and ages
printf ("\n input provided:\n\n");
output (fullname, age);
// sorts by name
bubblesortname (fullname, age, SIZE);
printf ("\n sorted by name:\n\n");
output (fullname, age);
//sorts age
bubblesortage (fullname, age, SIZE);
printf ("\n sorted by age:\n\n");
output (fullname, age);
return 0;
}
void input (char fullname[][25], int age[])
{
int i = 0;
size_t nchr = 0;
for (i = 0; i < SIZE; i++) {
printf ("\nEnter a full name: ");
if (fgets (fullname[i], 24, stdin) != NULL)
{
nchr = strlen (fullname[i]);
while (nchr > 0 && (fullname[i][nchr -1] == '\n' || fullname[i][nchr -1] == '\r'))
fullname[i][--nchr] = 0;
}
printf ("Enter the age : ");
scanf ("%d", &age[i]);
fflush_stdin();
}
}
void output (char fullname[][25], int age[])
{
int i;
for (i = 0; i < SIZE; i++)
printf (" %-30s, %d\n", fullname[i], age[i]);
} //end function
void bubblesortname (char fullname[][25], int *age, int size)
{
int j = 0, i = 0;
int temp_age = 0;
char temp_name[25] = {0};
for (i = 0; i < size - 1; ++i) {
// for (j = 0; i < size - 1; ++j) {
for (j = 0; j < size - 1 - i; ++j) {
if (strcmp (fullname[j], fullname[j + 1]) > 0) {
temp_age = age[j + 1];
age[j + 1] = age[j];
age[j] = temp_age;
strcpy (temp_name, fullname[j + 1]);
strcpy (fullname[j + 1], fullname[j]);
strcpy (fullname[j], temp_name);
} //end if
} //end inner for
} //end for
} //end function
void bubblesortage (char fullname[][25], int *age, int size)
{
int j = 0, i = 0;
int temp_age = 0;
char temp_name[25] = {0};
for (i = 0; i < size - 1; ++i) {
// for (j = 0; j < size - 1; ++j) {
for (j = 0; j < size - 1 - i; ++j) {
if (age[j] > age[j + 1]) {
temp_age = age[j + 1];
age[j + 1] = age[j];
age[j] = temp_age;
strcpy (temp_name, fullname[j + 1]);
strcpy (fullname[j + 1], fullname[j]);
strcpy (fullname[j], temp_name);
} // end inner for
} // end outer for
} // end function
}
void fflush_stdin()
{ int c; while ((c = getchar()) != '\n' && c != EOF); }
Output
$ ./bin/freecookies
Enter a full name: George Carver
Enter the age : 143
Enter a full name: Albert Einstein
Enter the age : 115
Enter a full name: Ma Ferguson
Enter the age : 131
Enter a full name: George Charles Butte
Enter the age : 116
Enter a full name: Alexander Hamilton
Enter the age : 277
input provided:
George Carver , 143
Albert Einstein , 115
Ma Ferguson , 131
George Charles Butte , 116
Alexander Hamilton , 277
sorted by name:
Albert Einstein , 115
Alexander Hamilton , 277
George Carver , 143
George Charles Butte , 116
Ma Ferguson , 131
sorted by age:
Albert Einstein , 115
George Charles Butte , 116
Ma Ferguson , 131
George Carver , 143
Alexander Hamilton , 277
Seriously, the issues you had were (1) your indexes on the bubble sorts were completely wrong, (2) you cannot assign strings to each other, you must copy string-to-string, and (3) fflush(stdin)
is never correct resulting in Undefined Behavior.