Thread: Help working with array of structures!

  1. #1
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22

    Question Help working with array of structures!

    Hi alll!
    I've created this C program(see attachments please) to accept names, roll numbers and percentages of 6 students. I've created an array of 6 structs each comprising above mentioned variables. The aim is to display the records in the descending order of the students' percentages(using bubble sort) I've written two copies of the program: one which uses a function for sorting and another in which all code is written in main. Although there aren't any errors in both of them, I ain't getting the desired results for any copy.

    P.S. I tried using pointers too with no different results.

    Any help would be greatly appreciated!

    Just in case those attachments get deleted within one hour the program typed for you:

    WITHOUT FUNCTION:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    struct stu
    {
    char name[15];
    int roll_no;
    float per;
    };
    
    
    void main()
    {
     int i,j;
     struct stu s[6],temp;
     clrscr();
     
     for(i=0;i<6;i++)
     {
     printf("Enter name,roll number and percentage of student:");
     scanf("%s %d %f",s[i].name,&s[i].roll_no,&s[i].per);
     
     }
     
     for(i=0;i<6;i++)
     {
      for(j=0;j<6;j++)
      {
       if (s[i].per<s[j].per)
       {temp.per=s[i].per;             //per swap//
       s[i].per=s[j].per;
       s[j].per=temp.per;
    
       strcpy(temp.name,s[i].name);
       strcpy(s[i].name,s[j].name);            //name swap
       strcpy(s[j].name,temp.name);
    
       temp.roll_no=s[i].roll_no;
       s[i].roll_no=s[j].roll_no;                 //roll no swap
       s[j].roll_no=temp.roll_no;
       }
      }
     }
    
    for(i=0;i<6;i++)
    {printf("%s %d %f",s[i].name,s[i].roll_no,s[i].per);
    }
    getch();
    }


    WITH SORT FUNCTION:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    struct stu
    {
    char name[15];
    int roll_no;
    float per;
    };
    
    void sort(struct stu *s);
    void main()
    {
     int i;
     struct stu s[6];
     clrscr();
     for(i=1;i<=6;i++)
     {
     printf("Enter name,roll number and percentage of student %d:",i);
     scanf("%s %d %f",s[i].name,&s[i].roll_no,&s[i].per);
     }
    
     sort(&s[0]);
    }
    
    void sort(struct stu *s)
    {
     int i,j;
     struct stu temp;
     for(i=0;i<6;i++)
     {
      for(j=0;j<6;j++)
      {
       if((*(s+i)).per<(*(s+j)).per)
       { temp.per=(*(s+i)).per;
         (*(s+i)).per=(*(s+j)).per;
         (*(s+j)).per=temp.per;
    
         strcpy(temp.name,(*(s+i)).name);
         strcpy((*(s+i)).name,(*(s+j)).name);
         strcpy((*(s+j)).name,temp.name);
    
         temp.roll_no=(*(s+i)).roll_no;
         (*(s+i)).roll_no=(*(s+j)).roll_no;
         (*(s+j)).roll_no=temp.roll_no;
       }
      }
     }
     for(i=0;i<6;i++)
     {
      printf("%s %d %f",s[i].name,s[i].roll_no,s[i].per);
    
     }
    getch();
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. Please don't color your code, there is no reason for it.
    2. Void main is wrong, read: How to define main-FAQ
    3. You can directly assign structures in C; there is no need to copy individual members

    With the above information, you should be able to:
    1. Create 2 functions, a comparator for evaluations of your structures; e.g. what member you are comparing on
    2. A sort function that implements the bubble sort calling you comparator function to determine how to sort

    Again, there are many ways to do this, try this one and see what you get.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    ~weird...
    Last edited by AndrewHunter; 09-11-2011 at 12:22 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Sorry! Was new to the site so thought of coloring when I later found out about the code tag and then forgot to de-colorize the program!
    Okay I got the part about main.
    Create 2 functions, a comparator for evaluations of your structures; e.g. what member you are comparing on
    Could you be more specific? I'm not a professional at C yet. I've only recently covered structures in my programming course.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Structures have multiple variables (usually), so to compare one structure against the other, you need to decide what one of its variables you want to use to compare them.
    Code:
    struct house
    {
        int numOfBedRooms;
        _Bool hasfireplace;
        int garagesize;
        float numOfBathRooms;
        ...
    };
    Let us say we have some houses, and we want to compare them to decide what is better. Well, houses are different (ignoring suburbs where they are all the same house) so you have to decide what to you makes the house better. Let us compare by the number of closets, so I will have some place to store all my purple tights.
    Code:
    int compbyclosetspace( struct house h1, struct house h2 )
    {
        if( h1.closetSpace > h2.closetSpace )
            return 1;
        if( h2.closetSpace > h1.closetSpace )
            return 2;
        return 0;
    }
    Now I can see what house is better based on how much storage I have. But if you think houses with fire places are the way to go, then you should write a function to compare that.


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

  6. #6
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Okay now I get what was implied by a 'comparator'. But how about swapping? How would you integrate a swapping routine with a comparator code(with respect to my program)?

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    In the code you posted you are not taking advantage of the fact that you can assign structures directly; with this knowledge you could create a function where you pass 2 structures and the function returns true or false based on the comparison. In that function you would compare the actual structure members you wanted to based on the rules you define. Something like:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct foo{
    	int x;
    	int y;
    };
    
    int compare(struct foo*, struct foo*);
    
    int main(void){
    
    	struct foo fooy, fool;
    	fooy.x =2;
    	fooy.y = 6;
    	fool.x = 1;
    	fool.y =6;
    
    	if(compare(&fooy, &fool)){
    		printf("fooy > fool");
    	}else{
    		printf("fool > fooy");
    	}
    
    	getchar();
    	return(0);
    }
    int compare(struct foo* first, struct foo* second){
    
    	if(first->x < second->x)
    		return 0;
    
    	return 1;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Alright..I understand that my code is not so efficient(may be not at all). But shouldn't it give me the same result?(regardless how concise my code is) Could you point out any error in my code so I would be able to understand it more clearly?

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    Let us compare by the number of closets, so I will have some place to store all my purple tights.
    Quzah.
    Getting ready for your big "stage performance". I did say I would pay to see it.......

    Quote Originally Posted by black_stallion View Post
    Alright..I understand that my code is not so efficient(may be not at all). But shouldn't it give me the same result?(regardless how concise my code is) Could you point out any error in my code so I would be able to understand it more clearly?
    I won't, someone else probably will, but I do not see the point of allowing you to continue down this path of lunacy.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Whoa Whoa! I expected some educated answers; not condemnation for trying to learn C! I accepted that my code was not at all good, didn't I? But I only requested for some error to be pointed out to me so that I could understand the working(or lack of) of my code better(and where the program execution went awry). As to whether my path is lunatic or not is a matter of personal choice! So if you don't want to provide relevant answers, please don't even bother to reply!

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by black_stallion View Post
    Whoa Whoa! I expected some educated answers; not condemnation for trying to learn C! I accepted that my code was not at all good, didn't I? But I only requested for some error to be pointed out to me so that I could understand the working(or lack of) of my code better(and where the program execution went awry). As to whether my path is lunatic or not is a matter of personal choice! So if you don't want to provide relevant answers, please don't even bother to reply!
    No problem......
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    U said, u are using bubble sort, right?! I see there is a problem in the bubble sort algorithm, that's the possible cause of the 'undesired result', i guess!!

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    >> You can directly assign structures in C; there is no need to copy individual members

  14. #14
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    I've found the problem. The float variable per is the problem. Replacing the type float in the structure with an int resolves the problem though I have no idea why this is so!

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you inadvertently fixed the problem while making the change from float to int (since the variable per being float was not actually the problem). (Or, at least, appeared to fix the problem.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help working with functions, structures, and arrays
    By leway in forum C Programming
    Replies: 3
    Last Post: 05-09-2010, 05:24 PM
  2. working with pointers to structures
    By Delia in forum C Programming
    Replies: 2
    Last Post: 03-21-2010, 05:23 PM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. 2 Dimenstional Array vs Array of Structures
    By jrahhali in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2004, 04:51 AM
  5. Working with arrays of structures, need advice...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-14-2002, 08:38 AM

Tags for this Thread