(Solved Homework): Table of Contents Abstract…………………………………………………………………………………………..2 1. Overview…………………………………………..

Table of Contents Abstract…………………………………………………………………………………………..2 1. Overview…………………………………………………………………………………….3 2. SmartArray.h………………………………………………………………………………..4 3. Function Requirements………………………………………………………………….5 4. Compilation and Testing (CodeBlocks)……………………………………………8 5. Compilation and Testing (Linux/Mac Command Line)……………………..9 6. Getting Started: A Guide for the Overwhelmed………………………………10 7. Troubleshooting: File Not Found Errors with Mac OS X…………………12 8. Test Cases and the test-all.sh Script……………………………………………….12 9. Deliverables……………………………………………………………………………….12 10. Grading……………………………………………………………………………………13 11. Special Restrictions…………………………………………………………………..13 Abstract In this programming assignment, you will implement smart arrays (arrays that expand automatically whenever they get too full). This is an immensely powerful and awesome data structure, and it will ameliorate several problems we often encounter with arrays in C (see pg. 3 of this PDF). By completing this assignment, you will gain advanced experience working with dynamic memory management and structs in C. You will also gain additional experience managing programs that use multiple source files. In the end, you will have an awesome and useful data structure that you can reuse in the future. Attachments SmartArray.h, testcase{01-05}.c, output{01-05}.txt, names.txt, SmartArray-diagram.pdf, and test-all.sh Deliverables SmartArray.c (Note! Capitalization and spelling of your filename matters!) 1. Overview A smart array is an array that grows to accommodate new elements whenever it gets too full. As with normal arrays in C, we have direct access to any index of the smart array at any given time. There are four main advantages to using smart arrays, though: 1. We do not need to specify the length of a smart array when it is created. Instead, it will automatically expand when it gets full. This is great when we don’t know ahead of time just how much data we’re going to end up holding in the array. 2. We will use get() and put() functions to access individual elements of the array, and these functions will check to make sure we aren’t accessing array positions that are out of bounds. (Recall that C doesn’t check whether an array index is out of bounds before accessing it during program execution. That can lead to all kinds of whacky trouble!) 3. If our arrays end up having wasted space (i.e., they aren’t full), we can trim them down to size. 4. In C, if we have to pass an array to a function, we also typically find ourselves passing its length to that function as a second parameter. With smart arrays, the length will get passed automatically with the array, as they’ll both be packaged together in a struct. While some languages offer built-in support for smart arrays (such as Java’s ArrayList class), C does not. That’s where you come in. You will implement basic smart array functionality in C, including: 1. automatically expanding the smart array’s capacity when it gets full; 2. adding new elements into arbitrary positions in the smart array, or at the end of the smart array; 3. providing safe access to elements at specific positions in the smart array; 4. gracefully signaling to the user (i.e., the programmer (re-)using your code) when he or she attempts to access an index in the smart array that is out of bounds (instead of just segfaulting); 5. … and more! In this assignment, your smart array will be designed to hold arrays of strings. A complete list of the functions you must implement, including their functional prototypes, is given below in Section 3, “Function Requirements”). You will submit a single source file, named SmartArray.c, that contains all required function definitions, as well as any auxiliary functions you deem necessary. In SmartArray.c, you should #include any header files necessary for your functions to work, including SmartArray.h (see Section 2, “SmartArray.h”). Note that you will not write a main() function in the source file you submit! Rather, we will compile your source file with our own main() function(s) in order to test your code. We have attached example source files that have main() functions, which you can use to test your code. We realize this is completely new territory for most of you, so don’t panic. We’ve included instructions on compiling multiple source files into a single executable (e.g., mixing your SmartArray.c with our SmartArray.h and testcase01.c) in Sections 4 and 5 (“Compilation and Testing”). Although we have included sample main() functions to get you started with testing the functionality of your code, we encourage you to develop your own test cases, as well. Ours are by no means comprehensive. We will use much more elaborate test cases when grading your submission. Start early. Work hard. Good luck! 2. SmartArray.h This header file contains the struct definition and functional prototypes for the smart array functions you will be implementing. You should #include this file from your SmartArray.c file, like so: #include “SmartArray.h” Recall that the “quotes” (as opposed to ) indicate to the compiler that this header file is found in the same directory as your source, not a system directory. You should not modify SmartArray.h in any way, and you should not send SmartArray.h when you submit your assignment. We will use our own unmodified copy of SmartArray.h when compiling your program. If you write auxiliary functions in SmartArray.c (which is strongly encouraged!), you should not add those functional prototypes to SmartArray.h. Just put those functional prototypes at the top of your SmartArray.c. Think of SmartArray.h as a public interface to the SmartArray data structure. It contains only the functions that the end user (i.e., the programmer (re-)using your code) should call in order to create and use a SmartArray. You do not want the end user to call your auxiliary functions directly, so you do not put those functional prototypes in SmartArray.h. That way, the end user doesn’t need to worry about all your auxiliary functions in order to use an SmartArray; everything just works. (And you don’t have to worry about the end user mucking everything up by accidentally calling auxiliary functions that he or she shouldn’t be messing around with!) The basic struct you will use to implement the smart arrays (defined in SmartArray.h) is as follows: typedef struct SmartArray { } SmartArray; char **array; int size; int capacity; // pointer to array of strings // number of elements in array // length of array (maximum capacity) The SmartArray struct contains a double char pointer that can be used to set up a 2D char array (which is just an array of char arrays, otherwise known as an array of strings). array will have to be allocated dynamically at runtime. It will probably be the bane of your existence for the next week or so. The struct also has size and capacity variables, which store the number of elements in the array (initially zero) and the current length (i.e., maximum capacity) of the array, respectively. 3. FunctionRequirements In the source file you submit, SmartArray.c, you must implement the following functions. You may implement any auxiliary functions you need to make these work, as well. Please be sure the spelling, capitalization, and return types of your functions match these prototypes exactly. In this section, I often refer to malloc(), but you’re welcome to use calloc() or realloc() instead, as you see fit. SmartArray *createSmartArray(int length); Description: Dynamically allocate space for a new SmartArray. Initialize its internal array to be of length length or DEFAULT_INIT_LEN, whichever is greater. (DEFAULT_INIT_LEN is defined in SmartArray.h.) Properly initialize pointers in the array to NULL, and set the size and capacity members of the struct to the appropriate values. Output: “-> Created new SmartArray of size .” (Output should not include the quotes. Terminate the line with a newline character, ‘n’. should of course be the length of the new array, without the angled brackets.) Returns: A pointer to the new SmartArray, or NULL if any calls to malloc() failed. SmartArray *destroySmartArray(SmartArray *smarty); Description: Free any dynamically allocated memory associated with the SmartArray struct and return NULL. Returns: NULL pointer. SmartArray *expandSmartArray(SmartArray *smarty, int length); Description: Dynamically allocate a new array of length length. Copy the contents of smarty’s old array into the new array. Free any memory associated with the old smarty→array that is no longer in use, then set smarty→array to point to the newly created array. Be sure all pointers are properly initialized. Update the size and capacity of the SmartArray (if applicable). Note: If length is less than or equal to smarty’s current array capacity, or if the smarty pointer is NULL, you should NOT modify the SmartArray at all. In that case, just return from the function right away without producing any output. Output: “-> Expanded SmartArray to size .” (Output should not include the quotes. Terminate the line with a newline character, ‘n’. should be the new length of the array, without the angled brackets. Do NOT produce any output if you the array is not expanded.) Returns: A pointer to the SmartArray, or NULL if any calls to malloc() failed. SmartArray *trimSmartArray(SmartArray *smarty); Description: If smarty’s capacity is greater than its current size, trim the length of the array to the current size. You will probably want to malloc() a new array to achieve this. If so, avoid memory leaks as you get rid of the old array. Update any members of smarty that need to be updated as a result of this action. Output: “-> Trimmed SmartArray to size .” (Output should not include the quotes. Terminate the line with a newline character, ‘n’. should be the new length of the array, without the angled brackets. Do NOT produce any output if the length of the array is not reduced by this function.) Returns: A pointer to the SmartArray, or NULL if malloc() failed or if smarty was NULL. char *put(SmartArray *smarty, char *str); Description: Insert a copy of str into the next unused cell of the array. If the array is already full, call expandSmartArray() to grow the array to length (capacity * 2 + 1) before inserting the new element. When copying str into the array, only allocate the minimum amount of space necessary to store the string. Returns: A pointer to the copy of the new string that was inserted into the array, or NULL if the string could not be added to the array (e.g., malloc() failed, or smarty or str was NULL). char *get(SmartArray *smarty, int index); Description: Attempts to return the element at the specified index. This is where you protect the user from going out-of-bounds with the array. Returns: A pointer to the string at position index of the array, or NULL if index was out of bounds or the smarty pointer was NULL. char *set(SmartArray *smarty, int index, char *str); Description: If the array already has a valid string at position index, replace it with a copy of str. Otherwise, the operation fails and we simply return NULL. Ensure that no more space is used to store the new copy of str than is absolutely necessary (so, you might have to use malloc() and free() here). Returns: A pointer to the copy of the string placed in the SmartArray, or NULL if the operation failed for any reason (e.g., invalid index, or smarty or str was NULL). char *insertElement(SmartArray *smarty, int index, char *str); Description: Insert a copy of str at the specified index in the array. Any elements to the right of index are shifted one space to the right. If the specified index is greater than the array’s size, the element being inserted should be placed in the first empty position in the array. (Continued from previous page…) As with the put() function, if the SmartArray is already full, call expandSmartArray() to grow the array to length (capacity * 2 + 1) before inserting the new element. When copying str into the array, only allocate the minimum amount of space necessary to store the string. Returns: A pointer to the copy of the string inserted into the array, or NULL if insertion fails for any reason (e.g., malloc() failed, or smarty or str was NULL). int removeElement(SmartArray *smarty, int index); Description: Remove the string at the specified index in the array. Strings to the right of index are shifted one space to the left, so as not to leave a gap in the array. The SmartArray’s size member should be updated accordingly. If index exceeds the SmartArray’s size, nothing is removed from the array. Returns: 1 if an element was successfully removed from the array, 0 otherwise (including the case where the smarty pointer is NULL). int getSize(SmartArray *smarty); Description: This function returns the number of elements currently in the array. We provide this function to discourage the programmer from accessing smarty→size directly. That way, if we decide to change the name or meaning of the size variable in our SmartArray struct, the programmers who download the latest version of our code can get it working right out of the box; they don’t have to go through their own code and change all instances of smarty→size to something else, as long as we provide them with a getSize() function that works. Returns: Number of elements currently in the array, or -1 if the smarty pointer is NULL. void printSmartArray(SmartArray *smarty); Description: Print all strings currently in the array. Output: Print all strings currently in the array. Print a newline character, ‘n’, after each string. If the SmartArray pointer is NULL, or if the array is empty, simply print “(empty array)” (without quotes), followed by a newline character, ‘n’. double difficultyRating(void); Returns: A double indicating how difficult you found this assignment on a scale of 1.0 (ridiculously easy) through 5.0 (insanely difficult). double hoursSpent(void); Returns: An estimate (greater than zero) of the number of hours you spent on this assignment.

