Thread: Sorting .an array

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    13

    Sorting .an array

    Dear all,

    I found it quite difficult to handle a series of data without using arrays.

    For example, if I want to store a group of 10 students and their marks, it will be easier for me to define two arrays, one for storing the student names and the other to store their marks.

    For example, I can define the arrays as follows:

    char studentName[10][31]; //store up to 10 names with

    //maximum length for name up to 30 characters.

    float studentMark[10];

    I can assign values to those arrays as follows:
    Code:
    int i;
    
    for(i=0; i<10; i++) 
    
    { 
    
    	printf("Enter name for student #%d", i+1);
    
    	scanf("%s", studentName[i]);
    
    	printf("Enter mark for student #%d", i+1);
    
    	scanf("%f", &studentMark[i]); 
    
     }
    However, I got no idea how to sort the arrays such that it will print the name and the mark for the highest student at the top followed by the next highest and so on...

    Anybody can help me on that?

    Thanks in advance.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Cprogramming.com Tutorial: Structures Make an array of them.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Anybody can help me on that?
    How To Ask Questions The Smart Way

    You could search the board for 'sorting', it gets asked an answered on a regular basis.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    You can simply modify the swap function in the sorting procedure.
    But why not use something like a structure?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Arrays are the most useful programming data structure in C, imo.

    There are three ways to handle your problem of sorting data:

    1) When you are sorting, and need to swap a name in the array (say name is the key on this sort), swap the data that "belongs" to that name, at the same time, in the parallel array. If you have more than three arrays, forget using parallel arrays, because it just gets too clumsy and repetitive.

    2) Use a struct that you have made, for this purpose. Generally the popular way to go.

    3) Instead of swapping names and it's data or structs, create an array of integers, and swap the integers, instead. You can use pointers if you prefer (very C, but most beginners will hate it). Integers are easier to work with. These integers will be your index array, into your name and grade arrays.

    No data is moved, just the integers (or pointer addresses), but now the data can be printed out in sorted order, AND the original order is still preserved, since no names or grades were moved. For some problems, this is a real lifesaver.

    Linked lists are a great data structure, but in practice, I seldom use them. For me, arrays just make everything quicker to run, and easier to code, generally.
    Last edited by Adak; 06-27-2011 at 03:10 AM.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    13
    Thank you for your info Adak. I am the beginners in C programming and not familiar with structure and pointer.
    Here are my coding which i do swap function on studentMark to get ascending order for studentMark.
    Unfortunately, the studentName is not swap together. Are there any suggestion on how structure can help me to sorting studentName and studentMark together?

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
         char studentName[10][31];  //store up to 10 names with 
       	//maximum length for name up to 30 characters. 
         float studentMark[10]; 
         int i,j,hold;
    
        for(i=0; i<10; i++) 
        { 
        	printf("Enter name for student #%d", i+1);
         	scanf("%s", studentName[i]);
          	printf("Enter mark for student #%d", i+1);
           	scanf("%f", &studentMark[i]); 
            }
        for (i=0;i<=10-1;i++)    
        for (j=0; j<=10-2; j++)
        {
            if (studentMark[j]>studentMark[j+1])
            { 
                hold=studentMark[j];
                studentMark[j] = studentMark[j+1];
                studentMark[j+1] = hold;
                }
         }
         
         printf("\n List of student in ascending order\n");
         for (i=0; i<10; i++)
         {
              printf("%s\t\t%.0f\n", studentName[i], studentMark[i]);
              }
         getch(); 
         return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your inner sorting loop, you have a hold variable to temporarily hold the value of the mark that will be changed, in the array.

    After you swap marks, you need to immediately swap names. Since names is a string variable, include <string.h> into your include file area, and you can use strcpy(), to copy the names, and keep the same parallel array format.

    I don't use parallel arrays much, *however*, it's important to know how they can be used in a program. For that reason, I'd stick with parallel arrays, for this exercise. Code to include would look like:
    Code:
    char tempstr[31] = {'\0'};
    
    for(i = .......) {
      for(j = ......) {
        if(marks[j] < marks[j+1] {
          //swap the marks, they're out of order
          hold = marks[j];
          marks[j] = marks[j+1];
          marks[j+1] = hold;
    
          //now swap the names  
          strcpy(tempstr, names[j]];
          strcpy(names[j], names[j+1]);
          strcpy(names[j+1], tempstr);
        }
      }
    }
    While it's pretty complete, the above code is not a working program, and is meant to show the general idea, not something to copy and paste and whoopee, it works.

    IMO, it's better to work with parallel arrays, until you have been introduced to structs, and how to work with them, for two reasons:

    1) It's a useful trick for problem solving, in it's own right. If you don't learn it now, you probably won't learn it later.

    2) Sorting with an array of something you haven't studied yet, is just doing the right thing, in the wrong order. Learning should work by building on the known, not working with the unknown, to learn something new.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You'll just have to swap studentName whenever you swap studentMark.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    13
    Thanks for your guiding...i'll try as your suggestion..

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    13
    Thanks adak, my coding are work which i can sort name and mark together based on your guide. It's interest me to learn fast on the chapter that i'm not studied yet to growth my knowledge, especially struct and pointer....

    Thanks again.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're quite welcome.

    You have a good attitude, but try to work with the C subjects that your lessons are focusing on, right now. Structs and pointers will still arrive soon, and it's best if you REALLY know the elementary basics of C, before you start working with them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an array
    By dxgalaxy in forum C Programming
    Replies: 11
    Last Post: 06-09-2011, 06:31 AM
  2. 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
  3. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  4. Sorting an array
    By OnionKnight in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2005, 03:31 AM
  5. Int/Array Sorting
    By Reaperc89 in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2004, 08:38 AM