The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// @Name loadMusicFile
// @Brief Load the music database
// 'size' is the size of the database.
char** loadMusicFile(const char* fileName, int size){
FILE *myFile = fopen(fileName,"r");
// Allocate memory for each character-string pointer
char** database = malloc(sizeof(char*)*size);
unsigned int song=0;
for(song =0; song < size; song++){
// Allocate memory for each individual character string
database[song] = malloc(sizeof(char)*80);
// Copy over string
database[song] = fgets(database[song],sizeof(char)*80,myFile);
}
// Import to always remember to close any file we open.
fclose(myFile);
// Return the pointer to our database
return database;
}
// @Name printArrayOfCharStrings
// @Brief Prints an array of C-style strings
void printArrayOfCharStrings(char** array, unsigned int start, unsigned int end){
int i;
for(i = start; i < end; i++){
printf("[%d] %s",i,array[i]);
}
}
// @Name swapStrings
// @Brief Swaps two strings.
// The lower string is put first
void swapStrings(char** s1, char** s2){
if( strcmp(*s1,*s2)==0 ){
// Strings are identical, do nothing
return;
}else if( strcmp(*s1,*s2) < 0 ){
// Do nothing--we are already sorted
return;
}else{
char* temp = *s1;
*s1 = *s2;
*s2 = temp;
}
}
// @Name bruteForceSort
// @Brief A simple O(N*N) sorting algorithm.
void bruteForceSort(char** array, unsigned int start, unsigned int end){
int i,j;
for(i =start; i < end-1; i++){
for(j =start; j < end-1; j++){
// Note the swap here.
swapStrings(&array[j],&array[j+1]);
}
}
}
// @Name partition
// @Brief Helper funcion for quicksort
int partition(char** array, unsigned int low, unsigned int high){
// TODO:
char** pivot = &array[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++){
if (array[j] <= pivot){
i++;
swapStrings(&array[i], &array[j]);
}
}
swapStrings(&array[i + 1], &array[high]);
return (i + 1);
}
void quicksort(char** array, unsigned int low, unsigned int high){
// TODO:
if (low < high){
int pivot = partition(array, low, high);
quicksort(array, low, pivot - 1);
quicksort(array, low, pivot +1);
}
}
int main(){
// Load our unsorted music file
// We load two copies, as we will compare two sorting algorithms.
char** musicDatabase1 = loadMusicFile("./musicdatabase.txt",13609);
char** musicDatabase2 = loadMusicFile("./musicdatabase.txt",13609);
// Print out a portion of the music database.
printf("The first 10 entries of 13609 unsorted are...\n");
printArrayOfCharStrings(musicDatabase1,0,10);
printf("\n");
// ===========================================
// ===== Experiment 1 - Using Brute Force Sort ====
// Create a clock to measure the elapsed time
clock_t start1,end1;
start1 = clock();
// perform bruteForceSort after starting your timer
bruteForceSort(musicDatabase1,0,13609);
end1 = clock();
double experiment1 = ((double)(end1-start1)/CLOCKS_PER_SEC);
// ===========================================
// ===========================================
// ===== Experiment 2 - Using Quick Sort ====
//printArrayOfCharStrings(musicDatabase2,0,994);
// Create a clock to measure the elapsed time
clock_t start2,end2;
start2 = clock();
// perform quicksort after starting your timer
quicksort(musicDatabase2,0,13609);
end2 = clock();
double experiment2 = ((double)(end2-start2)/CLOCKS_PER_SEC);
// ===========================================
// check correctness
const int items = 10; // change this to up to 13609
printf("O(N*N) sort produces\n");
printArrayOfCharStrings(musicDatabase1,0,items);
printf("\n quick sort produces\n");
printArrayOfCharStrings(musicDatabase2,0,items);
// ============ Results ==============
printf("\nResults of sorting:\n");
printf("%f time taking for brute force\n", experiment1);
printf("%f time taking for quick sort\n", experiment2);
return 0;
}// at line 82 pointer typecasting required
// and you have to check file is open or not
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// @Name loadMusicFile
// @Brief Load the music database
// 'size' is the size of the database.
char** loadMusicFile(const char* fileName, int size){
FILE *myFile = fopen(fileName,"r");
if (myFile == NULL)
{
printf("File Not Found: %s ",fileName);
exit(1);
}
// Allocate memory for each character-string pointer
char** database = malloc(sizeof(char*)*size);
unsigned int song=0;
for(song =0; song < size; song++){
// Allocate memory for each individual character string
database[song] = malloc(sizeof(char)*80);
// Copy over string
database[song] =
fgets(database[song],sizeof(char)*80,myFile);
}
// Import to always remember to close any file we open.
fclose(myFile);
// Return the pointer to our database
return database;
}
// @Name printArrayOfCharStrings
// @Brief Prints an array of C-style strings
void printArrayOfCharStrings(char** array, unsigned int start,
unsigned int end){
int i;
for(i = start; i < end; i++){
printf("[%d] %s",i,array[i]);
}
}
// @Name swapStrings
// @Brief Swaps two strings.
// The lower string is put first
void swapStrings(char** s1, char** s2){
if( strcmp(*s1,*s2)==0 ){
// Strings are identical, do nothing
return;
}else if( strcmp(*s1,*s2) < 0 ){
// Do nothing--we are already sorted
return;
}else{
char* temp = *s1;
*s1 = *s2;
*s2 = temp;
}
}
// @Name bruteForceSort
// @Brief A simple O(N*N) sorting algorithm.
void bruteForceSort(char** array, unsigned int start, unsigned int
end){
int i,j;
for(i =start; i < end-1; i++){
for(j =start; j < end-1; j++){
// Note the swap here.
swapStrings(&array[j],&array[j+1]);
}
}
}
// @Name partition
// @Brief Helper funcion for quicksort
int partition(char** array, unsigned int low, unsigned int
high){
// TODO:
char** pivot = &array[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++){
if (array[j] <= (char*)pivot){
i++;
swapStrings(&array[i], &array[j]);
}
}
swapStrings(&array[i + 1], &array[high]);
return (i + 1);
}
void quicksort(char** array, unsigned int low, unsigned int
high){
// TODO:
if (low < high){
int pivot = partition(array, low, high);
quicksort(array, low, pivot - 1);
quicksort(array, low, pivot +1);
}
}
int main(){
// Load our unsorted music file
// We load two copies, as we will compare two sorting
algorithms.
char** musicDatabase1 =
loadMusicFile("./musicdatabase.txt",13609);
char** musicDatabase2 =
loadMusicFile("./musicdatabase.txt",13609);
// Print out a portion of the music database.
printf("The first 10 entries of 13609 unsorted are... ");
printArrayOfCharStrings(musicDatabase1,0,10);
printf(" ");
// ===========================================
// ===== Experiment 1 - Using Brute Force Sort ====
// Create a clock to measure the elapsed time
clock_t start1,end1;
start1 = clock();
// perform bruteForceSort after starting your timer
bruteForceSort(musicDatabase1,0,13609);
end1 = clock();
double experiment1 = ((double)(end1-start1)/CLOCKS_PER_SEC);
// ===========================================
// ===========================================
// ===== Experiment 2 - Using Quick Sort ====
//printArrayOfCharStrings(musicDatabase2,0,994);
// Create a clock to measure the elapsed time
clock_t start2,end2;
start2 = clock();
// perform quicksort after starting your timer
quicksort(musicDatabase2,0,13609);
end2 = clock();
double experiment2 = ((double)(end2-start2)/CLOCKS_PER_SEC);
// ===========================================
// check correctness
const int items = 10; // change this to up to 13609
printf("O(N*N) sort produces ");
printArrayOfCharStrings(musicDatabase1,0,items);
printf(" quick sort produces ");
printArrayOfCharStrings(musicDatabase2,0,items);
// ============ Results ==============
printf(" Results of sorting: ");
printf("%f time taking for brute force ", experiment1);
printf("%f time taking for quick sort ", experiment2);
return 0;
}


