need help on structure

This is a discussion on need help on structure within the C Programming forums, part of the General Programming Boards category; hi i have a program and i want to sort the numbers each time i will input a number up ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    need help on structure

    hi i have a program and i want to sort the numbers each time i will input a number up to 5 numbers.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    void sort(struct student info);
    
    struct student
    {
           int gpa[5];
           char name[30];      
    };
    
    main()
    {     int i=0;
          int count;
          struct student info;      
          
          for(i=0;i<5;i++)
          {
          printf("enter gpa");
          scanf("%i",&info.gpa[i]);
           
          }
         menu(info);
          
          getch();
          
    }
    void sort(struct student info)
    {
         int temp ;
         int array;
         int j;
         int i;
         for(i=0;i<5;i++)
         {
              for(j=i+1;j<5;j++)
              {
                  if(array[i] > array[j])  //normal is descending sort, so I changed it for you
                 {
                 temp=array[i];
                 array[i]=array[j];
                 array[j]=temp;                  
                 }         
              }
         }
         printf("\n\n"); 
         for(i=0;i<6;i++)
         {
         printf(" %f ",info.gpa[i]);
         }
         getch();
         
    
    }
    Last edited by mouse666666; 03-21-2010 at 02:21 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > i want to sort the numbers each time i will input a number up to 5 numbers.
    So create a separate sort function, which is outside of the menu function (which is an odd place for it anyway).

    Then you can call it from wherever you want.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You also have a mistake in the first for loop. i cannot go all the way to 4, because if it does so, j = i+1 will make j out of bounds. i < 4 is the correct condition

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    so what is my mistake

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    i cannot go all the way to 4, because if it does so, j = i+1 will make j out of bounds. i < 4 is the correct condition

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    i dont get what u are sayingi change it to 4 but it still wont run

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are declaring array as a simple int instead of an array.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also your logic doesn't make any sense. WHAT IS IT you are actually trying to sort?

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    iam tryint to enter 5 number into gpa and the pass it into the function and the sort it. iam lost that is why my code doesnt make sense

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, here are a few things for you to work on:

    1) You are passing a student structure to the sort function but are not using it anywhere inside. My guess is that you want to sort the gpa[] array contained in that student structure. To do this you will need to pass a pointer to the structure rather than the structure itself, so change the sort function header to this:

    Code:
    void sort(struct student *info){
      /* contents */
    }
    2) Now inside the sort function remove the array variable. You are now sorting the gpa[] array member of info. So replace array[i], array[j] with info->gpa[i], info->gpa[j].

    3) I dont know what menu(info) does in your main. That function is not defined anywhere or you just haven't included it's definition in the posting.

    4) After you input the student data in main, make sure to call sort(&info); This will call the function sort and pass a pointer to info (which is the argument type sort() now accepts).

    Good luck! Let us know how if you have any other problems.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    is there anyway to do it with out using pointers because i dont know how to use pointer yet

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you want to modify the variable you are passing as an argument to the function, no, there is no other way.

    Alternatively you can move everything in the sort function in your main if you don't want to/not allowed to use pointers.

    If not, just modify the code according to the instructions above. You don't really need a lot of knowledge about pointers at this point, only that a pointer is a memory address. So instead of passing the values contained in structure info to the sort function you are passing the memory address of where that structure resides in memory and then modify it there, so that the changes are reflected in the variable info in main after the function sort is exited.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    wont sort

    i think i go it
    Last edited by mouse666666; 03-21-2010 at 04:47 PM.

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    cant sort

    i did something like this and it wont sort is my code possbile
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    void pass (struct student information);
    struct student
    {
           int mark[5];
           int name[30];
    };
    main()
    {     struct student information;
          int i;     
          
          for(i=0;i<5;i++)
          {
          printf("enter mark %i:",i+1);
          scanf("%i",&information.mark[i]);
          }
          pass (information);
          getch();
    }
    void pass (struct student information)
    {         int temp;
              int i;
              int j;
            
              for(i=0;i<5;i++)
              {
                  for(j=i+1;i<5;i++)
                  {
                    if(information.mark[i]<information.mark[j])
                    { 
                    temp=information.mark[i];
                    information.mark[i]= information.mark[j];
                    information.mark[j]=temp;   
                    }           
                  }
              }
              for(i=0;i<5;i++)
              {                 
              printf("%i",information.mark[i]);
              }
    }

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As I previously said twice, it will never sort because you are not passing the address of the struct in your sort function but just the values contained therein. You need to pass the pointer to the struct if you want to modify it (i.e. sort it) inside another function.

    Pass struct student *info to the function and call it in main as sort(&info).

    Also change all your dots when accessing members to ->

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 05:53 PM
  2. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

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