6. GettingStarted:AGuidefortheOverwhelmed

Don't use plagiarized sources. Get Your Custom Essay on
(Solved Homework): Table of Contents Abstract…………………………………………………………………………………………..2 1. Overview…………………………………………..
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

Okay, so, this might all be overwhelming, and you might be thinking, “Where do I even start with this assignment?! I’m in way over my head!”

Don’t panic! We’re here to help in office hours, and here’s my general advice on starting the assignment (as well as a few hints and spoilers):

First and foremost, start working on this assignment early. Nothing will be more frustrating than running into unexpected errors or not being able to figure out what the assignment is asking you to do on the day that it is due.

Start by creating a skeleton SmartArray.c file. Add a header comment, add some standard #include directives, and be sure to include SmartArray.h from your source file. Then copy and paste each functional prototype from SmartArray.h into SmartArray.c, and set up all those functions return dummy values (zero, NULL, etc.). For example:

#include <stdio.h>
#include <stdlib.h>
#include “SmartArray.h”
SmartArray *createSmartArray(int length)
{
return NULL;
}
int getSize(SmartArray *smarty)
{
return 0; }

// …and so on.
3. Test that your SmartArray.c source file compiles. If you’re at the command line on a Mac or in Linux, your source file will need to be in the same directory as SmartArray.h, and you can test compilation like so:

gcc -c SmartArray.c

Alternatively, you can try compiling it with one of the test case source files, like so:

gcc SmartArray.c testcase01.c

For more details, see Section 5, “Compilation and Testing (Linux/Mac Command Line).”

If you’re using an IDE (i.e., you’re coding with something other than a plain text editor and the command line), open up your IDE and start a project using the instructions above in Section 4, “Compilation and Testing (CodeBlocks)”. Import SmartArray.h, testcase01.c, and your new SmartArray.c source file, and get the program compiling and running before you move forward. (Note that CodeBlocks is the only IDE we officially support in this class.)

Once you have your project compiling, go back to the list of required functions (Section 3, “Function Requirements”), and try to implement one function at a time. Always stop to compile and test your code before moving on to another function!

You’ll probably want to start with the createSmartArray() function. (Alternatively, printSmartArray() might be a good starting point, as well.)

As you work on createSmartArray(), write your own main() function that calls createSmartArray() and then checks the results. For example, you’ll want to ensure that createSmartArray() is returning a non-NULL pointer to begin with, and that the fields inside the SmartArray struct that it created are properly initialized when you examine them back in main(). If you’re uncertain about how to call certain functions, read through my sample main files for examples.

After writing createSmartArray(), I would probably work on the printSmartArray() function, because it will be immensely useful in debugging your code as you work. Here’s how I’d test these functions at first: In your own main() function, call createSmartArray(). Then, back in main(), manually insert one or two strings into the smart array before passing it to the printSmartArray() function. Make sure everything works as intended and the output is as expected. If not, trace carefully through your code to see what went wrong.

If you get stuck, draw diagrams. Make boxes for all the variables in your program. If you’re dynamically allocating memory, diagram them out and make up addresses for all your variables. Trace through your code carefully using these diagrams.

With so many pointers, you’re bound to encounter errors in your code at some point. Use printf() statements liberally to verify that your code is producing the results you think it should be producing (rather than making assumptions that certain components are working as intended). You should get in the habit of being immensely skeptical of your own code and using printf() to provide yourself with evidence that your code does what you think it does.

When looking for a segmentation fault, you should always be able to use printf() and fflush() to track down the exact line you’re crashing on.

You’ll need to examine a lot of debugging output. You might want to set up a function that prints debugging strings only when you #define a DEBUG value to be something other than zero, so you can easily flip debugging output on and off. (Just be sure to remove your debugging statements before you submit your assignment, so your code is nice and clean and easy for us to read.)

Names

Karan
Kristjan
Shahidul
Rachael
Daniel
Fereshteh
Richie
Pierre

Contents of SmartArray.h

#ifndef __SMART_ARRAY_H

#define __SMART_ARRAY_H

// Default capacity for new SmartArrays

#define DEFAULT_INIT_LEN 10

typedef struct SmartArray

{

// We will store an array of strings (i.e., an array of char arrays)

char **array;

// Size of array (i.e., number of elements that have been added to the array)

int size;

// Length of the array (i.e., the array’s current maximum capacity)

int capacity;

} SmartArray;

// Functional Prototypes

SmartArray *createSmartArray(int length);

SmartArray *destroySmartArray(SmartArray *smarty);

SmartArray *expandSmartArray(SmartArray *smarty, int length);

SmartArray *trimSmartArray(SmartArray *smarty);

char *put(SmartArray *smarty, char *str);

char *get(SmartArray *smarty, int index);

char *set(SmartArray *smarty, int index, char *str);

char *insertElement(SmartArray *smarty, int index, char *str);

int removeElement(SmartArray *smarty, int index);

int getSize(SmartArray *smarty);

void printSmartArray(SmartArray *smarty);

double difficultyRating(void);

double hoursSpent(void);

#endif

Contents of test-all.sh

#!/bin/bash

# =======================

# SmartArray: test-all.sh

# =======================

# You can run this script at the command line like so:

#

# bash test-all.sh

#

# For more details, see the assignment PDF.

PASS_CNT=0

NUM_TEST_CASES=5

for i in `seq -f “%02g” 1 $NUM_TEST_CASES`;

do

echo -n “Checking testcase$i… ”

# Attempt to compile.

gcc SmartArray.c testcase$i.c -lm 2> /dev/null

compile_val=$?

if [[ $compile_val != 0 ]]; then

echo “fail (failed to compile)”

continue

fi

# Run program. Capture return value to check whether it crashes.

./a.out > myoutput$i.txt 2> /dev/null

execution_val=$?

if [[ $execution_val != 0 ]]; then

echo “fail (program crashed)”

continue

fi

# Run diff and capture its return value.

diff myoutput$i.txt output$i.txt > /dev/null

diff_val=$?

 

# Output results based on diff’s return value.

if [[ $diff_val != 0 ]]; then

echo “fail (output does not match)”

else

echo “PASS!”

PASS_CNT=`expr $PASS_CNT + 1`

fi

done

# Clean up the executable file.

rm -f a.out

# Clean up the output files generated by this script.

for i in `seq -f “%02g” 1 $NUM_TEST_CASES`;

do

rm -f myoutput$i.txt

done

if [ $PASS_CNT -eq $NUM_TEST_CASES ]; then

echo “”

echo “”

echo “CONGRATULATIONS! You appear to be passing all the test cases!!”

echo “”

echo “Now, have you considered writing some additional ones of your own?”

echo “Keep in mind, the test cases I wrote for you are just a sort of”

echo “starter pack, designed to show you how you can write test cases for”

echo “your programs, which you can do even before you’ve completed the”

echo “functions you’re echo working on. You should create additional test”

echo “cases in order to fully test the functionality and correctness of”

echo “your program.”

echo “”

else

echo “”

echo ” .”

echo ” “:””

echo ” ___:____ |”\/”|”

echo ” ,’ `. \ /”

echo ” | o \___/ |”

echo ” ~^~^~^~^~^~^~^~^~^~^~^~^~”

echo “”

echo ” (fail whale)”

echo “”

echo “Note: The fail whale is friendly and adorable! He is not here to”

echo ” demoralize you, but rather, to bring you comfort and joy in”

echo ” your time of need. “Keep plugging away,” he says! “You”

echo ” can do this!””

echo “”

fi

Contents of testcase01.c

// ========================

// SmartArray: testcase01.c

// ========================

#include <stdio.h>

#include <string.h>

#include “SmartArray.h”

int main(void)

{

int i; char buffer[32];

SmartArray *smarty1 = createSmartArray(-1);

SmartArray *smarty2 = createSmartArray(-1);

FILE *ifp = fopen(“names.txt”, “rb”);

// Read all names from the file and add them to smarty1.

while (fscanf(ifp, “%s”, buffer) != EOF)

put(smarty1, buffer);

// Add the names to smarty2 in reverse order.

for (i = getSize(smarty1) – 1; i >= 0; i–)

put(smarty2, get(smarty1, i));

// Print the contents of smarty1.

printf(“n– SMART ARRAY 1: –n”);

printSmartArray(smarty1);

// Print the contents of smarty2.

printf(“n– SMART ARRAY 2 (First Names): –n”);

printSmartArray(smarty2);

// Swap last names with first names in smarty2.

for (i = 0; i < getSize(smarty2); i++)

{

if (strcmp(get(smarty2, i), “Daniel”) == 0)

set(smarty2, i, “Mandragona”);

else if (strcmp(get(smarty2, i), “Kristjan”) == 0)

set(smarty2, i, “Arumae”);

else if (strcmp(get(smarty2, i), “Karan”) == 0)

set(smarty2, i, “Daei-Mojdehi”);

else if (strcmp(get(smarty2, i), “Shahidul”) == 0)

set(smarty2, i, “Islam”);

else if (strcmp(get(smarty2, i), “Fereshteh”) == 0)

set(smarty2, i, “Jafariakinabad”);

else if (strcmp(get(smarty2, i), “Pierre”) == 0)

set(smarty2, i, “LaBorde”);

else if (strcmp(get(smarty2, i), “Rachael”) == 0)

set(smarty2, i, “Sera”);

else if (strcmp(get(smarty2, i), “Richie”) == 0)

set(smarty2, i, “Wales”);

}

// Print the contents of smarty2.

printf(“n– SMART ARRAY 2 (Last Names): –n”);

printSmartArray(smarty2);

// Print smarty1 (in reverse order) and smarty2, to match up first and last

// names.

printf(“n– COMBINED ARRAYS (First and Last Names): –n”);

for (i = 0; i < getSize(smarty2); i++)

printf(“%s %sn”, get(smarty1, getSize(smarty1) – 1 – i), get(smarty2, i));

// Add elements from smarty1 to the end of smarty1 (in reverse order).

printf(“n”);

for (i = getSize(smarty1) – 1; i >= 0; i–)

printf(“Adding %s to smarty1 …n”, put(smarty1, get(smarty1, i)));

// Print the contents of smarty1.

printf(“n– SMART ARRAY 1: –n”);

printSmartArray(smarty1);

// Insert a string at the beginning of array smarty1.

insertElement(smarty1, 0, “List of Names:”);

// Print the contents of smarty1.

printf(“n– SMART ARRAY 1: –n”);

printSmartArray(smarty1);

// Remove all elements from smarty1.

while (getSize(smarty1))

removeElement(smarty1, 0);

// Print smarty1, which is now an empty array.

printf(“n– SMART ARRAY 1: –n”);

printSmartArray(smarty1);

// Destroy our smart arrays.

smarty1 = destroySmartArray(smarty1);

smarty2 = destroySmartArray(smarty2);

// Make sure smarty1 is good and destroyed (and that destroySmartArray

// doesn’t segfault when passed a NULL pointer).

smarty1 = destroySmartArray(smarty1);

// Print the empty arrays one last time.

printf(“n– SMART ARRAY 1: –n”);

printSmartArray(smarty1);

printf(“n– SMART ARRAY 2: –n”);

printSmartArray(smarty2);

return 0;

}

Contents of testcase02.c

#include <stdio.h>

#include “SmartArray.h”

int main(void)

{

SmartArray *smarty = createSmartArray(-1);

// Trim smart array. This should reduce its capacity to 0.

trimSmartArray(smarty);

// Trim again. This should produce no output since capacity is already 0.

trimSmartArray(smarty);

// Print the empty array.

printSmartArray(smarty);

printf(“Size of array: %dn”, getSize(smarty));

// Destroy smart array. Hopefully this won’t segfault.

smarty = destroySmartArray(smarty);

// Print the empty array.

printSmartArray(smarty);

printf(“Size of array: %dn”, getSize(smarty));

// Since smarty has been destroyed and is now NULL, the put() function should

// return NULL

if (put(smarty, “Hello, world!n”) == NULL)

printf(“Awesome!n”);

else

printf(“Not so awesome!n”);

// Create new array and add strings to the end. Print out the strings we’re

// adding (to ensure put() is returning pointers to those strings).

printf(“%sn”, put(smarty = createSmartArray(-1), “Hello,”));

printf(“%sn”, put(smarty, “world!”));

// Print smarty again.

printf(“SmartArray contents:n”);

printSmartArray(smarty);

smarty = destroySmartArray(smarty);

return 0;

}

Expert Answer

 

SmartArray.c
———————————————–
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include “SmartArray.h”

// this creates a smart array pointer
// our code should create an array that dynamically increases or decreases
SmartArray *createSmartArray(int length)
{
int n=0, i, capacity;

// Dynamically allocate space for a new SmartArray
SmartArray *smarty;
char **temp;

temp = NULL;

smarty = malloc(sizeof(SmartArray));

if(smarty==NULL)
return NULL;

smarty->size = 0;

// Initialize its internal array to be of length length or
// DEFAULT_INIT_LEN
if(length <= DEFAULT_INIT_LEN)
{
temp = malloc(sizeof(char * ) * DEFAULT_INIT_LEN);
n = DEFAULT_INIT_LEN;
}
else if (length > DEFAULT_INIT_LEN )
{
temp = malloc(sizeof(char * ) * length);

n = length;
}

smarty->capacity = n;

if (temp == NULL)
return NULL;

smarty->array=temp;

// initialize pointers in the array to NULL
for (i=0; i<n; i++)
smarty->array[i] = NULL;

printf(“-> Created new SmartArray of size %d.n”, n );

return smarty;
}

// this function should print the contents of array
// if the array is empty it prints empty array
void printSmartArray(SmartArray *smarty)
{

int i, siz, capacity, n;

if(smarty==NULL)
{
printf(“(empty array)n”);
return;
}

siz = smarty->size;
capacity = smarty->capacity;

if (smarty->array==NULL)
{
printf(“(empty array)n”);
return;
}
else if(smarty->array[0]==NULL)
{
printf(“(empty array)n”);
return;
}
else if(smarty->array!=NULL)
{
for(i=0; i<capacity; i++)
{
if(smarty->array[i]!=NULL)
printf(“%sn”, smarty -> array[i]);
}
}

}

// free the smart array in reverse order of allocation
// must free the memory in order to avoid leaks
SmartArray *destroySmartArray(SmartArray *smarty)
{
int i, j;

if(smarty==NULL)
return NULL;

// if smarty still exists but the array is null
// must delete smarty and lurking array
if(smarty->array==NULL)
{
free(smarty->array);
free(smarty);
return NULL;
}

for(i=0; i<smarty->capacity; i++)
{
free(smarty->array[i]);
}

free(smarty->array);
free(smarty);

return NULL;
}

char *put(SmartArray *smarty, char *str)
{

// use strlen to find string length
// insert copy of str in next unused cell

int slen , size, capacity;
char *tempVar;

// may want to create a temp variable to store str
tempVar=NULL;

if(smarty==NULL)
return NULL;

if(str==NULL)
return NULL;

size = smarty->size;
capacity = smarty->capacity;
slen = strlen(str);

// expands the array if full
// but really just creates an array of capacity*2 +1 and destroys old
if (size==capacity)
{
expandSmartArray( smarty, (capacity * 2 + 1));

}

tempVar = malloc(sizeof(char) * (slen+1));

if(tempVar==NULL)
return NULL;

strcpy(tempVar, str);

smarty->array[size] = tempVar;

if (str== NULL||smarty->array== NULL||smarty==NULL)
return NULL;

smarty->size = size + 1;

// return the contents of string pointer
return smarty->array[size];

}

// expands smartArray to size of length
SmartArray *expandSmartArray(SmartArray *smarty, int length)
{
int i, cap, si;
char **tempArray;

if(smarty==NULL)
return NULL;

if(smarty->array==NULL)
return NULL;

tempArray=NULL;
si = smarty->size;
cap = smarty->capacity;

if(length <= cap)
{
return NULL;

}

if(cap<length){

// Initialize its internal array to be of length length or DEFAULT_INIT_LEN
tempArray = malloc(sizeof(char * ) * length);

if(tempArray == NULL)
return NULL;

// next copy the old addresses to the temp
for(i=0; i<si; i++)
{
// array is basically temp variable
tempArray[i] = smarty->array[i];

}

for (i=si; i<length; i++)
{
tempArray[i]= NULL;
}

// erase the old smarty array
free(smarty->array);

// copies the temp array address to the old array
smarty->array = tempArray;

smarty->capacity = length;

printf(“-> Expanded SmartArray to size %d.n”, length);
}
else
{
return NULL;
}

return smarty;
}

// returns the element at the index
// this function protects the user from going out of bounds with the array
char *get(SmartArray *smarty, int index)
{
// if index was out of bounds or if the smarty pointer was null
if((index < 0) || (index > smarty->capacity)|| smarty==NULL)
return NULL;

return smarty->array[index];

}

// sets a string at the index indicated
// if no string null; if it does replace
char *set(SmartArray *smarty, int index, char *str)
{
char *tempVar;
int slen, size;

if((index < 0) || (index > smarty->capacity)|| smarty==NULL||str==NULL)
return NULL;

size = smarty->size;
tempVar=NULL;
slen = strlen(str);

if(smarty->array[index]==NULL)
return NULL;

tempVar = malloc(sizeof(char) * (slen+1));

if(tempVar==NULL)
return NULL;

strcpy(tempVar, str);

free(smarty->array[index]);

smarty->array[index]=tempVar;

if(smarty->array[index] != tempVar)
return NULL;

return smarty->array[index];
}

// insert copy of str at the specified index
// shift all others to the left
char *insertElement(SmartArray *smarty, int index, char *str)
{
// use strlen to find string length
// insert copy of str in next unused cell

int slen , size, capacity, i;
char *tempVar;
char **tempArray;

if((index < 0) || smarty==NULL||str==NULL)
return NULL;

size = smarty->size;
capacity = smarty->capacity;
slen = strlen(str);
tempVar = NULL;
tempArray=NULL;

tempVar = malloc(sizeof(char) * (slen+1));

if(tempVar==NULL)
return NULL;

strcpy(tempVar, str);

if (size==capacity)
{

expandSmartArray( smarty, (capacity * 2 + 1));

}

if (index>=size)
{
smarty->array[size]=tempVar;

if(smarty->array[size] != tempVar)
return NULL;

smarty->size= size +1;
}
else if(index<size)
{

tempArray = malloc(sizeof(char * ) * capacity);

if(tempArray == NULL)
return NULL;

if(index>0)
{
for(i=0; i<index; i++)
tempArray[i]=smarty->array[i];
}

tempArray[index]=tempVar;

// next copy the old addresses to the temp
for(i=index; i<size; i++)
{

tempArray[i+1] = smarty->array[i];

}

for (i=size+1; i<capacity; i++)
{
tempArray[i]= NULL;
}

// copies the temp array address to the old array
smarty->array = tempArray;

smarty->size= size +1;

}

// elements are shifted to the right one space

if(smarty->array[index]==tempVar)
return smarty->array[index];

return NULL;

}

// remove the string at the specified index in array
// no gaps left
int removeElement(SmartArray *smarty, int index)
{

int i, capacity, size;

if (index<0 || smarty==NULL)
return 0;

size = smarty->size;
capacity = smarty->capacity;

if(size<=index)
return 0;
if(smarty->array==NULL)
return 0;
if(smarty->array[index]==NULL)
return 0;

smarty->array[index]= NULL;

size = smarty->size;

for(i=index;i<size; i++)
{

smarty->array[i]=smarty->array[i+1];
}

for(i=size;i<capacity;i++)
{

smarty->array[i]=NULL;
}

smarty->size = size-1;

return 1;
}

int getSize(SmartArray *smarty)
{
int size;

if(smarty==NULL)
return -1;

size = smarty->size;

if(smarty->capacity==0)
return size;

if(smarty->array!=NULL)
return size;

return -1;
}

// trim any extra nodes from the array
SmartArray *trimSmartArray(SmartArray *smarty)
{
int capacity, size, i;
char **tempArray;

tempArray=NULL;

if(smarty==NULL)
return NULL;

size = smarty->size;
capacity = smarty ->capacity;

if(capacity==0)
return NULL;

// what happens if the size is zero
if (size == 0)
{
for(i=0; i<capacity; i++)
free(smarty->array[i]);

free(smarty->array);

smarty->array=NULL;

smarty->capacity=0;
}
else if (capacity>size)
{
tempArray=malloc(sizeof(char*)*size);

if(tempArray == NULL)
return NULL;

smarty->capacity = size;

for(i=0; i<size; i++)
{
tempArray[i] = smarty->array[i];
}

free(smarty->array);

smarty->array=tempArray;
}

// output if length trimmed
printf(“-> Trimmed SmartArray to size %d.n”, size);

return smarty;

}

double difficultyRating(void)
{
return 5.0;
}

double hoursSpent(void)
{
return 40.0;
}
—————————————————————————-
SmartArray.h
—————————————-
#ifndef __SMART_ARRAY_H
#define __SMART_ARRAY_H

// Default capacity for new SmartArrays
#define DEFAULT_INIT_LEN 10

typedef struct SmartArray
{
// We will store an array of strings (i.e., an array of char arrays)
char **array;

// Size of array (i.e., number of elements that have been added to the array)
int size;

// Length of the array (i.e., the array’s current maximum capacity)
int capacity;

} SmartArray;

// Functional Prototypes

SmartArray *createSmartArray(int length);

SmartArray *destroySmartArray(SmartArray *smarty);

SmartArray *expandSmartArray(SmartArray *smarty, int length);

SmartArray *trimSmartArray(SmartArray *smarty);

char *put(SmartArray *smarty, char *str);

char *get(SmartArray *smarty, int index);

char *set(SmartArray *smarty, int index, char *str);

char *insertElement(SmartArray *smarty, int index, char *str);

int removeElement(SmartArray *smarty, int index);

int getSize(SmartArray *smarty);

void printSmartArray(SmartArray *smarty);

double difficultyRating(void);

double hoursSpent(void);

#endif
————————————————————-
testcase01.c / main
——————————————-
#include <stdio.h>
#include <string.h>
#include “SmartArray.h”

int main(void)
{
int i; char buffer[32];

SmartArray *smarty1 = createSmartArray(-1);
SmartArray *smarty2 = createSmartArray(-1);

FILE *ifp = fopen(“names.txt”, “rb”);

// Read all names from the file and add them to smarty1.
while (fscanf(ifp, “%s”, buffer) != EOF)
put(smarty1, buffer);

// Add the names to smarty2 in reverse order.
for (i = getSize(smarty1) – 1; i >= 0; i–)
put(smarty2, get(smarty1, i));

// Print the contents of smarty1.
printf(“n– SMART ARRAY 1: –n”);
printSmartArray(smarty1);

// Print the contents of smarty2.
printf(“n– SMART ARRAY 2 (First Names): –n”);
printSmartArray(smarty2);

// Swap last names with first names in smarty2.
for (i = 0; i < getSize(smarty2); i++)
{
if (strcmp(get(smarty2, i), “Daniel”) == 0)

set(smarty2, i, “Mandragona”);

else if (strcmp(get(smarty2, i), “Kristjan”) == 0)

set(smarty2, i, “Arumae”);

else if (strcmp(get(smarty2, i), “Karan”) == 0)

set(smarty2, i, “Daei-Mojdehi”);

else if (strcmp(get(smarty2, i), “Shahidul”) == 0)

set(smarty2, i, “Islam”);

else if (strcmp(get(smarty2, i), “Fereshteh”) == 0)

set(smarty2, i, “Jafariakinabad”);

else if (strcmp(get(smarty2, i), “Pierre”) == 0)

set(smarty2, i, “LaBorde”);

else if (strcmp(get(smarty2, i), “Rachael”) == 0)

set(smarty2, i, “Sera”);

else if (strcmp(get(smarty2, i), “Richie”) == 0)

set(smarty2, i, “Wales”);
}

// Print the contents of smarty2.
printf(“n– SMART ARRAY 2 (Last Names): –n”);
printSmartArray(smarty2);

// Print smarty1 (in reverse order) and smarty2, to match up first and last
// names.
printf(“n– COMBINED ARRAYS (First and Last Names): –n”);
for (i = 0; i < getSize(smarty2); i++)
printf(“%s %sn”, get(smarty1, getSize(smarty1) – 1 – i), get(smarty2, i));

// Add elements from smarty1 to the end of smarty1 (in reverse order).
printf(“n”);
for (i = getSize(smarty1) – 1; i >= 0; i–)
printf(“Adding %s to smarty1 …n”, put(smarty1, get(smarty1, i)));

// Print the contents of smarty1.
printf(“n– SMART ARRAY 1: –n”);
printSmartArray(smarty1);

// Insert a string at the beginning of array smarty1.
insertElement(smarty1, 0, “List of Names:”);

// Print the contents of smarty1.
printf(“n– SMART ARRAY 1: –n”);
printSmartArray(smarty1);

// Remove all elements from smarty1.
while (getSize(smarty1))
removeElement(smarty1, 0);

// Print smarty1, which is now an empty array.
printf(“n– SMART ARRAY 1: –n”);
printSmartArray(smarty1);

// Destroy our smart arrays.
smarty1 = destroySmartArray(smarty1);
smarty2 = destroySmartArray(smarty2);

// Make sure smarty1 is good and destroyed (and that destroySmartArray
// doesn’t segfault when passed a NULL pointer).
smarty1 = destroySmartArray(smarty1);

// Print the empty arrays one last time.
printf(“n– SMART ARRAY 1: –n”);
printSmartArray(smarty1);

printf(“n– SMART ARRAY 2: –n”);
printSmartArray(smarty2);

return 0;
}
——————————————————————–
names.txt
——————————
Karan
Kristjan
Shahidul
Rachael
Daniel
Fereshteh
Richie
Pierre

Homework Ocean
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents