Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having a problem with my code. My first function won\'t return the statemen

ID: 3861260 • Letter: I

Question

I'm having a problem with my code. My first function won't return the statement for the case of 'true' and when I test the rest I get a Segmentation fault (core dumped) error. If you need to see how I'm testing it I can also add that code.

//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)

{

   int count = 0, i;
   for (i = 0; i < lusage; i++)
   {
       if (lhs[i] == rhs[i])
       {
           count++;
       }
       else
       {
           break;
       }
   }
   if (count == (lusage - 1))
   {
       return true;
   }
   else
   {
       return false;
   }

}

// 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);
   int i;
   if (new_size > ARRAY_MAX_SIZE)
   {
       return *dest;
   }
   else
   {
       int new_dest[new_size];
       for (i = 0; i < dusage; i++)
       {
           new_dest[i] = dest[i];
       }
       int j = 0;
       while (i < new_size)
       {
           new_dest[i++] = src[j++];
       }
       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);
   int i;
   if (new_size > ARRAY_MAX_SIZE || index > dusage)
   {
       return *dest;
   }
   else
   {
       int k = 0;
       int new_dest[new_size];
       for (i = 0; i < index; i++)
       {
           new_dest[i] = dest[k++];
       }
       int j = 0;
       while ( j < susage)
       {
           new_dest[i++] =src[j++];
       }
       while (i < new_size)
       {
           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;
   int new_array[new_size];
   int i = 0, j = 0;
   if (index > usage)
   {
       return *array;
   }
   else
   {
       while (i < usage)
       {
           if (i > index && i < (count + index))
           {
               continue;
           }
           else
           {
               new_array[j++] = array[i];
           }
           i++;
       }
       return *new_array;
   }

}

Explanation / Answer

Here is the modified version of the first function:

// 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)   // If the number of elements in both elements are not same,
                       // the arrays are not equal.
   return false;
int count = 0, i;
for(int i = 0; i < lusage; i++)   //For each element in the arrays.
   if(lhs[i] != rhs[i])       //If both the elements in relavent positions are not equal.
       return false;           //The arrays are not equal.
return true;                   //If all comparisons are successful, the arrays are equal.
}

If it still didn't work, please respond along with the tester code.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote