Im getting all types of confused now. Originally I had it printing out fine and yes you're right when you said that anything higher than 25 elements caused a segmentation fault. After a few changes suggested by my roommate who is also in my class and had his working correctly, I was left with all those problems above and now more confused than ever. Idk where I went wrong, and honestly my code is just screwed up. algorism I did see your post and I couldn't use the command of gbd on my computer but I will look more into that later and see what I can do for it. Thank you for all the help.
Sorry, just trying to get you on a good basis. I would suggest to drop passing the array size as a pointer... it makes your code hard to read and understand. But, I think algorism had it right.. you have uninitialized variable e in your print function which would cause seg fault.
<Morpheus> What is the value of e here? Why even have e variable at all? Hmm? What is real? How do you define real? If its e and its uninitialized, what is its value? Hmm? </Morpheus>Code:printf("Descending Order: \n"); for(c = 0; c < *x; c++) { insertionSort(arr, q_array, x); printf("%d\n", *q_array[e]); }
Last edited by MacNilly; 11-28-2016 at 11:40 AM.
Well haha.. good luck to you then, I've done all I can here.
One last advice: comment out your function calls in main, one by one... get them working individually. Without sorting, first. And I think it illogical (Spock!) that your print function actually calls the sorting functions. That's... well, illogical. First sort, then print.
I notice that you don't call the famous "blah" function (which sets up the pointer arrays) before you call the selection sort (which uses the pointer arrays).
I'm of the feeling that the OP is not even sure what he's about here... waste of time. Maybe a bit of copy-paste from his "roommate" version of working code plus a little ad-hoc variable renaming.. rofl...
Actually all my own work except for a few suggestions and changes that happened to make my roommates code work but not mine. In the end I figured out my segmentation fault and was able to get everything printing out and working correctly. No need to be a dick about it since going over it and writing it out again, it was an easy fix. I might not have the same level experience as you but I'm trying my best. Appreciate the help.
My apologies, then. I'm curious what was the easy fix?
I must admit, BTW, that in my earlier days I "borrowed" some code from another student.. and didn't understand a lick. My strategy was to rename variables, but at that time, I lacked the ability to really obfuscate the code to make it different. Yes, it was cheating, but on the other hand, I was really trying to learn it.. it was just too complex, and fast paced for me... in the end, I persevered. So, I'm sorry to come off rude like that.. but it could have been true LOL.
Last edited by MacNilly; 11-28-2016 at 02:34 PM.
I did that last year in Java and learned absolutely nothing. Didn't want to do that this year, wanted to actually learn something. As for the easy fix I called my prototypes in both main and my printArray which was causing a problem. There was a little more fixing to do but I ended up getting it to work. I am having another issue with another piece of code that I had to write. It runs and prints out fine, but it won't reach my print function when I call it in main and therefore wont print out my sorted array.
Code:#include <stdio.h> #include <time.h> #include <stdlib.h> float **jagged(); void populate(float **jaggedArray); void quickSort(float jaggedArray[], int first, int last); void insertionSort(int i, float *jaggedArray); void printArray(float **jaggedArray); int main() { float **jaggedArray = jagged(); populate(jaggedArray); float temp[5]; float temp1[5]; float temp2[5]; float temp3[5]; float temp4[5]; float temp5[5]; float temp6[5]; temp[0] = jaggedArray[0][0]; temp[1] = jaggedArray[1][0]; temp[2] = jaggedArray[2][0]; temp[3] = jaggedArray[3][0]; temp[4] = jaggedArray[4][0]; quickSort(temp, 0,4); temp1[0] = jaggedArray[0][1]; temp1[1] = jaggedArray[1][1]; temp1[2] = jaggedArray[2][1]; temp1[3] = jaggedArray[3][1]; temp1[4] = jaggedArray[4][1]; insertionSort(4, temp1); temp2[0] = jaggedArray[0][2]; temp2[1] = jaggedArray[1][2]; temp2[2] = jaggedArray[3][2]; temp2[3] = jaggedArray[4][2]; quickSort(temp2, 0, 3); temp3[0] = jaggedArray[0][3]; temp3[1] = jaggedArray[1][3]; temp3[2] = jaggedArray[3][3]; temp3[3] = jaggedArray[4][3]; insertionSort(3, temp3); temp4[0] = jaggedArray[0][4]; temp4[1] = jaggedArray[1][4]; temp4[2] = jaggedArray[3][4]; temp4[3] = jaggedArray[4][4]; quickSort(temp4, 0, 3); temp5[0] = jaggedArray[0][5]; temp5[1] = jaggedArray[3][5]; temp5[2] = jaggedArray[4][5]; insertionSort(2, temp5); temp6[0] = jaggedArray[0][6]; temp6[1] = jaggedArray[3][6]; quickSort(temp6, 0 , 2); jaggedArray[0][0] = temp[0]; jaggedArray[1][0] = temp[1]; jaggedArray[2][0] = temp[2]; jaggedArray[3][0] = temp[3]; jaggedArray[4][0] = temp[4]; jaggedArray[0][1] = temp1[0]; jaggedArray[1][1] = temp1[1]; jaggedArray[2][1] = temp1[2]; jaggedArray[3][1] = temp1[3]; jaggedArray[4][1] = temp1[4]; jaggedArray[0][2] = temp2[0]; jaggedArray[1][2] = temp2[1]; jaggedArray[3][2] = temp2[2]; jaggedArray[4][2] = temp2[3]; jaggedArray[0][3] = temp3[0]; jaggedArray[1][3] = temp3[1]; jaggedArray[3][3] = temp3[2]; jaggedArray[4][3] = temp3[3]; jaggedArray[0][4] = temp4[0]; jaggedArray[1][4] = temp4[1]; jaggedArray[3][4] = temp4[2]; jaggedArray[4][4] = temp4[3]; jaggedArray[0][5] = temp5[1]; jaggedArray[3][5] = temp5[2]; jaggedArray[4][5] = temp5[3]; jaggedArray[0][6] = temp6[0]; jaggedArray[3][6] = temp6[1]; printArray(jaggedArray); return 0; } float **jagged() { float **jaggedArray; jaggedArray = (float **)calloc(5 + 1, sizeof(float*)); jaggedArray[0] = (float*)calloc(10, sizeof(float)); jaggedArray[1] = (float*)calloc(5, sizeof(float)); jaggedArray[2] = (float*)calloc(2, sizeof(float)); jaggedArray[3] = (float*)calloc(7, sizeof(float)); jaggedArray[4] = (float*)calloc(6, sizeof(float)); jaggedArray[5] = NULL; return jaggedArray; } //populate jagged array void populate(float **jaggedArray) { int i; int rando = 0; // printf("--- Integers in the first row --- \n"); for(i = 0; i < 10; i++) { rando = (0) + 100 * ((float) rand()) / RAND_MAX; jaggedArray[0][i] = rando; printf("%.2f ", jaggedArray[0][i]); } printf("\n"); //printf("--- Integers in the second row --- \n"); for(i = 0; i < 5; i++) { rando = (0) + 100 * ((float) rand()) / RAND_MAX; jaggedArray[1][i] = rando; printf("%.2f ", jaggedArray[1][i]); } printf("\n"); //printf("--- Integers in the third row --- \n"); for(i = 0; i < 2; i++) { rando = (0) + 100 * ((float) rand()) / RAND_MAX; jaggedArray[2][i] = rando; printf("%.2f ", jaggedArray[2][i]); } printf("\n"); //printf("--- Integers in the fourth row --- \n"); for(i = 0; i < 7; i++) { rando = (0) + 100 * ((float) rand()) / RAND_MAX; jaggedArray[3][i] = rando; printf("%.2f ", jaggedArray[3][i]); } printf("\n"); //printf("--- Integers in the last row --- \n"); for(i = 0; i < 6; i++) { rando = (0) + 100 * ((float) rand()) / RAND_MAX; jaggedArray[4][i] = rando; printf("%.2f ", jaggedArray[4][i]); } printf("\n"); } void quickSort(float jaggedArray[], int first, int last) { int i,j,temp,swap = 0; if(first < last) { swap = first; i = first; j = last; } while(i < j) { while(jaggedArray[i] <= jaggedArray[swap] && i <= last) { i++; } while(jaggedArray[j] > jaggedArray[swap] && j >= first) { j--; } if(i < j) { temp = jaggedArray[i]; jaggedArray[i] = jaggedArray[j]; jaggedArray[j] = temp; } temp = jaggedArray[j]; jaggedArray[j] = jaggedArray[swap]; jaggedArray[swap] = temp; quickSort(jaggedArray, first, j - 1); quickSort(jaggedArray, j + 1, last); } } void insertionSort(int i, float *jaggedArray) { int j; float temp; if(i > 0) { insertionSort(i - 1, jaggedArray); temp = jaggedArray[i]; j = i - 1; while(jaggedArray[j] > temp && j >= 0) { jaggedArray[j + 1] = jaggedArray[j]; j--; } jaggedArray[j + 1] = temp; } } //Print jagged array row by row copy populate printf statement void printArray(float **jaggedArray) { int i; printf("Sorted array: \n"); for(i = 0; i < 10; i++) { printf("%.2f ", jaggedArray[0][i]); } printf("\n"); for(i = 0; i < 5; i++) { printf("%.2f ", jaggedArray[1][i]); } printf("\n"); for(i = 0; i < 2; i++) { printf("%.2f ", jaggedArray[2][i]); } printf("\n"); for(i = 0; i < 7; i++) { printf("%.2f ", jaggedArray[3][i]); } printf("\n"); for(i = 0; i < 6; i++) { printf("%.2f ", jaggedArray[4][i]); } }
Your dimensions don't seem to match up.
In "jagged" you create an array with 5 dimensions of sizes 10, 5, 2, 7, 6.
But in main you are treating it as having 7 dimensions of sizes 5, 5, 4, 4, 4, 3, 2.
It would have been better to have coded it without the hard-coded fixed sizes. And some loops wouldn't hurt. Example:
You should also have a function to freee the array's memory.Code:int ndims = 7; int dims[7] = { 5, 5, 4, 4, 4, 3, 2 }; float **arr = make_jagged(ndims, dims); for (int d = 0; d < ndims; d++) { for (int i = 0; i < dims[d]; i++) { arr[d][i] = 0.0; } }
per the homework assignment I was to create a jagged array in C. Taking in 5 rows, first row containing 10 random float integers, second row containing 5 random float integers, third row 2 random integers etc etc. Then, write a function that will sort the first numbers of every row (utilize recursive quicksort . Then, write afunction to sort the second numbers of every row (utilize the recursive insertion sort). Then, utilize the quick sort function to sort the third numbers in every row. Then, againthe insertion sort for the fourth numbers. Keep going like this till you reach the 10th number of therows. Finally, write a function to print the sorted jagged array row by row.
You're missing an srand(time(NULL)) call early in main. Without it you will always get the same sequence of random numbers from rand().
And it's probably not sorting correctly since your quicksort has bugs. You need a testing routine to check if the array has been properly sorted.
Bugs in your quicksort:
* if first is not less than last, i will be used uninitialized in the while condition.
* the swapping of the pivot and the recursive calls should be AFTER the while loop (not in it).
(The normal name for your 'swap' variable is 'pivot'.)