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
41 changes: 41 additions & 0 deletions arg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 3) {
printf("usage: %s <arg1> <arg2>\n", argv[0]);
return 1;
}

char *a = *++argv;
char *b = *++argv;
char c[2] = { 0, 0 };
char *p;
char *q;
char *A;
char *z;

for (p = a; *p; p++) {

if (a <= *p && *p <= z) {
*c = *p + A - a;
}
else {
*c = *p;
printf("%s", c);
}
}

p = (char *)malloc(strlen(a) + strlen(b) + 1);
strcpy(p, a);
q = p + strlen(a);
while ((*q++ = *b++) != 0) {
;
printf("%s\n", p);
}
p = NULL;

return 0;
}
15 changes: 15 additions & 0 deletions assignment8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
What is the output? Assume that the malloc() call was successful. If you think that the program contains bugs (other than memory leaks) that can make the program crash (ex. segmentation fault), write "CAN CRASH" and then write the most likely output if the program happens to run without crashing.

abcabc1
abc12
abc123

If the program is run using valgrind, how many bytes will valgrind report as "definitely lost"? You can write 0 if you think there is no memory leak that valgrind will consider "definitely lost". Please make sure to write the number of BYTES.

0

Identify the line numbers that contains memory errors other than memory leaks (invalid access, for example.) If you think that there is no memory error other than possible memory leaks, write NONE. You don’t have to identify the nature of the memory errors. Just line numbers.

NONE

Modify the program to fix ALL memory errors, including memory leaks. Write only the lines that need to be fixed. Write the line number and the line of code that will replace that line. If you think there is nothing to fix, write "NO CHANGE". The program can be memory error free by modifying 0 to 3 lines of code. Please do not modify more than 3 lines of code.
97 changes: 97 additions & 0 deletions sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int sorting(length){

srand(time(NULL));
int i, j, q, l, m, p, c, h, f, y, r;
int*arr1=(int*)malloc(length*4);
for (i=0;i<length;i++)
{
int num = rand()%9+0;
arr1[i]=num;
}

printf("The 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)
{ //Insertion Sort algorithm taken from wikipedia
//https://en.wikipedia.org/wiki/Insertion_sort
arr2[k+1]=arr2[k];
k=k-1;
}

arr2[k+1]=x;
}

printf("\n\nThe sorted array is:\n");

for(m=0;m<length;m++)
{
printf("%d", arr2[m]);
}


int*arr3=(int*)malloc(length*4);

for (q=0;q<length;q++) {
arr3[q] = arr2[q]; //Insertion Sort implemented backwards
}
for (f=0;f<length;f++)
{
int y = arr3[f];


int h = f-1;

while (h>=0 && arr3[h] < y) {
arr3[h+1]=arr3[h];
h=h-1;
}

arr3[h+1]=y;
}

printf("\n\nThe assorted array is:\n");

for (r=0;r<length;r++)
{
printf("%d", arr3[r]);
}

printf("\n");

return 0;
}




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