#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    C Assignment Problem


    Write a function with one parameter (a linked list of ints), converts it into an array of ints, and then returns that array. The first element contains the number of elements in the array. Hence if the linked list has two nodes in it, then the array you return should have three elements, with the first one having value 2 and the remaining 2 elements containing the values from the linked list (in the same order as in the list).
    (15 marks)

    Each node of the linked list consists of two consecutive 4-byte values. The first is the value of the node (an int). The second is a pointer (address) to the next node in the list, with 0 representing null.
    (2 marks)

    The function header is:
    int * list2array(void * list)

    That is, the function is sent a pointer to the first node of the list but with an unkown type and it returns the address of the first element of the array.
    You can assume the list sent to the function is well formed (no bad links). You can't assume anything about the length of the list, so you'll have to allocate space for the array being returned dynamically.
    (2 marks)

    An "empty" list will be indicated by the pointer sent to the function being null (0) - in this case return the address of a one-element array containing 0.
    (1 mark)

    Put your function in a file named list2array.c. Don't put any test code in this file - especially NO main() function.
    Below is a simple test program for the list2array function. Copy and paste it into a separate file (testlist2array.c). Modify the list to test your function with different data.





    # include <stdio.h>

    int * list2array(void * list);

    int main() {

    // create a list with three nodes: 17 -> -2 -> 3]
    // with the first node at &data[2];
    int data[6];
    data[0] = -2;
    data[1] = (int)&data[4];
    data[2] = 17;
    data[3] = (int)&data[0];
    data[4] = 3;
    data[5] = 0;

    // call the function
    int * array = (int *)list2array((void *)&data[2]);

    // print the result
    if (array[0] == 0) {
    printf("[]\n");
    }
    else {
    int i;
    printf("[%d", array[1]);
    for (i=2; i<=array[0]; i+=1) {
    printf(", %d", array[i]);
    }
    printf("]\n");
    }
    return 0;
    }
    Your code should compile with no errors (including warnings). Warnings will result in lost points, use -std=c99 during compilation (i.e, gcc -std=c99 -o list2array.c). Test your program as completely as you can, before submissions.

    How to do?
    You must use C programming language in Linux environment for this assignment.

    What to submit?
    Printed report and softcopy of the tested program in text file.

    When to submit?
    You must submit your program by the due date/time in order to receive full credit. Every late submission will incur a 10% penalty per day. Submission MUST be with official assignment cover page. Any plagiarism or cheating will be penalized.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Where I modify the code


    Where should I modify the code
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    So show us what you've done so far. If you have specific questions about specific problems you're having, post them and we will help you. We will not do your homework for you, which is what it looks very much like you are doing here.

    Any plagiarism or cheating will be penalized.
    Please keep that in mind. Also realize that teachers know how to use Google and other search tools, so they can easily find their assignments posted in forums. We've even seen some cheaters get caught here.

    Do your own work. We will help you with problems that you encounter.

    And when you compile your code, be sure to tell gcc to display warnings -- I use -Wall, though there's also -Werror and possibly more. Never ignore warnings. Warnings are much more important than error messages. Treat all warnings as if they were errors and corrected them. Never ignore warnings.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    And please also use code tags to retain your code's formatting. Otherwise your code is just an unreadable mess. This is especially important since you are using K&R formatting that hides open braces at the ends and in the middle of lines. Thus K&R depends absolutely on proper and consistent indenting. You strip out the indenting of code using K&R format and you render it worse than unreadable.

IMN logo majestic logo threadwatch logo seochat tools logo