![]() |
| | #1 |
| Registered User Join Date: Dec 2001
Posts: 4
| #include<stdio.h> void datain(int, int[10000][3]); void dataout(int, int[10000][3]); void main() { int down,person[10000][3]; /*choose the size of table required*/ printf("Please enter the amount of the people required: \n\r"); scanf("%d",&down); printf("Amount of people required: %d\n\r",down); /*validate user input*/ while ((down <=0) || (down > 100000)) { printf("please re-enter the number of people required:\n\r"); scanf("%d",&down); } /*Create table and input data*/ datain(down, person); /*print out the table unsorted*/ dataout(down, person); } void datain(int numberofpeople, int person[10000][3]) { /*load up person arrays*/ int array1; for (array1 = 1; array1 <= numberofpeople; array1 ++) { printf("\n\rPlease input the person identification: "); scanf("%d",&person[array1][0]); printf("Please input the person Wage: "); scanf("%d",&person[array1][1]); printf("Please input the person Sex(0 is male and 1 is female): "); scanf("%d",&person[array1][2]); /*validate user input*/ while ((person[array1][2] < 0) || (person[array1][2] > 1)) { printf("please re-enter the sex data\n\r"); scanf("%d",&person[array1][2]); } printf("Please input the person Age: "); scanf("%d",&person[array1][3]); /*validate user input*/ while ((person[array1][3] < 16) || (person[array1][3] > 65)) { printf("please re-enter the age data\n\r"); scanf("%d",&person[array1][3]); } } } void dataout (int number,int person[10000][3]) { int array; printf(" \n\r The Unsorted Data:\n"); printf(" ---------------------\n\r"); printf("Personnal ID Wage Sex Age\n\r"); for (array=1; array<=number; array++) { printf("%8d%8d%8d%8d\n\r",person[array][0],person[array][1],person[array][2],person[array][3]); } /*select form of display from user*/ char nochmal; char sort; nochmal=1; while (nochmal==1) { /*select display from user*/ printf("\n\rHow is the data to be sorted ?"); printf("\n\rAscending by Person ID,type a"); printf("\n\rAscending by Wage, type b"); printf("\n\rAscending by Age, type c"); printf("\n\rOnly females, type f"); printf("\n\rOnly males, type m\n\r"); scanf("\n%c",&sort); /* Sort the data*/ { if (sort == 'a') if (sort == 'b') if (sort == 'c') if (sort == 'd') if (sort == 'f') if (sort == 'm'); } int array, index, total; for( index = 0; index < array - 1; index ++ ) for( array = 0; array < array - index + 1; index ++ ) if( person[array][0] > person[array + 1][0] ) { total = person[array][0]; person[array][0] = person[array + 1][0]; person[array + 1][0] = total; } for (array = 0; array < array; array ++) { printf("Personnel ID: %d\t", person[array][0]); printf("Wage: %d\t", person[array][1]); printf("Sex: %d\t", person[array][2]); printf("Age: %d\t\n", person[array][3]); } /*to resort the data*/ printf("Would you like to sort the data again Yes = y No = n \n"); scanf("%d",&nochmal); return; } } |
| Matt is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,642
| Do you even read the replies you're given? How many times do I have to say it, your array indexing is WRONG: for (array1 = 1; array1 <= numberofpeople; array1 ++) Should be: for (array1 = 0; array1 < numberofpeople; array1 ++) WRONG: printf("Please input the person Age: "); scanf("%d",&person[array1][3]); /*validate user input*/ while ((person[array1][3] < 16) || (person[array1][3] > 65)) { printf("please re-enter the age data\n\r"); scanf("%d",&person[array1][3]); } There is no array[x][3] !! It is ONLY: array[x][0] to array[x][2] Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Registered User Join Date: Sep 2001
Posts: 132
| Would you PLEASE read replies? Would you PLEASE use [code][/code]-tags? Would you PLEASE read the reader? Here's "working" code I fixed only C syntax and indexing errors. The sort algorithm works, but only sorts the ID's. I guess programming will never be a hobby of yours? Code: #include<stdio.h>
#define NOCHMAL 'y'
void datain (int, int[10000][4]);
void dataout (int, int[10000][4]);
int
main (void)
{
int down, person[10000][4];
/*choose the size of table required */
printf ("Please enter the amount of the people required:\n\r");
scanf ("%d", &down);
printf ("Amount of people required: %d\n\r", down);
/*validate user input */
while ((down <= 0) || (down > 10000))
{
printf ("please re-enter the number of people required:\n\r");
scanf ("%d", &down);
}
/*Create table and input data */
datain (down, person);
/*print out the table unsorted */
dataout (down, person);
return 0;
}
void
datain (int numberofpeople, int person[10000][4])
{
/*load up person arrays */
int array1;
for (array1 = 0; array1 < numberofpeople; array1++)
{
printf ("\n\rPlease input the person identification: ");
scanf ("%d", &person[array1][0]);
printf ("Please input the person Wage: ");
scanf ("%d", &person[array1][1]);
printf ("Please input the person Sex(0 is male and 1 is female):");
scanf ("%d", &person[array1][2]);
/*validate user input */
while ((person[array1][2] < 0) || (person[array1][2] > 1))
{
printf ("please re-enter the sex data\n\r");
scanf ("%d", &person[array1][2]);
}
printf ("Please input the person Age: ");
scanf ("%d", &person[array1][3]);
/*validate user input */
while ((person[array1][3] < 16) || (person[array1][3] > 65))
{
printf ("please re-enter the age data\n\r");
scanf ("%d", &person[array1][3]);
}
}
}
void
dataout (int number, int person[10000][4])
{
int array;
char nochmal;
char sort;
int index, total;
printf (" \n\r The Unsorted Data:\n");
printf (" ---------------------\n\r");
printf ("Personnal ID Wage Sex Age\n\r");
for (array = 0; array < number; array++)
{
printf ("%8d%8d%8d%8d\n\r", person[array][0],
person[array][1], person[array][2], person[array][3]);
}
/*select form of display from user */
nochmal = NOCHMAL;
while (nochmal == NOCHMAL)
{
/*select display from user */
printf ("\n\rHow is the data to be sorted ?");
printf ("\n\rAscending by Person ID,type a");
printf ("\n\rAscending by Wage, type b");
printf ("\n\rAscending by Age, type c");
printf ("\n\rOnly females, type f");
printf ("\n\rOnly males, type m\n\r");
scanf (" %c", &sort);
/* Sort the data */
/*
if (sort == 'a')
if (sort == 'b')
if (sort == 'c')
if (sort == 'd')
if (sort == 'f')
if (sort == 'm');
*/
for (index = 0; index < number; index++)
for (array = 0; array < number - 1; array++)
if (person[array][0] > person[array + 1][0])
{
total = person[array][0];
person[array][0] = person[array + 1][0];
person[array + 1][0] = total;
}
for (array = 0; array < number; array++)
{
printf ("Personnel ID: %d\t", person[array][0]);
printf ("Wage: %d\t", person[array][1]);
printf ("Sex: %d\t", person[array][2]);
printf ("Age: %d\t\n", person[array][3]);
}
/*to resort the data */
printf ("Would you like to sort the data again Yes = y No = n\n");
scanf (" %c", &nochmal);
}
}
|
| alex is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C in UNIX environment; external bubble sort doesn't work | stn0091 | C Programming | 2 | 05-13-2009 03:25 PM |
| bubble sort | nynicue | C Programming | 7 | 04-15-2009 05:09 AM |
| problem with gets and bubble sort | wwwildbill | C Programming | 4 | 04-04-2009 01:31 AM |
| Alternate energy sources | Govtcheez | A Brief History of Cprogramming.com | 10 | 02-02-2005 07:07 PM |
| optimizing bubble sort | Sargnagel | C Programming | 14 | 01-23-2003 06:27 AM |