The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...
C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size; song++){ // Allocate memory for each individual character string database[song] =...
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
I am getting the Segmentation fault error on the Ubuntu machine
but not on macOS.
Any help would be appreciated.
/**** main.c ****/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#define WORD_LEN 6
#define TOP 10
char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r";
struct Word {
char word[30];
int freq;
};
int threadCount;
int fileDescriptor;
int fileSize;
off_t chunk;
struct Word* wordArray;
int arrIndex = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...
this is c code. please answer all questions on a piece of paper and
show work. i need to prepare as i have a midterm i will have to be
completing on paper
1) Bit Operators: This C program compiles and runs. What is its output? 1) #include <stdio.h> 2) void main (void) 3) unsigned char x =60; 4) 5) 6) 7) 8 ) 9) 10) 11) 12) 13) unsigned char a = x < 1; unsigned char b unsigned...
I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...
Hello, I want to check if my C++ code is correct and follows the
requeriments described thanks.
Requeriments:
Assignment Sorting
Benchmark each of the sorting methods listed below.
Insertion Sort
Bubble Sort
Selection Sort
Heap Sort.
Quick Sort.
Merge Sort.
Benchmark each of the above sorting methods for data sizes of
10000, 20000, 30000, 40000 and 50000. Display the results in a
table as shown below. The table should have rows and columns.
However, the rows and columns need not...
C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...
I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...
Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) { int i, j, k, c[100000]; i = low; k = low; j = mid + 1; while (i <= mid && j <= high) { if (a[i] < a[j]) { c[k] = a[i]; k++; i++; } else { ...
Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; long length = 1000; const long max_length = 100000; int list[max_length]; void read() { ifstream fin("random.dat", ios::binary); for (long i = 0; i < length; i++) { fin.read((char*)&list[i], sizeof(int)); } fin.close(); } void bubbleSort() { int temp; for(long i = 0; i < length; i++) { for(long j = 0; j< length-i-1; j++)...