C Programming Simple Array Processing

[Pages:3]CS 2505 Computer Organization I

Assignment 4: Simple Array Processing in C

C Programming

Simple Array Processing

For this assignment, you will implement the following C functions:

// Compares the contents of arrays lhs[] and rhs[]. bool array_cmp(int lhs[], size_t lusage, int rhs[], size_t rusage);

// Appends the contents of array src[] at the end of array dest[]. size_t array_append(int dest[], size_t dusage, int src[], size_t susage);

// Inserts contents of src[], into dest[] at the specified index. size_t array_insert(int dest[], size_t dusage,

int src[], size_t susage, size_t index);

// Erases up to count elements from array[] starting at the specified // index. size_t array_erase(int array[], size_t usage, size_t index, size_t count);

// Restrictions: // You MAY NOT use string.h, nor any function declared within. // This includes strcpy(), memcpy(), memmove(), memset(), or any variant. // // You MUST manipulate the arrays with your own code (loops/indices), // not with built in functions.

Begin by downloading the posted files:

main.c array_ops.h array_ops.c

sample test driver; modify as you like C header file for compiling with test driver --- do not modify! C source file for implementation of array functions

The file array_ops.h, includes header comments for each of these functions. Pay attention to the comments in the header file. All the stated pre- and post-conditions are part of the assignment.

You can compile these files by using the following command in a Linux shell:

gcc ?o driver ?std=c99 ?Wall main.c array_ops.c

Using this command with the supplied files will yield an executable named driver. You can execute the testing code by using the following command in a Linux shell:

./driver

Although the given code will compile correctly, the resulting program will not satisfy the requirements of the assignment, since the supplied function bodies merely return hard-wired values regardless of the values of the parameters. So, you must correctly complete the implementation of each function.

You may, and should, modify the supplied testing code since it's not exactly thorough. But, if your solutions don't compile with the supplied testing code, they won't compile with the real testing code either.

It's not likely, but you may need to add #include directives to your .c file, as needed for any allowed C Standard Library features you use. You may write secondary "helper" functions if you like; if so, those must be defined and declared within the supplied array_ops.c file. Such functions should be declared as static.

Inserting and Erasing Elements When you insert a new element (or elements) into an array, you have to move the current elements to make an "empty" slot (or slots). Assume we start with the array shown below. The array has a dimension of 5 and initially has a usage of 3, so we don't know what's stored in index 3 or index 4. Lets say we want to insert the value 10 at index 0.

This is an individual assignment!

1

CS 2505 Computer Organization I

Assignment 4: Simple Array Processing in C

To insert, first we have shift the contents out:

Index: 0

1

2

3

4

7

5

3

X

X

Below we've shifted all of the values over by one index. We overwrote index 3 with the value of index 2, overwrote index 2 with the value of index 1, and worked our way down through the other indices from there. For the moment, the value of index 0 and index 1 are the same.

Index: 0

1

2

3

4

7

7

5

3

X

To finish the insert operation, we overwrite index 0 with the value 10. The usage is now 4:

Index: 0

1

2

3

4

10

7

5

3

X

Erasing from an array is a similar operation, but it works in reverse. When you erase from the array, slots aren't "removed" or set to NULL, rather the contents of the array are shifted down and the element(s) being erased are overwritten (or ignored at the end of the array). Let's say we want to erase the contents of index 1:

Index: 0

1

2

3

4

10

7

5

3

X

Below we shifted all of the values down by one index, overwriting index 1 with the value of index 2, and index 2 with the value of index 3. The usage of the array is 3 now, so the value at index 3 is no longer logically part of the array.

Index: 0

1

2

3

4

10

5

3

3

X

A Suggestion or Warning This assignment uses size_t variables. A size_t is an unsigned integer type, that "can store the maximum size of a theoretically possible object of any type (including array)"[cppreference], or more simply, a size_t is often used since it can represent the biggest possible array index for your machine.

Given that size_t is an unsigned type, you need to be careful with edge cases. Many students try to implement the functions using a similar loop to the one shown below:

for (size_t i = usage; i >= 0; i--) {

// do something with an array }

Since we are counting down, you want i to eventually become less than 0, however since the number is unsigned, there aren't any values less than 0. When i = 0, i-- wraps back around to the max value (SIZE_MAX), probably causing an out of bounds error and a program crash. If it doesn't crash it will cause an infinite loop.

I'd suggest always counting up for this assignment, try to avoid counting down.

This is an individual assignment!

2

CS 2505 Computer Organization I

Assignment 4: Simple Array Processing in C

What to Submit You will your array_ops.c file, containing nothing but the implementation of the specified C functions. Be sure to conform to the specified function interfaces.

Your submission will be compiled with a test driver and graded according to how many cases your solution handles correctly.

This assignment will be graded automatically. You will be allowed up to ten submissions for this assignment. Test your function thoroughly before submitting it. Make sure that your function produces correct results for every test case you can think of.

Any additional requirements specified above will be checked manually by a TA after the assignment is closed. Any shortcomings will be assessed a penalty, which will be applied to your score from the Curator.

The course policy is that the submission that yields the highest score will be checked. If several submissions are tied for the highest score, the latest of those will be checked.

Links to the Student Guide and other pertinent information, such as the link to the proper submit page, can be found on the course website.

Pledge Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the submitted file:

// On my honor:

//

// - I have not discussed the C language code in my program with

//

anyone other than my instructor or the teaching assistants

//

assigned to this course.

//

// - I have not used C language code obtained from another student,

//

or any other unauthorized source, either modified or unmodified.

//

// - If any C language code or documentation used in my program

//

was obtained from another source, such as a text book or course

//

notes, that has been clearly noted with a proper citation in

//

the comments of my program.

//

// - I have not designed this program in such a way as to defeat or

//

interfere with the normal operation of the Curator System.

//

//

Failure to include this pledge in a submission is a violation of the Honor Code.

This is an individual assignment!

3

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download