### Thread: Help to sort alphabetically! it's urgent!

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
9
Rep Power
0

#### Help to sort alphabetically! it's urgent!

Hello guys, I need your help *-*, i have to finish this before midnight...

I've almost finished just the sorting alphabetically part is ****ing me.

Look, the example of imput and output:

input:

Pedro Paulo
1000.00
2000.00
Ana Maria da Silva
5000.00
100.00
Luis Meira
3000.00
0.00
Guilherme Coelho
2500.00
500.00
FIM

Output:

Folha de Pagamento

Salario no Mes: R\$ 3000.00
- Guilherme Coelho
- Luis Meira
- Pedro Paulo

Salario no Mes: R\$ 5100.00
- Ana Maria da Silva

the problem is that my output is comming:

Folha de Pagamento

Salario no Mes: R\$ 3000.00
- Pedro Paulo
- Luis Meira
- Guilherme Coelho

Salario no Mes: R\$ 5100.00
- Ana Maria da Silva

I've tried many ways of sorting but it always ignore the sections... for example, Ana Maria da Silva goes to the R\$ 3000.00 section because of the sorting...

I'm a begginer, help me please *-*

#include <stdio.h>

int main(){
int i,x=0,y=0,z=0,m=0,n,f=0,g=0,temp;
float vet[100];
char tempp[100];

typedef struct funcionario {
char nome[100];
char nome1[100];
float s1;
float s2;
float s3;
} funcionario;

funcionario name[100];
funcionario func[100];

for (i=0; i<100; i++){
fgets(func[i].nome,100,stdin);
if((func[i].nome[0]=='F') && (func[i].nome[1]=='I') && (func[i].nome[2]=='M')){
break;
}
scanf("%f\n", &func[i].s1);
scanf("%f\n", &func[i].s2);
func[i].s3 = func[i].s1 + func[i].s2;
vet[i] = func[i].s3;
strcpy(func[i].nome1, func[i].nome);
}

// ascending order
for(x=0;x<i;x++){
for(y=x;y<i;y++){
if(vet[x] > vet[y]){
temp=vet[x];
vet[x]=vet[y];
vet[y]=temp;
}
}
}

int cont = i;
printf("Folha de Pagamento\n");
for(x=0;x<i;x++){
if (vet[x] == vet[x-1]){
}
else{
printf("\nSalario no Mes: R\$ %.2f\n", vet[x]);
}
for(m=0;m<i;m++){
if (vet[x] == func[m].s3){

printf("- %s", func[m].nome);
func[m].s3 = '\0';
}
}
}

system("pause");
return 0;
}
2. It's high time you learnt how to use the [code][/code] tags.
Mentioned here and here
3. As Salem says, you should always put code samples inside of [code] tags, like this:

[code]
my code here
[/code]

This causes it to preserve the indentation and formatting, like so:

Code:
`    my code here`
You can do this by hand, or you can use the code tag button '#' at the top of the editing window and paste the code in (alternately, you can use the highlight marker button, which gives additional highlighting to the code). Always do this when posting a code sample, as the default for the forum (and HTML in general) is to drop the extra whitespace, which causes formatting to be lost.

As a service to you, here is your code with the appropriate formatting tags around it (modulo some minor fixes):

C Code:
```#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
const float epsilon = 0.001;

int i,x=0,y=0,m=0,temp;
float vet[100];

typedef struct funcionario
{
char nome[100];
char nome1[100];
float s1;
float s2;
float s3;
} funcionario;

funcionario func[100];

for (i=0; i<100; i++)
{
fgets(func[i].nome,100,stdin);
if((func[i].nome[0]=='F') && (func[i].nome[1]=='I') && (func[i].nome[2]=='M'))
{
break;
}
scanf("%f\n", &func[i].s1);
scanf("%f\n", &func[i].s2);
func[i].s3 = func[i].s1 + func[i].s2;
vet[i] = func[i].s3;
strcpy(func[i].nome1, func[i].nome);
}

/* ascending order */
for(x=0; x<i; x++)
{
for(y=x; y<i; y++)
{
if(vet[x] > vet[y])
{
temp=vet[x];
vet[x]=vet[y];
vet[y]=temp;
}
}
}

printf("Folha de Pagamento\n");
for(x=0; x<i; x++)
{
if (fabs(vet[x] - vet[x-1]) < epsilon)
{
}
else
{
printf("\nSalario no Mes: R\$ %.2f\n", vet[x]);
}
for(m=0; m<i; m++)
{
if (vet[x] == func[m].s3)
{
printf("- %s", func[m].nome);
func[m].s3 = '\0';
}
}
}

return 0;
}```

As for the problem at hand, it may make the most sense to have separate arrays for the sections, and sort the sections individually. Alternately, you might use an array of linked lists, which would be more efficient in terms of space usage.
Last edited by Schol-R-LEA; May 27th, 2013 at 03:17 PM.