Like Tree2Likes

Sorting Without an Array

This is a discussion on Sorting Without an Array within the C Programming forums, part of the General Programming Boards category; Hi, i'm trying to sort a structure but its not set to any specific amount. However i'm trying to apply ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    22

    Sorting Without an Array

    Hi, i'm trying to sort a structure but its not set to any specific amount. However i'm trying to apply a bubble sort but the struct is not have an array! Is there any way to sort without an array??

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    What do you you mean "without an array"? Is it filled with a bunch of different variables?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    yes its a struct! but for input and output i used no arrays
    Code:
    struct database{ 
            char fname[50]; 
            char lname[50]; 
            int age; 
            char gender[50]; 
            int group; 
            int position; 
            };

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    When you have a struct, you sort based on a key of it. For example age. But what exactly you want to do? Sort an array of structs?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Given a single instance of your struct database, what would you expect "sorting" it to achieve?
    iMalc likes this.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    Ok so i basically want to sort by, as you said, age, but im not usings any arrays throughout my program! so i cant use arrays to sort!
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
     
    struct database{ 
            char fname[50]; #include <stdio.h> 
    #include <stdlib.h> 
     
    struct database{ 
            char fname[50]; 
        char lname[50]; 
            int age[]; 
            char gender[50]; 
        int group; 
            int position; 
            };//structure named database 
             
    void menu(struct database); 
    void input(struct database); 
    void files(struct database); 
    void output(struct database); 
    void sort(struct database);
    void sort_age(struct database); 
     
    int count; 
     
    int main() 
    { 
        struct database ind;//ind_Individual 
        menu(ind); 
         
    } 
     
    void menu(struct database ind) 
    { 
        int opt; 
            do{  
                printf("1. Add\n2. View\n3. Sort\n4. Exit\n\n"); 
                printf("Option: "); 
                scanf("%d", &opt); 
                switch(opt) 
                { 
                           case 1: 
                                input(ind); 
                                break; 
                         
                           case 2: 
                                output(ind); 
                                break; 
                         
                           case 3: 
                    sort(ind); 
                                break; 
                 
                case 4: 
                    exit(0); 
                    break; 
                         
                           default: 
                                   printf("INVALID\n\n"); 
                                   break; 
                } 
            }while(opt!=4); 
    } 
     
    void input(struct database ind) 
    {                
         printf("Enter first name: "); 
         scanf("%s", ind.fname); 
         printf("Enter last name: "); 
         scanf("%s", ind.lname); 
         printf("Enter age: "); 
         scanf("%d", &ind.age);      
         printf("Enter gender: "); 
         scanf("%s", ind.gender); 
         printf("Enter group[1-4]: "); 
         scanf("%d", &ind.group);      
         printf("Enter position[1-3]: "); 
         scanf("%d", &ind.position); 
         files(ind); 
         count++; 
          
    } 
     
    void files(struct database ind) 
    { 
        FILE*fp=fopen("REG.txt", "a");  
        fprintf(fp,"%s\n", ind.fname); 
        fprintf(fp,"%s\n", ind.lname); 
        fprintf(fp,"%d\n", ind.age); 
        fprintf(fp,"%s\n", ind.gender); 
        fprintf(fp,"%d\n", ind.group); 
        fprintf(fp,"%d\n", ind.position); 
             fclose(fp); 
    } 
     
    void output(struct database ind) 
    { 
             int inc;//increment 
        FILE*fp=fopen("REG.txt", "r"); 
        for(inc=0; inc<count; inc++) 
        { 
             fscanf(fp,"%s", ind.fname); 
             fscanf(fp, "%s", ind.lname); 
        printf("NAME: %s %s\n", ind.fname, ind.lname); 
             fscanf(fp,"%d", &ind.age); 
             printf("AGE: %d\n", ind.age); 
             fscanf(fp,"%s", ind.gender); 
             printf("GENDER: %s\n", ind.gender); 
        fscanf(fp,"%d", &ind.group); 
            printf("GROUP: %d\n", ind.group); 
             fscanf(fp,"%d", &ind.position); 
             printf("POSITION: %d\n\n", ind.position); 
          } 
             fclose(fp); 
    } 
     
    void sort(struct database ind) 
    { 
        int choice; 
        do{ 
            printf("How would you like to sort the data?\n\n"); 
            printf("1. Age\n2. Group\n3. Position\n4. Main Menu\n\n"); 
            printf("Choice: "); 
            scanf("%d", & choice); 
            switch(choice) 
            {     
                case 1: 
                    sort_age(ind); 
                    break; 
                 
                case 2: 
                    printf("By Group"); 
                    break; 
     
                case 3: 
                    printf("By Position\n"); 
                    break; 
     
                case 4:  
                    menu(ind); 
                    break; 
                 
                default: 
                    printf("INVALID OPTION\n\n"); 
                    break; 
            } 
            }while( choice != 4); 
    }
    
    void sort_age(struct database ind)
    {
        int i, j, temp;
        for (i=0; i<= count-2; i++)
        {
            for (j=0; j<= (count-2) - i; j++)
            {
                if (ind.age[j] > ind.age[j + 1] )
                {
                    temp = ind.age[j];
                    ind.age[j] = ind.age[j + 1];
                    ind.age[j + 1] = temp;
                }
            }
        }#include <stdio.h> 
    #include <stdlib.h> 
     
    struct database{ 
            char fname[50]; 
        char lname[50]; 
            int age[]; 
            char gender[50]; 
        int group; 
            int position; 
            };//structure named database 
             
    void menu(struct database); 
    void input(struct database); 
    void files(struct database); 
    void output(struct database); 
    void sort(struct database);
    void sort_age(struct database); 
     
    int count; 
     
    int main() 
    { 
        struct database ind;//ind_Individual 
        menu(ind); 
         
    } 
     
    void menu(struct database ind) 
    { 
        int opt; 
            do{  
                printf("1. Add\n2. View\n3. Sort\n4. Exit\n\n"); 
                printf("Option: "); 
                scanf("%d", &opt); 
                switch(opt) 
                { 
                           case 1: 
                                input(ind); 
                                break; 
                         
                           case 2: 
                                output(ind); 
                                break; 
                         
                           case 3: 
                    sort(ind); 
                                break; 
                 
                case 4: 
                    exit(0); 
                    break; 
                         
                           default: 
                                   printf("INVALID\n\n"); 
                                   break; 
                } 
            }while(opt!=4); 
    } 
     
    void input(struct database ind) 
    {                
         printf("Enter first name: "); 
         scanf("%s", ind.fname); 
         printf("Enter last name: "); 
         scanf("%s", ind.lname); 
         printf("Enter age: "); 
         scanf("%d", &ind.age);      
         printf("Enter gender: "); 
         scanf("%s", ind.gender); 
         printf("Enter group[1-4]: "); 
         scanf("%d", &ind.group);      
         printf("Enter position[1-3]: "); 
         scanf("%d", &ind.position); 
         files(ind); 
         count++; 
          
    } 
     
    void files(struct database ind) 
    { 
        FILE*fp=fopen("REG.txt", "a");  
        fprintf(fp,"%s\n", ind.fname); 
        fprintf(fp,"%s\n", ind.lname); 
        fprintf(fp,"%d\n", ind.age); 
        fprintf(fp,"%s\n", ind.gender); 
        fprintf(fp,"%d\n", ind.group); 
        fprintf(fp,"%d\n", ind.position); 
             fclose(fp); 
    } 
     
    void output(struct database ind) 
    { 
             int inc;//increment 
        FILE*fp=fopen("REG.txt", "r"); 
        for(inc=0; inc<count; inc++) 
        { 
             fscanf(fp,"%s", ind.fname); 
             fscanf(fp, "%s", ind.lname); 
        printf("NAME: %s %s\n", ind.fname, ind.lname); 
             fscanf(fp,"%d", &ind.age); 
             printf("AGE: %d\n", ind.age); 
             fscanf(fp,"%s", ind.gender); 
             printf("GENDER: %s\n", ind.gender); 
        fscanf(fp,"%d", &ind.group); 
            printf("GROUP: %d\n", ind.group); 
             fscanf(fp,"%d", &ind.position); 
             printf("POSITION: %d\n\n", ind.position); 
          } 
             fclose(fp); 
    } 
     
    void sort(struct database ind) 
    { 
        int choice; 
        do{ 
            printf("How would you like to sort the data?\n\n"); 
            printf("1. Age\n2. Group\n3. Position\n4. Main Menu\n\n"); 
            printf("Choice: "); 
            scanf("%d", & choice); 
            switch(choice) 
            {     
                case 1: 
                    sort_age(ind); 
                    break; 
                 
                case 2: 
                    printf("By Group"); 
                    break; 
     
                case 3: 
                    printf("By Position\n"); 
                    break; 
     
                case 4:  
                    menu(ind); 
                    break; 
                 
                default: 
                    printf("INVALID OPTION\n\n"); 
                    break; 
            } 
            }while( choice != 4); 
    }
    
    void sort_age(struct database ind)
    {
        int i, j, temp;
        for (i=0; i<= count-2; i++)
        {
            for (j=0; j<= (count-2) - i; j++)
            {
                if (ind.age[j] > ind.age[j + 1] )
                {
                    temp = ind.age[j];
                    ind.age[j] = ind.age[j + 1];
                    ind.age[j + 1] = temp;
                }
            }
        }
        printf ("\n\nData Sorted by Age:\n");
        for (i=0; i<= count-1; i++)
            printf("%d\t", ind.age[i]); 
        getch();
    } 
    
        printf ("\n\nData Sorted by Age:\n");
        for (i=0; i<= count-1; i++)
            printf("%d\t", ind.age[i]); 
        getch();
    } 
    
            char lname[50]; 
            int age; 
            char gender[50]; 
            int group; 
            int position; 
            };//structure named database 
             
    void menu(struct database); 
    void input(struct database); 
    void files(struct database); 
    void output(struct database); 
    void sort(struct database);
    void sort_age(struct database); 
     
    int count; 
     
    int main() 
    { 
        struct database ind;//ind_Individual 
        menu(ind); 
         
    } 
     
    void menu(struct database ind) 
    { 
        int opt; 
            do{  
                printf("1. Add\n2. View\n3. Sort\n4. Exit\n\n"); 
                printf("Option: "); 
                scanf("%d", &opt); 
                switch(opt) 
                { 
                           case 1: 
                                input(ind); 
                                break; 
                         
                           case 2: 
                                output(ind); 
                                break; 
                         
                           case 3: 
                    sort(ind); 
                                break; 
                 
                         case 4: 
                                    exit(0); 
                                    break; 
                         
                           default: 
                                   printf("INVALID\n\n"); 
                                   break; 
                } 
            }while(opt!=4); 
    } 
     
    void input(struct database ind) 
    {                
         printf("Enter first name: "); 
         scanf("%s", ind.fname); 
         printf("Enter last name: "); 
         scanf("%s", ind.lname); 
         printf("Enter age: "); 
         scanf("%d", &ind.age);      
         printf("Enter gender: "); 
         scanf("%s", ind.gender); 
         printf("Enter group[1-4]: "); 
         scanf("%d", &ind.group);      
         printf("Enter position[1-3]: "); 
         scanf("%d", &ind.position); 
         files(ind); 
         count++; 
          
    } 
     
    void files(struct database ind) 
    { 
        FILE*fp=fopen("REG.txt", "a");  
        fprintf(fp,"%s\n", ind.fname); 
        fprintf(fp,"%s\n", ind.lname); 
        fprintf(fp,"%d\n", ind.age); 
        fprintf(fp,"%s\n", ind.gender); 
        fprintf(fp,"%d\n", ind.group); 
        fprintf(fp,"%d\n", ind.position); 
             fclose(fp); 
    } 
     
    void output(struct database ind) 
    { 
             int inc;//increment 
        FILE*fp=fopen("REG.txt", "r"); 
        for(inc=0; inc<count; inc++) 
        { 
             fscanf(fp,"%s", ind.fname); 
             fscanf(fp, "%s", ind.lname); 
             printf("NAME: %s %s\n", ind.fname, ind.lname); 
             fscanf(fp,"%d", &ind.age); 
             printf("AGE: %d\n", ind.age); 
             fscanf(fp,"%s", ind.gender); 
             printf("GENDER: %s\n", ind.gender); 
             fscanf(fp,"%d", &ind.group); 
             printf("GROUP: %d\n", ind.group); 
             fscanf(fp,"%d", &ind.position); 
             printf("POSITION: %d\n\n", ind.position); 
          } 
             fclose(fp); 
    } 
     
    void sort(struct database ind) 
    { 
        int choice; 
        do{ 
            printf("How would you like to sort the data?\n\n"); 
            printf("1. Age\n2. Group\n3. Position\n4. Main Menu\n\n"); 
            printf("Choice: "); 
            scanf("%d", & choice); 
            switch(choice) 
            {     
                case 1: 
                    sort_age(ind); 
                    break; 
                 
                case 2: 
                    printf("By Group"); 
                    break; 
     
                case 3: 
                    printf("By Position\n"); 
                    break; 
     
                case 4:  
                    menu(ind); 
                    break; 
                 
                default: 
                    printf("INVALID OPTION\n\n"); 
                    break; 
            } 
            }while( choice != 4); 
    }
    
    void sort_age(struct database ind)
    {
        int i, j, temp;
        for (i=0; i<= count-2; i++)
        {
            for (j=0; j<= (count-2) - i; j++)
            {
                if (ind.age[j] > ind.age[j + 1] )
                {
                    temp = ind.age[j];
                    ind.age[j] = ind.age[j + 1];
                    ind.age[j + 1] = temp;
                }
            }
        }
        printf ("\n\nData Sorted by Age:\n");
        for (i=0; i<= count-1; i++)
            printf("%d\t", ind.age[i]); 
        getch();
    }

    the sort_age function can't work!
    Last edited by joeymaharaj; 03-02-2013 at 05:50 PM.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    I have an array of ints like this
    Code:
    int array[5] = {9,6,4,3,2};
    after sorting I want to achieve this result
    Code:
    int array[5] = {2, 3, 4, 6, 9};
    In your case what do you want to give for sorting and what to get as result?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    i understand sorting...in my case i dont have an array but im reading the entire file!

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    SO, you want to have many structs inside a file, right? A collection of "things" that are of the same type... If you don't have an array, you can write the 1st struct in the file and then compare the second with the first one and write it before or after the 1st one and so on...

    But why not to use an array?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    Quote Originally Posted by std10093 View Post
    SO, you want to have many structs inside a file, right? A collection of "things" that are of the same type... If you don't have an array, you can write the 1st struct in the file and then compare the second with the first one and write it before or after the 1st one and so on...

    But why not to use an array?
    i did, but it was giving errors when i tried to output from the file...so i tried something different! but i may have to revert :/

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Yes. Use an array. You can post back if you are getting errors
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    i tried it. the thing is...i dont want to set a definite amount of locations in the array...and without that my program crashes!

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by joeymaharaj View Post
    i understand sorting...in my case i dont have an array but im reading the entire file!
    From the code of your sort_age() function, it appears you somehow want to have a single struct that contains an array of ages of unspecified size, and you want to sort that array.


    Your problem description is fatally flawed. Firstly, it is not possible for a struct to contain an array with unspecified size (except in one special case, but that is not relevant to your problem). Second, if you did have an array of ages in the struct, sorting that array of ages would only have the effect of jumbling up your records (i.e. changing the recorded age of all people listed in the database).

    You're losing track of the fact that, with a collection of records of people's data, sorting it should not change the age of anyone - the association of the age with other fields needs to be maintained. You don't want a situation where Joe Bloggs has an age of 42 but, after sorting your database, he is recorded as having an age of 16.

    So you need an array of structs, and then sort the array. Not sorting the struct.
    Right 98% of the time, and don't care about the other 3%.

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    You mean that you do not want to have a fixed size for your array? There are some ways to bypass this issue. What I suggest is to use a simple linked list of structs. Here you can find one that handles integers.

    But if you just do your hw, it would be fine to use a fixed logical size I guess..
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  15. #15
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    i know...but its destroying the fundamentals of the program! the basic input and output isnt working now!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding nodes to an array at the top & array sorting
    By Wolf` in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Q sorting a 2D array
    By avi2886 in forum C Programming
    Replies: 4
    Last Post: 05-27-2009, 02:34 PM
  4. Array Sorting ???
    By Takis_ in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2008, 10:38 AM
  5. Sorting an array
    By cashmerelc in forum C Programming
    Replies: 2
    Last Post: 12-05-2007, 12:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21