Structured Program

Discussion in 'C' started by KHANZZzzz, Aug 9, 2012.

  1. KHANZZzzz

    KHANZZzzz New Member

    Joined:
    Jul 29, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Write a program which reads up to twenty student names and their exam marks from the keyboard. the program must then list the students in order of their success in the exam.?
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    there's probably a number of similar programs if you search the board. :)

    anyway, you could create a structure that holds info for the names and the exam grades; declare an array of that structure for 20 students, sort the array using the exam, and then print the results.

    Code:
    typedef struct {
       char name[20];
       int exam;
    } students;
    
    void sortByExam(students *tmp) {
       // sorting routine
    }
    
    void inputData(students *tmp) {
       // user input
    }
    
    students arr[20];
    
    inputData(arr);   // pass the entire array
    sortByExam(arr); // pass the entire array
    you probably would not want to hard code the array size, but use a symbolic constant instead. depending on the input loop - whether or not the user can quit input prior to adding 20 records, you may also want to add a parameter for the significant data, or perhaps return the number of records actually stored.

    play around with something like that, and then post your code if you need more help.

    good luck
     
  3. KHANZZzzz

    KHANZZzzz New Member

    Joined:
    Jul 29, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    m very much new to the programing.... if you could help me by creating the entire program i will be very thankfull to you.,
     
  4. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    I'll try and help as best I can, but you'll have to show some code. :)

    Here's a basic skeleton without functions.

    Code:
    #include <stdio.h>
    
    // define your structure
    
    int main(void) {
    
        // declare your student array and other variables as needed
        // in standard C, all variables must be declared at the start of 
        // a code block... so declare what you need here
    
        loop(while test condition isn't met) {
    
           // input student names, exam grade
           // for each index position in the array
    
        }
    
         // sort the array; try a [URL=http://www.go4expert.com/articles/bubble-sort-algorithm-absolute-beginners-t27883/]bubble sort[/URL] it's quite simple to implement
         // hint: create a temp variable of the structure to use in the swap
    
         for(...) {
            for(...)
               if(test the exam here) // hint: array[index].exam
                  // swap here
          }
    
          // output the results
    
          return 0;
    }
     
  5. sumedh yadav

    sumedh yadav New Member

    Joined:
    Aug 18, 2012
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    hi!
    This basically is a sorting problem ,Iknow three ways of sorting,namely
    1)selection sort
    2)insertion sort
    3)bubble sort
    Here are the algorithms of all these methods
    1)selection sort
    int indexOfMax(double marks[],int low,int high) {
    int max ;
    if(low == high) return low;
    max = indexOfMax(marks,low+1,high);
    if(marks[low] > marks[max]) return low ;
    return max;
    } // selSort.c
    #define EXCH(X,Y,Z) ((Z)=(X), (X)=(Y), (Y)=(Z))
    void selectionSort(double marks[], int noOfStdnt) {
    int i ;
    for(i = 0; i < noOfStdnt - 1; ++i) {
    int max = indexOfMax(marks, i, noOfStdnt-1);
    double temp ;
    EXCH(marks, marks[max], temp);
    }
    } // selSort.c
    1)selection sort iterative
    #define EXCH(X,Y,Z) ((Z)=(X), (X)=(Y), (Y)=(Z))
    void selectionSort(double marks[], int noOfStdnt) {
    int i ;
    for(i = 0; i < noOfStdnt - 1; ++i) {
    int max, j ;
    double temp ;
    temp = marks ;
    max = i ;
    for(j = i+1; j < noOfStdnt; ++j)
    if(marks[j] > temp) {
    temp = marks[j] ;
    max = j ;
    }
    EXCH(marks, marks[max], temp);
    }
    } // selSort1.c

    2)insertion sort
    void insertionSort(double marks[], int noOfStdnt){
    int i, j ;
    for(i=1; i < noOfStdnt; ++i) {
    double temp = marks ;
    for(j = i-1; j >= 0; --j) {
    if(marks[j]<temp) marks[j+1]=marks[j];
    else break ;
    }
    marks[j+1] = temp ;
    }
    } // insertionSort.c

    3)bubble sort
    #define EXCHANGE 0
    #define NOEXCHANGE 1
    #define EXCH(X,Y,Z) ((Z)=(X), (X)=(Y), (Y)=(Z))
    void bubbleSort(double marks[], int noOfStdnt) {
    int i, j, exchange, temp ;
    for(i=0; i < noOfStdnt - 1; ++i) {
    exchange = NOEXCHANGE ;
    for(j = noOfStdnt - 1; j > i; --j)
    if(marks[j-1] < marks[j]) {
    EXCH(marks[j-1], marks[j], temp);
    exchange = EXCHANGE ;
    }
    if(exchange) break ;
    }
    } // bubbleSort.c
    hope this method will clarify your methods
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice