Ask Question
18 December, 16:25

Consider designing a program where you need to store information about every student ASU. You need to be able to quickly determine which students are graduating this semester. Would you use an array or linked list? Analyze the problem, design a choice, and justify the choice

+4
Answers (1)
  1. 18 December, 16:51
    0
    Check the explanation

    Explanation:

    Both Arrays and Linked List can be used to store linear data of similar types, but they both have some advantages and disadvantages over each other. To store information about every student we can use an array

    Reason:

    In the array the elements belong to indexes, i. e., if you want to get into the fourth element you have to write the variable name with its index or location within the square bracket.

    In a linked list though, you have to start from the head and work your way through until you get to the fourth element.

    Accessing an element in an array is fast, while Linked list takes linear time, so it is quite a bit slower.

    Example: store the data of students and have faster access and this program in example also do sorting

    #include

    #include

    #include

    / / struct person with 3 fields

    struct Student {

    char * name;

    int id;

    char age;

    };

    / / setting up rules for comparison

    / / to sort the students based on names

    int comparator (const void * p, const void * q)

    {

    return strcmp (((struct Student*) p) - >name,

    ((struct Student*) q) - >name);

    }

    / / Driver program

    int main ()

    {

    int i = 0, n = 5;

    struct Student arr[n];

    / / Get the students data

    arr[0]. id = 1;

    arr[0]. name = "bd";

    arr[0]. age = 12;

    arr[1]. id = 2;

    arr[1]. name = "ba";

    arr[1]. age = 10;

    arr[2]. id = 3;

    arr[2]. name = "bc";

    arr[2]. age = 8;

    arr[3]. id = 4;

    arr[3]. name = "aaz";

    arr[3]. age = 9;

    arr[4]. id = 5;

    arr[4]. name = "az";

    arr[4]. age = 10;

    / / Print the Unsorted Structure

    printf ("Unsorted Student Records:/n");

    for (i = 0; i < n; i++) {

    printf ("Id = %d, Name = %s, Age = %d / n",

    arr[i]. id, arr[i]. name, arr[i]. age);

    }

    / / Sort the structure

    / / based on the specified comparator

    qsort (arr, n, sizeof (struct Student), comparator);

    / / Print the Sorted Structure

    printf ("/n/nStudent Records sorted by Name:/n");

    for (i = 0; i < n; i++) {

    printf ("Id = %d, Name = %s, Age = %d / n",

    arr[i]. id, arr[i]. name, arr[i]. age);

    }

    return 0;

    }

    Output:

    Unsorted Student Records: Id = 1, Name = bd, Age = 12 Id = 2, Name = ba, Age = 10 Id = 3, Name = bc, Age = 8 Id = 4, Name = aaz, Age = 9 Id = 5, Name = az, Age = 10
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Consider designing a program where you need to store information about every student ASU. You need to be able to quickly determine which ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers