The append, insert, and erase function are having issues I could really use some
ID: 3861553 • Letter: T
Question
The append, insert, and erase function are having issues I could really use some help with all three of them. Every time I test them I get a Segmentation fault (core dumped):
//MY CODE
#include "array_ops.h"
// Compares the contents of arrays lhs[] and rhs[].
//
// The two arrays are the equal if they both have the same usage and
// have the same values at every index up to that shared usage.
bool array_cmp(int lhs[], size_t lusage, int rhs[], size_t rusage)
{
if(lusage != rusage) //checks if number of elements in both are the same or not
{
return false;
}
int i;
for(i = 0; i < lusage; i++) //For loops through each element
{
if(lhs[i] != rhs[i]) //If both the elements in positions are not equal.
{
return false; //The arrays are not equalso returns false.
}
}
return true; //If all comparisons are successful,they are equal so return true.
}
// 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)
{
int new_size = (dusage - 1) + (susage - 1); //sum of src and dest arrays
int i;//intialize i
if (new_size > ARRAY_MAX_SIZE)
{
return *dest; //if new_size exceeds the max size of array then return dest
}
else
{
int new_dest[new_size]; //create new dest array using new size
for (i = 0; i < dusage; i++)// add every element in dest in new dest
{
new_dest[i] = dest[i];
}
int j = 0;//intitialize j, planned to use for pointing to index of src
while (i < new_size) //loop will iterate up to remaining idnexs of new dest
{
new_dest[i++] = src[j++]; //src element will append to new dest
}
return *new_dest;
}
}
// Inserts the contents of array src[] into array dest[], starting at
// the specified index.
size_t array_insert(int dest[], size_t dusage, int src[], size_t susage, size_t index)
{
int new_size = (dusage - 1) + (susage - 1);//new size is sum of dusage and susage
int i;//initialize i
if (new_size > ARRAY_MAX_SIZE || index > dusage)
{
return *dest;//returns dest if new size exceeds max or index exceeds dusage
}
else
{
int k = 0;// initialize k
int new_dest[new_size]; //creating new array
for (i = 0; i < index; i++)
{
new_dest[i] = dest[k++];//add every element up to pre index of spec.
}
int j = 0;//intitialize j
while ( j < susage) // loop will iterate up to remaining in the src
{
new_dest[i++] = src[j++]; //src array append to new dest
}
while (i < new_size) //remaining elements will append to new dest
{
new_dest[i] = dest[k++];
}
return *new_dest;
}
}
// 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)
{
int new_size = usage - count; //new size will be array size-count of elements to erase
int new_array[new_size]; //new array to store resultant array
int i = 0, j = 0; //i is pointing to indexes of array, j will be pointing to new index
if (index > usage)
{
return *array;
}
else
{
while (i < usage) // loops up to arrrays size
{
if (i > index && i < (count + index)) //if elements fall between
{
continue; // dont copy them into the new array
}
else
{
new_array[j++] = array[i];
}
i++;
}
return *new_array;
}
}
//ARRAY_OP.H
#ifndef ARRAY_OPS_H
#define ARRAY_OPS_H
#include <stdlib.h>
#include <stdbool.h>
// Every array used with the functions below must be created with
// dimension = ARRAY_MAX_SIZE.
#define ARRAY_MAX_SIZE 256
// Compares the contents of arrays lhs[] and rhs[].
//
// The two arrays are the equal if they both have the same usage and
// have the same values at every index up to that shared usage.
//
// Pre:
// lhs[] - Is an array of integers containing lusage elements.
// rhs[] - Is an array of integers containing rusage elements.
//
// Returns:
// true - If contents of the arrays are the same.
// false - If the contents of the arrays differ.
//
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[].
//
// Pre:
// dest[] - Is an array of integers containing dusage elements.
// src[] - Is an array of integers containing susage elements.
//
// Returns:
// The new usage of dest[] after the append. If the new usage would
// be > ARRAY_MAX_SIZE, no append occurs and function returns the
// original usage.
//
size_t array_append(int dest[], size_t dusage, int src[], size_t susage);
// Inserts the contents of array src[] into array dest[], starting at
// the specified index.
//
// Pre:
// dest[] - Is an array of integers containing dusage elements.
// src[] - Is an array of integers containing susage elements.
// index - The location the values will be inserted into array dest[].
//
// Post:
// The contents of src[] are inserted into dest[], starting at index.
// If index > dusage or the resulting usage would be > ARRAY_MAX_SIZE,
// no insertion occurs.
//
// Returns:
// The new usage of dest[] after the insertion. If index > dusage
// or the resulting usage would be > ARRAY_MAX_SIZE, no insertion occurs
// and the function returns the original usage.
//
// Other thoughts:
// You are going to insert "before" index. So if you insert at index = 0
// you are inserting before the first value. Further, if you were to
// insert at index = dusage, it's like appending at the end.
//
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.
//
// Pre:
// array[] - Is an array of integers containing usage elements.
//
// Post:
// If index < usage, then up to count elements have been erased from
// array[] starting at index. When index + count > usage less than
// count elements will be erased.
//
// Returns:
// The updated usage after erasing up to count elements.
//
// If index >= usage, then no erasure occurs and the function
// returns the original usage.
//
size_t array_erase(int array[], size_t usage, size_t index, size_t count);
#endif
Explanation / Answer
Please refer below code
1) array_ops.h
#ifndef ARRAY_OPS_H
#define ARRAY_OPS_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
// Every array used with the functions below must be created with
// dimension = ARRAY_MAX_SIZE.
#define ARRAY_MAX_SIZE 256
// Compares the contents of arrays lhs[] and rhs[].
//
// The two arrays are the equal if they both have the same usage and
// have the same values at every index up to that shared usage.
//
// Pre:
// lhs[] - Is an array of integers containing lusage elements.
// rhs[] - Is an array of integers containing rusage elements.
//
// Returns:
// true - If contents of the arrays are the same.
// false - If the contents of the arrays differ.
//
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[].
//
// Pre:
// dest[] - Is an array of integers containing dusage elements.
// src[] - Is an array of integers containing susage elements.
//
// Returns:
// The new usage of dest[] after the append. If the new usage would
// be > ARRAY_MAX_SIZE, no append occurs and function returns the
// original usage.
//
size_t array_append(int dest[], size_t dusage, int src[], size_t susage);
// Inserts the contents of array src[] into array dest[], starting at
// the specified index.
//
// Pre:
// dest[] - Is an array of integers containing dusage elements.
// src[] - Is an array of integers containing susage elements.
// index - The location the values will be inserted into array dest[].
//
// Post:
// The contents of src[] are inserted into dest[], starting at index.
// If index > dusage or the resulting usage would be > ARRAY_MAX_SIZE,
// no insertion occurs.
//
// Returns:
// The new usage of dest[] after the insertion. If index > dusage
// or the resulting usage would be > ARRAY_MAX_SIZE, no insertion occurs
// and the function returns the original usage.
//
// Other thoughts:
// You are going to insert "before" index. So if you insert at index = 0
// you are inserting before the first value. Further, if you were to
// insert at index = dusage, it's like appending at the end.
//
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.
//
// Pre:
// array[] - Is an array of integers containing usage elements.
//
// Post:
// If index < usage, then up to count elements have been erased from
// array[] starting at index. When index + count > usage less than
// count elements will be erased.
//
// Returns:
// The updated usage after erasing up to count elements.
//
// If index >= usage, then no erasure occurs and the function
// returns the original usage.
//
size_t array_erase(int array[], size_t usage, size_t index, size_t count);
#endif
2) my_code.c
#include "array_ops.h"
#include<limits.h>
// Compares the contents of arrays lhs[] and rhs[].
//
// The two arrays are the equal if they both have the same usage and
// have the same values at every index up to that shared usage.
bool array_cmp(int lhs[], size_t lusage, int rhs[], size_t rusage)
{
if(lusage != rusage) //checks if number of elements in both are the same or not
{
return false;
}
int i;
for(i = 0; i < lusage; i++) //For loops through each element
{
if(lhs[i] != rhs[i]) //If both the elements in positions are not equal.
{
return false; //The arrays are not equalso returns false.
}
}
return true; //If all comparisons are successful,they are equal so return true.
}
// 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)
{
int i;//intialize i
int new_size = dusage + 1 + susage + 1; //sum of src and dest arrays
if (new_size > ARRAY_MAX_SIZE)
return dusage; //if new_size exceeds the max size of array then return dest
else
{
for (i = dusage; i <= new_size; i++)// add every element in dest in new dest
{
dest[i] = src[i-dusage];
}
return new_size;
}
}
// Inserts the contents of array src[] into array dest[], starting at
// the specified index.
size_t array_insert(int dest[], size_t dusage, int src[], size_t susage, size_t index)
{
int new_dest[dusage+1]; //creating new array
int i,j,k;
if(((index + 1 + susage + 1) > ARRAY_MAX_SIZE) || (dusage < index))
return dusage;
else
{
for (i = 0; i <= dusage; i++)
{
new_dest[i] = dest[i];//add every element up to pre index of spec.
}
for(j = index; j <= (susage + 2 + index); j++)
{
dest[j] = src[j - index];
}
k = index;
for(i = j; i <= dusage;i++)
dest[i] = new_dest[k];
return i;
}
}
// 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)
{
int i = 0; //i is pointing to indexes of array, j will be pointing to new index
if (index >= usage)
{
return usage;
}
else
{
for(i = index; i <= usage; i++)
{
array[i] = INT_MIN;
}
return index;
}
}
Use below driver program to test above code
3) test_mycode.c
#include "array_ops.h"
int main()
{
int i,x;
int dest[ARRAY_MAX_SIZE];
int src[ARRAY_MAX_SIZE];
for(i = 0; i < 10; i++)
dest[i] = i+4;
for(i = 0; i < 4; i++)
dest[i] = i+9;
if(array_cmp(dest,10,src,4))
printf("Array Equal ");
else
printf("Array NOT equal ");
printf("Size after append = %d ",array_append(dest,10,src,4));
printf("Size after insert at particular index = %d ",array_insert(dest,14,src,4,3));
printf("size after erase is %d ", array_erase(dest,14,6,7));
return 0;
}
Please refer beow output
Array NOT equal
Size after append = 16
Size after insert at particular index = 15
size after erase is 6
Process returned 0 (0x0) execution time : 0.028 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.