Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions assignment8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Caleb Gershengorn

1:
It will print a,a (the zeroth term in the array which prints twice because it is twice not equal to 3)
I think it would then return 1 and end the function, but that seems completely wrong, but it makes sense since the code is at one point not equal to 3, thus it would return 1 and end.

2:
Based on the logic from the first answer, none of the code from underneath the return 1 runs because the return statement has terminated the code.

3:
Assuming that I am mistaken about the return ending the code, the lines with memory errors would be lines 27-28 and 19-20

4:
I honestly do not know, but I would guess that you could make the q in line 28 a dereference operator tomake sure that the memory is called and thus not lost.
59 changes: 59 additions & 0 deletions sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
//I used the sorting algorithm pseudocode from here: https://en.wikipedia.org/wiki/Insertion_sort
int sorting(length){
srand(time(NULL));
int i,j,l,m,p,q,c;
int* arr1=(int*)malloc(length*4);
for (i=0;i<length;i++)
{
int num = rand()%9+1;
arr1[i]=num;
}
printf("\n\nThe first array is: \n");
for (j=0;j<length;j++)
{
printf("%d ",arr1[j]);
}
int*arr2=(int*)malloc(length*4);
for (q=0;q<length;q++)
{
arr2[q]=arr1[q];
}
for (l=0;l<length;l++)
{
int x =arr2[l];
int k = l-1;
while (k>=0&&arr2[k] > x)
{
arr2[k+1]=arr2[k];
k=k-1;
}
arr2[k+1]=x;
}
printf("\n\nThe second array is: \n");
for (m=0;m<length;m++)
{
printf("%d ",arr2[m]);
}
int*arr3=(int*)malloc(length*4);
for (p=0;p<length;p++)
{
arr3[p]=arr2[(length-1)-p];
}
printf("\n\nThe third array is: \n");
for (c=0;c<length;c++)
{
printf("%d ",arr3[c]);
}
printf("\n");
return 0;
}

int main(){
int length;
printf("Enter the length of array you want: \n");
scanf("%d",&length);
sorting(length);
}