Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 26th, 2013, 07:14 PM
 whyyounocompile
Registered User

Join Date: May 2013
Posts: 9
Time spent in forums: 3 h 47 m 26 sec
Reputation 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
May 27th, 2013, 12:17 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 18 h 52 m 28 sec
Reputation Power: 1827
It's high time you learnt how to use the [code][/code] tags.
Mentioned here and here
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
May 27th, 2013, 02:50 PM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,800
Time spent in forums: 1 Month 2 Weeks 3 Days 2 h 27 m 30 sec
Reputation Power: 1569
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:
 Original - 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.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : May 27th, 2013 at 03:17 PM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Help to sort alphabetically! it's urgent!