Thread: swap funtion

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    9

    swap funtion

    ok here's the problem i have this swap function that was created for a previous program, and i want to use it for a sort routine (bubble)

    struct students temp;

    strcpy(temp.name, s1->name);
    temp.test1 = s1->test1;
    temp.test2 = s1->test2;


    strcpy(s1->name, s2->name);
    s1->test1 = s2->test1;
    s1->test2 = s2->test2;


    strcpy(s2->name, temp.name);
    s2->test1 = temp.test1;
    s2->test2 = temp.test2;
    }

    so the question is how do i go about passing elements of a structure(a string) from the sort funtion, into the swap fuction.

    heres the bubble sort so far

    void sort_make(struct cars *inven, int number){
    int i, j;
    for(j = number; j > number ; j--){

    for(i = 0; i < j; i++){

    if(inven[i].make > inven[i+1].make)
    swap(&inven[i].make, &inven[i+1].make);
    }
    }

    }

    -ManicC-

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly are you swapping? Strings, or whole structures? Are you swapping their locations in an array? In a linked list? Or what exactly?

    If it's just strings, copy the string from one, into a buffer, copy the other string over top of the first one, and then finally, copy the buffer over the second string.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    9
    thanks, so what's the syntax for passing from the bubble sort to the swap function? i just want to compare and sort a string element of a structure?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How about?
    Code:
    void swap(students *s1, students *s2)
    {
       struct students temp;
    
       strcpy(temp.name, s1->name); 
       temp.test1 = s1->test1; 
       temp.test2 = s1->test2; 
    
    
       strcpy(s1->name, s2->name); 
       s1->test1 = s2->test1; 
       s1->test2 = s2->test2; 
    
       strcpy(s2->name, temp.name); 
       s2->test1 = temp.test1; 
       s2->test2 = temp.test2; 
    }

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    9
    ok the problem is with the bubble sort itself
    this is what i have now

    void sort_make(struct cars *inven, int number){
    int i, j;
    for(j = number; j > number ; j--){

    for(i = 0; i < j; i++){

    if(inven[i].make > inven[i+1].make)
    swap(&inven[i].make, &inven[i+1].make);
    }
    }

    }

    the warning i get is incompatiable pointer type, also do i have to make a different swap file for each element i want to sort?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Could you please post the definition of
    struct cars
    and the prototype for
    swap (a, b)
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    9
    sure

    struct cars{

    char make[20];
    char model[20];
    char color[20];
    int year;
    int price;
    }inven[50];


    and

    void swap(struct cars *s1, struct cars *s2);

    i'm really not sure about the swap prototype

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well, there's your incompatible pointer type problem...
    swap(&inven[i].make, &inven[i+1].make);

    inven[i].make is a char *
    &inven[i].make is a char **
    swap is expecting a struct cars *

    You prolly want to use

    swap (&inven[i], &inven[i+1]);

    On top of that though, there are other problems in the code.... like
    for(j = number; j > number ; j--)
    Doesn't seem to work.

    Also,
    if(inven[i].make > inven[i+1].make)
    is comparing char *, which would make sense if you are ordering the cars based on their locations in memory, but you prolly want to use something more meaningful, like their price? Or are you trying to order the cars alphabetically (in which case you need to use strcmp)?
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    9
    hmmmm...i'm really lost, i have no idea how to sort this info using a bubble sort heres the complete code if anyone has any ideas, please let me know


    #include <stdio.h>

    struct cars{

    char make[20];
    char model[20];
    char color[20];
    int year;
    int price;
    }inven[50];



    int print_menu(void);
    void sort_make(struct cars*inven, int);
    void sort_model(struct cars*inven, int);
    void sort_color(struct cars*inven, int);
    void sort_year(struct cars*inven, int);
    void sort_price(struct cars*inven, int);
    void print_input(struct cars*inven, int);
    void swap(struct cars *s1, struct cars *s2);



    int main(void)
    {

    int i, number, choice;



    printf("Enter the number of cars: ");
    scanf("%d", &number);

    for(i = 0; i < number; i++){
    printf("--Car %d--\n", i + 1);
    printf("Make: ");
    scanf(" %s", &inven[i].make);
    printf("Model: ");
    scanf(" %s", &inven[i].model);
    printf("Color: ");
    scanf(" %s", &inven[i].color);
    printf("Year: ");
    scanf(" %d", &inven[i].year);
    printf("Price: ");
    scanf(" %d", &inven[i].price);
    printf("\n");
    }
    print_menu();

    if(choice == 1)
    print_input(inven, number);
    else if(choice == 2){
    sort_make(inven, number);
    }
    else if(choice == 3){
    sort_model(inven, number);
    }
    else if(choice == 4){
    sort_color(inven, number);
    }
    else if(choice == 5){
    sort_year(inven, number);
    }
    else if(choice == 6){
    sort_price(inven, number);
    }
    else{
    printf("error please make a selection");
    }


    }



    int print_menu(void){
    int choice;
    printf("How would you like the list displayed?\n");
    printf("1. Show list as input\n");
    printf("2. Show sorted by make\n");
    printf("3. Show sorted by model\n");
    printf("4. Show sorted by color\n");
    printf("5. Show sorted by year\n");
    printf("6. Show sorted by price\n");
    printf("Choice: ");
    scanf("%d", &choice);
    printf("\n");
    return choice;
    }


    void print_input(struct cars *inven, int number){

    int i;
    printf("Make \tModel \tColor \tYear \tPrice\n");
    printf("---- \t----- \t----- \t---- \t-----\n");

    for(i = 0; i < number; i++){

    printf("%s \t%s \t%s \t%d \t%d\n", inven[i].make, inven[i].model, inven[i].color, inven[i].year, inven[i].price);
    }
    }

    void sort_make(struct cars *inven, int number){
    int i, j;
    for(j = number; j > number ; j--){

    for(i = 0; i < j; i++){

    if(inven[i].make > inven[i+1].make)
    swap(&inven[i], &inven[i+1]);
    }
    }

    }

    void sort_model(struct cars *inven, number){
    }

    void sort_color(struct cars *inven, number){
    }

    void sort_year(struct cars *inven, number){
    }

    void sort_price(struct cars *inven, number){
    }

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    First, you'll have to modify your swap function so that it swaps cars, that's all there is to it. That really shouldn't be too much of a problem, it's quite similar to your previous swap function.

    Code:
    for(j = number; j > number ; j--){ 
    
    for(i = 0; i < j; i++){
    I'm just going to point out here that this code is not going to work. While refraining from giving you the answer, it's prolly worthwhile togo back to your notes and see what the parameters of this loop should look like. As it is, the for loop will just be skipped because you initilize j to number, but the test sees if j is greater than number, which it is not, so it terminates the loop once it starts.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list swap function
    By Axel in forum C Programming
    Replies: 32
    Last Post: 01-16-2011, 09:40 AM
  2. Atomic compare and swap.
    By Maz in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 02:47 PM
  3. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 06:32 AM
  4. Double Link List Swap function
    By kennny2004 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2006, 11:52 AM
  5. Skipping DX swap chain node - not good...
    By Magos in forum Game Programming
    Replies: 0
    Last Post: 03-02-2006, 08:37 PM