15,996,279 members
4.00/5 (1 vote)
See more:
Hello, I'm writing this project for my school, and was wondering how to have more than just 4 different locations to edit..basically when I run the code, it will allow you to add a new customer normally, but will not sort it, unless you delete one of the pre-determined array customer values. I think it might have something to do with the i<7, or j<4, i tried messing with the values, but it seems this is the only two values that makes the program run

C
```//--Print All Customers
void printAllCustomers ( int accountArray[], double balanceArray[], char customerNames[][25] )
{
int index;
int i, j;

char temp[SIZE];

for (i=0;i<;7;i++)
{
for (j=i+1;j<4;j++)
{
if (strcmp(customerNames[i],customerNames[j])<0)
{
strcpy(temp,customerNames[i]);
strcpy(customerNames[i],customerNames[j]);
strcpy(customerNames[j],temp);
}
}
}

for ( index = 0; index<SIZE; index++)
{
if ( accountArray[index] != 0 )

printf("\t%s\n\t%i\n\t%.2lf\n\n", customerNames[index], accountArray[index], balanceArray[index]);

}
system("pause");
bankerSide ( accountArray, balanceArray, customerNames );

}
//--------------------------------------------------------------------------------```
Posted
Updated 13-Jun-11 9:03am
v2

Solution 1

The bubble sort requires:
C
```for (i=0; i<n-1;>    for (j=i+1; j<n;>    {
//...
}
```

where `N` is the size of the array that should be sorted (you should take `N` as `printAllCustomers` argument).

Solution 2

I'm not sure if added this correct or not, it comes up with a bunch of weird numbers for the list of customers whenever i go to run it.

char temp[SIZE];

for (i=0;i<25-1;i++)
{
for (j=i+1;j<25;j++)
{
if (strcmp(customerNames[i],customerNames[j])>0)
{
strcpy(temp,customerNames[i]);
strcpy(customerNames[i],customerNames[j]);
strcpy(customerNames[j],temp);
}
}
}

Solution 3

If you're expected to keep the indexes lined up with the other two arrays, you're in a world of hurt.

The first thing I'd do is create an object to hold one element from each array (at the same index, of course). Next, I'd put thos objects into a generic list. Next, I'd use this tip/trick to sort on the desired property:

A Generic Comparison Class for Collection Items[^]

Finally, I'd put the values back into the arrays in the new order (if required).

Your current design is a freakin' nightmare.

Solution 4

The program runs normally with it being 4, but the problem is that whenever you are actually in the program, and add a value to the array ( a new customer ), and then come back to this function to print it sorted, it will only sort the pre-set values of the array, and not the added one from what the user just added.

int accountArray[SIZE] = {1202, 5841, 5644, 5481};
double balanceArray[SIZE] = {450.23, 541.25, 1027.27, 108.50};
char customerNames[SIZE][25] = {"George Washington", "John Smith", "Jane Smith", "Adam Smith"

The first time around it will actually sort the names in this order:
1202
450.23

George Washington
5481
541.25

Jane Smith
5644
1027.27

John Smith
5481
108.50

**But after I add a new customer to the array, it will display this function like this (only sorting the preset array values):
1202
450.23

George Washington
5481
541.25

Jane Smith
5644
1027.27

John Smith
5481
108.50

Entrey Smith
1000
500.50

****Entrey Smith should be under George in the aplhabet, so not sure how to have it like re-sort all this stuff at run-time? Thanks for the assistance! ;)

Solution 5

I managed to get my code to work using a for loop that calculates the length of the string!! Thanks so much though for help ;)