In: Computer Science
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;
}
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