bubble sort problems.Please help 
Author Message
 bubble sort problems.Please help

Quote:

> Hi

> I'am having trouble with my bubble sort.  I have to sort the file by last name.
>  I keep getting an error saying that temp[i] and person[i] are incompatiable
> types.  I have tried strcpy(temp[i].last,person[i].last) but this only switches
> the last name and leaves all the other fields the same.  But i need all fields
> to be switched.  Please if anyone can help me it would be appreciated.  I have
> marked the spot where the bubble sort is its at the bottom.

> thanks
> Jennifer
> #include <stdio.h>
> #include <string.h>

>  struct employee
>  {
>   int emp_num;   /*employee number*/
>   char first[10];   /*first name*/
>   char last[10];   /*last name*/
>   char prod_num[5];   /*product number*/
>   int prod_quota;   /*production quota*/
>   int units_prod;   /*units produced*/
>  };

> bubble_sort(struct employee person[],int count,struct employee temp[]);

> int main()
> {
> FILE *prod_data;
> struct employee person[100],temp[100];
> int i= 0;
> int j=0;
> int val;
> int k= 0;
> int a=0;
> int count = 0;
> prod_data = fopen("empprod.data","r");   /*opening the file*/
> val=fscanf(prod_data,"%d%10s%10s%5s%d%d",&person[i].emp_num,&person[i].
> first,&person[i].last,&person[i].prod_num,&person[i].prod_quota,
>       &person[i].units_prod);
> while (val != EOF)   /*determing the number of people*/
> {
>  i ++;
>  val=fscanf(prod_data,"%d%10s%10s%5s%d%d",&person[i].emp_num,&person[i].fi
> rst,&person[i].last,&person[i].prod_num,&person[i].prod_quota,
> &person[i].units_prod);
> }
> count = i;
> bubble_sort(person,count,temp);   /*call the bubble sort*/
> for (i=0; i<=count; i++)
> printf("Employee Number:%d\nFirst Name:%10s\nLast Name:%10s\nProduct
> Number:%5s\nProduction Quota:%d\nUnits Produced:%d\n\n",
> person[i].emp_num,person[i].first,person[i].last, person[i].prod_num,
> person[i].prod_quota,person[i].units_prod);
>  return;
>  }

> /*this is the bubble sort*/

> bubble_sort(struct employee person[],int count,struct employee temp[]);
> {
> int i;
> for (i=0; i<=count; i++)  /*count is number of people*/
>  {
>   if(strcmp(person[i].last,person[i+1].last)>0)  /*comparing first&second
>                                                                    persons last
> name*/
>   {
>    strcpy(temp[i],person[i]);
>    strcpy(person[i],person[i+1]);
>    strcpy(person[i+1],temp[i]);
>   }
>  }
> return;
> }
> --


Come on!  You simply have to switch each and every field: emp_num,
first, last prod_num, prod_quota and units_prod.

If you want the switch to *look* cleaner when you do the switch, then
use C++ and overload the copy.
--
**************************************
Mark
--



Mon, 18 Mar 2002 03:00:00 GMT  
 bubble sort problems.Please help


Quote:
>name*/
>  {
>   strcpy(temp[i],person[i]);
>   strcpy(person[i],person[i+1]);
>   strcpy(person[i+1],temp[i]);
>  }
> }
>return;
>}

but temp[i] and person[i] are not arrays of char.  That is what the
error message refers to.

Try using memcpy (but you will need to include the size of the employee
record.)

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Mon, 18 Mar 2002 03:00:00 GMT  
 bubble sort problems.Please help

Quote:


> > Hi

> > I'am having trouble with my bubble sort.  I have to sort the file by last name.
> >  I keep getting an error saying that temp[i] and person[i] are incompatiable
> > types.  I have tried strcpy(temp[i].last,person[i].last) but this only switches
> > the last name and leaves all the other fields the same.  But i need all fields
> > to be switched.  Please if anyone can help me it would be appreciated.  I have
> > marked the spot where the bubble sort is its at the bottom.

> > thanks
> > Jennifer
> > #include <stdio.h>
> > #include <string.h>

> >  struct employee
> >  {
> >   int emp_num;   /*employee number*/
> >   char first[10];   /*first name*/
> >   char last[10];   /*last name*/
> >   char prod_num[5];   /*product number*/
> >   int prod_quota;   /*production quota*/
> >   int units_prod;   /*units produced*/
> >  };

> > bubble_sort(struct employee person[],int count,struct employee temp[]);

> > int main()
> > {
> > FILE *prod_data;
> > struct employee person[100],temp[100];
> > int i= 0;
> > int j=0;
> > int val;
> > int k= 0;
> > int a=0;
> > int count = 0;
> > prod_data = fopen("empprod.data","r");   /*opening the file*/
> > val=fscanf(prod_data,"%d%10s%10s%5s%d%d",&person[i].emp_num,&person[i].
> > first,&person[i].last,&person[i].prod_num,&person[i].prod_quota,
> >       &person[i].units_prod);
> > while (val != EOF)   /*determing the number of people*/
> > {
> >  i ++;
> >  val=fscanf(prod_data,"%d%10s%10s%5s%d%d",&person[i].emp_num,&person[i].fi
> > rst,&person[i].last,&person[i].prod_num,&person[i].prod_quota,
> > &person[i].units_prod);
> > }
> > count = i;
> > bubble_sort(person,count,temp);   /*call the bubble sort*/
> > for (i=0; i<=count; i++)
> > printf("Employee Number:%d\nFirst Name:%10s\nLast Name:%10s\nProduct
> > Number:%5s\nProduction Quota:%d\nUnits Produced:%d\n\n",
> > person[i].emp_num,person[i].first,person[i].last, person[i].prod_num,
> > person[i].prod_quota,person[i].units_prod);
> >  return;
> >  }

> > /*this is the bubble sort*/

> > bubble_sort(struct employee person[],int count,struct employee temp[]);
> > {
> > int i;
> > for (i=0; i<=count; i++)  /*count is number of people*/
> >  {
> >   if(strcmp(person[i].last,person[i+1].last)>0)  /*comparing first&second
> >                                                                    persons last
> > name*/
> >   {
> >    strcpy(temp[i],person[i]);
> >    strcpy(person[i],person[i+1]);
> >    strcpy(person[i+1],temp[i]);
> >   }
> >  }
> > return;
> > }
> > --

> Come on!  You simply have to switch each and every field: emp_num,
> first, last prod_num, prod_quota and units_prod.

> If you want the switch to *look* cleaner when you do the switch, then
> use C++ and overload the copy.
> --
> **************************************
> Mark
> --


Chris,

You should be able to simply assign the structure member to be
swapped to a temporary location, etc., thusly:

  struct  employee temp;
  ...

/* swap the members using structure assignment */
  ...
  temp = person[i];
  person[i] = person[i+1];
  person[i+1] = temp;
  ...

[When you get into pointers, you will likely find that the
above is more efficient using them, because instead of
swapping the data itself (which can be large), you can swap
the pointers to the data, which are relatively small.]

Be careful of your comparison loop's ending value (i <= count),
since you are comparing each i'th member to the (i+1)'th member,
therefore, 'i' should stop one short of your 'count-1', i.e.,

  for( i=0; i<(count-1); i++ )  /*count is number of people*/

If 'count' is 5, you are comparing the members indexed
by: [0],[1],[2],[3] to [1],[2],[3],[4].  'count' is never used
as an array index value, since the array is not that long (since
it starts at zero).

Therefore, (typically) you do not use '<=', since you do not
usually want to access the 'count'th value: as mentioned, it
is one past the end of your array.

Yours,

Geoff Houck
systems hk

http://www.teleport.com/~hksys
--



Mon, 18 Mar 2002 03:00:00 GMT  
 bubble sort problems.Please help

Quote:

>   char last[10];   /*last name*/
>  val=fscanf(prod_data,"%d%10s%10s%5s%d%d",&person[i].emp_num,&person[i].fi
> rst,&person[i].last,&person[i].prod_num,&person[i].prod_quota,

Leave the & off person[i].last; you don't want a pointer to array,
but rather a pointer to the first member of the array, and that
is already what the array name will be turned into (without the &).

Quote:
> for (i=0; i<=count; i++)  /*count is number of people*/

Should be < not <=.  Actually, for a bubble sort, it should be
i < count-1, because when you reach the last element there is
nothing to compare it with.

There are other problems, too, but those caught my eye.
--



Mon, 18 Mar 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Please help-still having problems with bubble sort

2. bubble sort walkthrough needed, please

3. Quick sort vs. bubble sort

4. Bubble Sort Problem

5. Binary Search and Bubble Sort Problem

6. Bubble sort help?

7. Bubble sort help?

8. Help : Bubble Sort & Strings

9. RePost: Help : Bubble Sort & Strings

10. Help : Bubble Sort & Strings Cleaned up Version

11. Problems with sorting arrays-Please Help

12. Bubble sorting on disk.

 

 
Powered by phpBB® Forum Software