Thread: i am having trouble

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    i am having trouble

    hey i am new to the forum and i have being studying c for about a month now, and i need help with a part of a programe? here is the question:


    An assessor is interested to compare the results of the same group of students at three
    different modules. Write a program that reads the average grade for each of the three
    modules and displays statistics in the order of their results in the following format.

    NAME average
    EM103 60
    EM106 50
    EM108 45

    im not asking you to do the question, i want to know how to get the grades in order and have the subject stay along side them, here is what i have written so far

    Code:
    #include<stdio.h>
    #include<string.h>
    int main ()
    
    {
        int i,j,t;
        float avg1, avg2, avg3;
        char mod1[] = {"em108"}, mod2[] = {"em109"}, mod3[]= {"em102"};
    
    
        printf("%s\t", mod1);
        scanf("%f", &avg1);
        printf("%s\t", mod2);
        scanf("%f", &avg2);
        printf("%s\t", mod3);
        scanf("%f", &avg3);
    
        printf("\nmodule\t grade\n");
        printf("%s    %.2f\n", mod1, avg1);
        printf("%s    %.2f\n", mod2, avg2);
        printf("%s    %.2f\n", mod3, avg3);
    
    
    
    return 0;
    }
    i have been able to get the grades and module to be beside each other but as i said i don't know how to get them in order.

    my teacher told me to use the strcpy function but i haven't been able to find any good examples on the internet or in any books, and any help at all would be a great help. thank you.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> i want to know how to get the grades in order

    Use a sort function.

    >> and have the subject stay along side them

    Use a struct.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Snowball!

    This is a classic "parallel arrays" problem. Strings in the names array, have to stay in order, with the numbers in the grades array. (structs are also good, but this is a parallel array problem).

    Normally, when you sort, you move one item (the swap), if it's out of order. For this program, you need to move two items, if a grade is out of order:

    1) The grade (obviously), and 2) the name that goes with that grade.

    How are you doing with this?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    (structs are also good, but this is a parallel array problem)
    What, is there a difference?

    From http://en.wikipedia.org/wiki/Parallel_array:

    In computing, a parallel array is a data structure for representing arrays of records. It keeps a separate, homogeneous array for each field of the record, each having the same number of elements. Then, objects located at the same index in each array are implicitly the fields of a single record.
    C has structs, which are records, and parallel arrays emulate records.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, there's a difference. Parallel arrays are taught first, and structs are taught after that.



    The reason parallel arrays are taught first, is because they want the student to learn that association between index number in one array, with the data at the index number of another array, and how to use that to their advantage.

    While structs are generally much better in a program design, there are places where structs just suck eggs, and you need to use parallel arrays.
    Last edited by Adak; 02-23-2011 at 03:52 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What, is there a difference?
    1. Ease of use. If I want to pass a particular bit of info function, I only need one argument with a structure. Otherwise I need as many arguments as there are arrays.

    2. Size. The parallel arrays are probably smaller in memory. There is no padding.

    3. Convention (how you us them):

    eacharrayname[ slot ]

    vs

    arrayname[ slot ].each



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

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    hey everybody, i got the result i needed using a structure and bubble sort. i still dont know were the strcpy function comes into this program. but thanks everybody for all your help
    Last edited by snowball; 02-23-2011 at 04:39 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strcpy is used to copy strings from one place to another. You can't do this:
    Code:
    char array[ SOMESIZE ];
    
    array = "somestring";
    You cannot use the assignment operator to assign more than one element to an array at a time (other than when you first declare the variable), so you have to do it one element at a time.

    strcpy does this for you, by taking two arrays and looping through the second one to copy it to the first one.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C trouble with strtok and storing the values
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 03-18-2010, 06:52 AM
  2. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM