Question

In: Computer Science

Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...

Debug this code getting segmentation faults.

//array_utils.h

int contains(const int *arr,int size , int k);
int containsWithin(const int *arr,int size , int k,int i , int j);
int *paddedCopy(const int *arr,int oleSize , int newSize);
void reverse(int *arr,int size);
int *reverseCopy(const int *arr,int size);

//array_utils.c

#include"array_utils.h"
#include<stdlib.h>

int contains(const int *arr,int size , int k){
   int i=0;
   for(i=0;i<size;i++){
       if(arr[i] == k)return 1;
   }
   return 0;
}
int containsWithin(const int *arr,int size , int k,int i , int j){
   while(i<=j){
       if(arr[i] == k)return 1;
       i++;
   }
   return 0;
}
int *paddedCopy(const int *arr,int oldSize , int newSize){
   int * newArr = (int*)malloc(newSize*sizeof(int));
   int i;
   if(newSize<=oldSize){
       for(i=0;i<newSize;i++){
           newArr[i] = arr[i];
       }
   }
   else{
       for(i=0;i<oldSize;i++){
           newArr[i] = arr[i];
       }
   }
   return newArr;
}
void reverse(int *arr,int size){
   int i=0,j=size-1;
  
   while(i<j){
       int temp = arr[i];
       arr[i] = arr[j];
       arr[j] = temp;
       i++;
       j--;
   }
}
int *reverseCopy(const int *arr,int size){
   int * newArr = (int*)malloc(size*sizeof(int));
   int i;
   for(i=0;i<size;i--){
       newArr[i] = arr[size-i-1];
   }
   return newArr;
}

Solutions

Expert Solution

I have added a main function to test the functions and added a new function print() to print array elements. In reversecopy function variable i should be incremented.

In main() create an array and call each function to test it.

//array_utils.h

int contains(const int *arr,int size , int k);
int containsWithin(const int *arr,int size , int k,int i , int j);
int *paddedCopy(const int *arr,int oleSize , int newSize);
void reverse(int *arr,int size);
int *reverseCopy(const int *arr,int size);
//array_utils.c

#include"array_utils.h"
#include<stdlib.h>
#include<stdio.h>

int contains(const int *arr,int size , int k){
   int i=0;
   for(i=0;i<size;i++){
       if(arr[i] == k)return 1;
   }
   return 0;
}
int containsWithin(const int *arr,int size , int k,int i , int j){
   while(i<=j){
       if(arr[i] == k)return 1;
       i++;
   }
   return 0;
}
int *paddedCopy(const int *arr,int oldSize , int newSize){
   int * newArr = (int*)malloc(newSize*sizeof(int));
   int i;
   if(newSize<=oldSize){
       for(i=0;i<newSize;i++){
           newArr[i] = arr[i];
       }
   }
   else{
       for(i=0;i<oldSize;i++){
           newArr[i] = arr[i];
       }
   }
   return newArr;
}
void reverse(int *arr,int size){
   int i=0,j=size-1;
 
   while(i<j){
      
       int temp = arr[i];
       arr[i] = arr[j];
       arr[j] = temp;
       i++;
       j--;
      
   }
  }
void print(int *arr,int size)//Function to display array elements
{int i;
    for(i=0;i<size;i++)
    printf("%d ",arr[i]);
    printf("\n");
}
int *reverseCopy(const int *arr,int size){
   int * newArr = (int*)malloc(size*sizeof(int));
   int i;
 
   for(i=0;i<size;i++)//Here instead of incrimenting i,you used i-- which wil make the for loop run without exit
   {
       newArr[i] = arr[size-1-i];
       
   }
  
   return newArr;
}

//Create a main function to test functions
void main()
{
   int arr[10]={1,2,3,4};
   int *b,i;    
   print(arr,4);
   reverse(arr,4);
   print(arr,4);
   b=reverseCopy(arr,4);
   print(b,4);
   b=paddedCopy(arr,4,7);
   print(b,4);
b=paddedCopy(arr,4,2);
print(b,2);
printf("Searching for 20 in array  %d\n",contains(arr,4,20));
printf("Searching for 3 in array between 0 and 2nd position= %d",containsWithin(arr,4,3,0,2));
}

SAMPLE OUTPUT

1 2 3 4
4 3 2 1
1 2 3 4
4 3 2 1
4 3
Searching for 20 in array 0
Searching for 3 in array between 0 and 2nd position= 1


Related Solutions

code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int stateLength = 6; const int cityLength = 20; const int streetLength = 30; int custNomber = -1; // Structure struct Customers { long customerNumber; char name[nameLength]; char state[stateLength]; char city[cityLength]; char streetAddress1[streetLength]; char streetAddress2[streetLength]; char isDeleted = 'N'; char newLine = '\n'; int zipCode; }; int main() { ofstream file; file.open("Customers.dat", fstream::binary | fstream::out); char go; long entries = 0; struct Customers data; do...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
Convert the following C++ statements to an ARM assembly language program: const int size = 10;...
Convert the following C++ statements to an ARM assembly language program: const int size = 10; int x[size] = {8, 2, 9, 6, 7, 0, 1, 3, 5, 4}; int y[size] = {399, -87, 12, 0, 42, -367, 57, 92, -1000, 25}; for i = 0; i < size; i++) if (x([ i ] > y[ i ]) z[ i ] = 0 else z[ i ] = 1;
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
Replace the todo comments with the right code. //Create variables to use later const int TRIG_PIN...
Replace the todo comments with the right code. //Create variables to use later const int TRIG_PIN = 9; const int ECHO_PIN = 10; const int RED_PIN = 3; const int GREEN_PIN = 5; const int BLUE_PIN = 6; float duration, distance_in_cm, distance_in_feet; void setup() { //Setup pins for correct I/O pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); } void loop() { //Generate the ultrasonic waves digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); //Read in the echoed...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT