void menu() { my_initialize_heap(4000); int size; printf("enter a positive interger\n"); scanf_s("%d", &size); int *arr = (int*)my_alloc(sizeof(int) * size); int x; double mean = 0; for (int i = 0; i < size; i++) { scanf_s("%d", &x); arr[i] = x; mean += x; } mean = mean / size; double std_dev = 0; for (int i = 0; i < size; i++) { std_dev += pow(arr[i] - mean, 2); } std_dev = sqrt(std_dev / size); printf("Standard Deviation: %f\n", std_dev); system("pause"); }
int main(){ my_initialize_heap(5000); /*printf("TEST 1\n"); int *first =(int*) my_alloc(sizeof(int)); printf("%p\n",first); my_free(first); int *second =(int*) my_alloc(sizeof(int)); printf("%p\n",second); my_free(second);*/ printf("TEST 2\n"); int* one = (int*)my_alloc(sizeof(int)); printf("%d\n", one); int* two = (int*)my_alloc(sizeof(int)); printf("%d\n",two ); /* printf("TEST 3\n"); my_initialize_heap(50); int *uno =(int*) my_alloc(sizeof(int)); printf("%p\t\n",uno); int *dos = (int*)my_alloc(sizeof(int)); printf("%p\t\n",dos); int *tres = (int*)my_alloc(sizeof(int)); printf("%p\t\n",tres); my_free(dos); double *d_tres = (double*)my_alloc(sizeof(double)); printf("\nNew Allocation\n"); printf("%p\t\n",d_tres); int *tres_new = (int*)my_alloc(sizeof(int)); printf("%p\t\n", tres_new);*/ /*printf("TEST 4\n"); my_initialize_heap(50); char *word = (char*)my_alloc(sizeof(unsigned char*)); printf("%p\t\n",word); int *numero = (int*)my_alloc(sizeof(int)); printf("%p\n",numero*/ /*printf("TEST 5\n"); int* array = (int*)my_alloc((100 * sizeof(int))); int* int_val = (int*)my_alloc(sizeof(int)); printf("array: %p\t",array); printf("int value: %p\t\n",int_val); my_free(array); printf("Array has been freed\n"); printf("array: %p\t",array); printf("int value: %p\t\n",int_val);*/ //standard_deviation(); return 0; }
void test_4() { my_initialize_heap(300); printf("-----Test Case 4-----\n"); char *a = (char*)my_alloc(sizeof(unsigned char*)); printf("char a: %p\t\n", a); int *b = (int*)my_alloc(sizeof(int)); printf("int a: %p\t\n", b); system("pause"); }
void test_5() { my_initialize_heap(3000); int* arr = (int*)my_alloc((100 * sizeof(int))); int* i = (int*)my_alloc(sizeof(int)); printf("address: %p\taddress: %p\n", arr, i); my_free(arr); printf("address: %p\taddress: %p\n", arr, i); system("pause"); }
void test_1() { my_initialize_heap(300); printf("-----Test Case 1-----\n"); int *x = (int*)my_alloc(sizeof(int)); printf("%p\t\n",x); my_free(x); int *y = (int*)my_alloc(sizeof(int)); printf("%p\n", y); my_free(y); system("pause"); }
void test_3() { my_initialize_heap(300); printf("-----Test Case 3-----\n"); int *a = (int*)my_alloc(sizeof(int)); printf("int a: %p\t\n", a); int *b = (int*)my_alloc(sizeof(int)); printf("int b: %p\n", b); int *c = (int*)my_alloc(sizeof(int)); printf("int c: %p\n", c); my_free(b); double *d = (double*)my_alloc(sizeof(double)); printf("double d: %p\n", d); int *e = (int*)my_alloc(sizeof(int)); printf("int e: %p\n", e); system("pause"); }
/* Coolects user data for calculating the standard deviation */ void program(){ printf("\nPlease input the value of n (integer)\n"); int size = 0; scanf("%i", &size); printf("%i entered \n", size); my_initialize_heap((int)((size * psize) + msize + psize)); int* int_array = my_alloc((int)(size * psize)); int i; for(i = 0; i < (int)(size); i++){ int data = 0; printf("Please input integer number %d:\n", i); scanf("%i", &data); *(int_array + i) = (int)data; } std_deviation(int_array, size); free(buffer); }
/* The minimum test requirements for the class with console outputs */ void class_test_run(){ int size = 1024; my_initialize_heap(size); printf("Test 1:\n"); int i = 0; int test; int* test1a = my_alloc(sizeof(int)); printf("Allocated address 1: %ld \n", test1a); my_free(test1a); printf("Allocation 1 freed: \n"); int* test1b = my_alloc(sizeof(int)); printf("Allocated address 2: %ld \n\n", test1b); printf("Test 2:\n"); int* test2a = my_alloc(sizeof(int)); printf("Allocated address 1: %ld \n", test2a); int* test2b = my_alloc(sizeof(int)); printf("Allocated address 2: %ld \n", test2b); printf("Address 2 - 1 = %ld - %ld = %ld \n",test2b, test2a, (test2b - test2a)*sizeof(int)); printf("Size of int = %i, Size of overhead = %i, sum = %i\n\n", msize, (msize + psize), (msize + msize + psize)); printf("Test 3:\n"); printf("!!!Note, void* size is 8, so double * 2 used:\n"); int* test3a = my_alloc(sizeof(int)); printf("Allocated address 1: %ld \n", test3a); int* test3b = my_alloc(sizeof(int)); printf("Allocated address 2: %ld \n", test3b); int* test3c = my_alloc(sizeof(int)); printf("Allocated address 3: %ld \n", test3c); my_free(test3b); printf("Allocation 2 freed: \n"); double* test3d = my_alloc(2 * sizeof(double)); printf("Allocated address 4 (double double): %ld \n", test3d); int* test3e = my_alloc(sizeof(int)); printf("Allocated address 5 (int): %ld \n\n", test3e); printf("Test 4:\n"); char* test4a = my_alloc(sizeof(char)); printf("Allocated address 1: %ld \n", test4a); int* test4b = my_alloc(sizeof(int)); printf("Allocated address 2: %ld \n", test4b); printf("Address 2 - 1 = %ld - %ld = %ld \n",test4b, test4a, ((int*)test4b - (int*)test4a)*sizeof(int)); printf("Size of int = %i, Size of overhead = %i, sum = %i\n\n", msize, (msize + psize), (msize + msize + psize)); printf("Test 4:\n"); int** test5a = my_alloc(sizeof(int) * 100); printf("Allocated address 1: %ld \n", test5a); int* test5b = my_alloc(sizeof(int)); printf("Allocated address 2: %ld \n", test5b); printf("Address 2 - 1 = %ld - %ld = %ld \n",test5b, test5a, ((int*)test5b - (int*)test5a)*sizeof(int)); printf("Size of array = %i, Size of overhead = %i, sum = %i\n", sizeof(int) * 100, (msize + psize), (sizeof(int) * 100 + msize + psize)); my_free(test5a); printf("Allocation 1 freed: \n"); printf("Allocated address 2: %ld \n", test5b); free(buffer); }
int main() { /* //-----------------------// //-----TEST ALLOCATOR----// //-----------------------// my_initialize_heap(10000); //Test 1 //-----------------------// printf("\n***TEST 1***\n\n"); int *p, *r; p = my_alloc(sizeof(int)); printf("Address of int pointer: 0x%X in hex and %d in dec \n", p, p); my_free(p); r = my_alloc(sizeof(int)); printf("Address of int pointer: 0x%X in hex and %d in dec \n", r, r); //-----------------------// //Test 2 //-----------------------// printf("\n***TEST 2***\n\n"); int *q, *s; q = my_alloc(sizeof(int)); s = my_alloc(sizeof(int)); printf("Address of int pointer 1: 0x%X in hex and %d in dec\n", q, q); printf("Address of int pointer 2: 0x%X in hex and %d in dec\n", s, s); printf("Size of overhead + int: %d \n", SIZE_OF_OVERHEAD + sizeof(int)); //-----------------------// //Test 3 //-----------------------// printf("\n***TEST 3***\n\n"); int *x, *t, *v; x = my_alloc(sizeof(int)); t = my_alloc(sizeof(int)); v = my_alloc(sizeof(int)); printf("Address of int pointer 1: 0x%X in hex and %d in dec\n", x, x); printf("Address of int pointer 2: 0x%X in hex and %d in dec\n", t, t); printf("Address of int pointer 3: 0x%X in hex and %d in dec\n", v, v); my_free(t); double *y; y = my_alloc(sizeof(double)); printf("Address of double pointer: 0x%X in hex and %d in dec\n", y, y); int *w = my_alloc(sizeof(int)); printf("Address of int pointer: 0x%X in hex and %d in dec\n", w, w); //-----------------------// //Test 4 //-----------------------// printf("\n***TEST 4***\n\n"); char *c; int *z; c = my_alloc(sizeof(char)); z = my_alloc(sizeof(int)); printf("Address of char pointer: 0x%X in hex and %d in dec \n", c, c); printf("Address of int pointer: 0x%X in hex and %d in dec \n", z, z); //-----------------------// //Test 5 //-----------------------// printf("\n***TEST 5***\n\n"); int *myArray = my_alloc(100*sizeof(int)); int *f = my_alloc(sizeof(int)); *f = 13; printf("Address of array: 0x%X in hex and %d in dec \n", myArray, myArray); printf("Address of int: 0x%X in hex and %d in dec \n", f, f); printf("Value of int: %d in dec \n", *f); my_free(myArray); printf("Address of int: 0x%X in hex and %d in dec \n", f, f); printf("Value of int: %d in dec \n", *f); //-----------------------// */ //--------------------------------------// //---SOLVE STANDARD DEVIATION PROBLEM---// //--------------------------------------// int numberOfIntegers = 0; int currentInput = 0; double stdDeviation = 0; my_initialize_heap(10000); //First get user input //Get the size of the array printf("Please enter a positive integer: "); scanf_s("%d", &numberOfIntegers); //Allocate space for an array of entered integers int *stdDevArray = my_alloc(numberOfIntegers * sizeof(int)); //Read integers into the array for (int i = 0; i < numberOfIntegers; i++) { printf("Please enter an integer to put into the array: "); scanf_s("%d", &stdDevArray[i]); } // Calculate standard deviation of integers // Formula: // sqrt( 1/n * summation( xi - u )^2, 1 to n ), // where xi = integers entered and u = total arithmetic mean stdDeviation = calculateStdDeviation(stdDevArray, numberOfIntegers); //Print the standard deviation printf("The standard deviation is : %f ", stdDeviation); system("pause"); return 0; }