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
13 changes: 13 additions & 0 deletions Part2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Matthew Danielson
7/11/16
Questions for Part 2


1) It will print usage: a <arg1> <arg2>, because the first number is 1, not 3. If that is not the case - but I believe it is - then this code wil print out the address of pointer c, and will continue to print out the addresses of pointer p in the final lines of the code.

2) 1 byte from the original address of p; all of the other values are not values that are manually and dynamically created, thus they are not memory leaks.

3) Line 28

4) Add before the end of main - line 34 - free(q);
On Line 28, the & operator is necessary to get the address of the pointer; otherwise, this code is not valid.
47 changes: 47 additions & 0 deletions sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Matthew Danielson
* 7/11/16
* sort.c
* Sorts random numbers by ascending and descending order
*/

#include <math.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
srand(time(NULL));
printf("How long will your set of numbers be?");
int length;
scanf("%d", &length);
int *array = (int*) malloc(length*sizeof(int));
int *ascending = (int*) malloc(length*sizeof(int));
int *descending = (int*) malloc(length*sizeof(int));
for(int x = 0; x<length; x++){
array[x] =(int) (100*(((float)rand())/RAND_MAX));
ascending[x] = array[x];
descending[x] = array[x];
}
for(int x = 0; x< length; x++){
for(int y = 0; y < length-1; y++){
if(ascending[y] > ascending[y+1]){
int temp = ascending[y+1];
ascending[y+1] = ascending[y];
ascending[y] = temp;

}

}

}
for(int x = 0; x<=length; x++){
descending[x] = ascending[length-x-1];

}
for(int x = 0; x<length; x++){
printf("%5d, %5d, %5d\n", array[x], ascending[x], descending[x]);

}
free(array);
free(ascending);
free(descending);
}