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] = 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:
}
void quicksort(char** array, unsigned int low, unsigned int high){
// TODO:
}
int main(){
// Load our unsorted music file
// We load two copies, as we will compare two sorting algorithms.
char** musicDatabase1 = loadMusicFile("./musicdatabase.txt",13610);
char** musicDatabase2 = loadMusicFile("./musicdatabase.txt",13610);
// 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;
}
Code :-
// 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] = 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){
if (low < high) // place pivot in it's correct position if
more than 1 element
{
unsigned int pivot = partition(array, low, high); //
returns index of pivot placed in it's correct position
quicksort(array, low, pivot - 1); // quicksort
array left of the pivot
quicksort(array, pivot + 1, high); // quicksort array
right of the pivot
}
}
void quicksort(char** array, unsigned int low, unsigned int high){
char* pivot = array[low]; // pick pivot as the lowest index string
unsigned int j = low; // stores index till which we have probed
unsigned int i = j - 1; // stores index below which all strings are smaller than the pivot string
while(j <= high){ // probe strings till high index
if (strcmp(array[j], pivot) < 0){ // if new string is smaller than the pivot then swap with string bigger than pivot
i++;
swapStrings(&array[i], &array[j]);
}
j++;
}
swapStrings(&array[i + 1], &array[low]); // swap the pivot to it's correct position
return i + 1;
}
int main(){
// Load our unsorted music file
// We load two copies, as we will compare two sorting algorithms.
char** musicDatabase1 = loadMusicFile("./musicdatabase.txt",13610);
char** musicDatabase2 = loadMusicFile("./musicdatabase.txt",13610);
// 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;
}
Note :- The music data file was not provided so i am unable to provide the sample output.
C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include...
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;...
#include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...
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...
Implement merge sort and merge
#include <iostream>
using namespace std;
void * merge(int arr[], int start1, int end1, int start2, int
end2){
int * combined = new int[end2-start1 + 1];
}
void mergeSort(int arr[], int start, int end){
//base case: down to 1 item, do nothing
//recursive case:
//MergeSort(left)
//MergeSort(right)
//Merge(left, right)
int m = (end - start) / 2;
if(start==end){ }
else {
mergeSort(arr, start, m);
mergeSort(arr, m+1, end);
merge(arr, start, m, m+1, end);
}
}
void fill(int arr[],...
Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...
Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{ char Word[21]; int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() { struct myWord wordList[20]; char line[100]; printf("Enter an English Sentence:\n"); gets(line); int size = tokenizeLine(line, wordList); printf("\n"); printf("Unsorted word list.\n"); printList(wordList, size);...
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...
void insertion_sort(int array[], int length, int & count_comp, int & count_move); void merge_sort(int array[], int length, int & count_comp, int & count_move); void heap_sort(int array[], int length, int & count_comp, int & count_move); void quick_sort_sort(int array[], int length, int & count_comp, int & count_move); // YOUR // Implementation // should // go // here // or be put in separated .cpp files typedef void (*sort_algo_type)(int array[], int length, int& count_comp, int & count_move); void run_sort_algo(int array[], int length, sort_algo_type sorting,...
#include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each of the three threads to prevent other threads from accessing global_sum during their additions int global_sum = 0; typedef struct{ char* word; char* filename; }MyStruct; void *count(void*str) { MyStruct *struc; struc = (MyStruct*)str; const char *myfile = struc->filename; FILE *f; int count=0, j; char buf[50], read[100]; // myfile[strlen(myfile)-1]='\0'; if(!(f=fopen(myfile,"rt"))){ printf("Wrong file name"); } else printf("File opened successfully\n"); for(j=0; fgets(read, 10, f)!=NULL; j++){ if (strcmp(read[j],struc->word)==0)...
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 { ...