Shell sorting random float array not working [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I wrote a shell sorting algorithm which works perfectly on integer values, but gives me segmentation fault when trying to sort float numbers. Could you help me with this ? Thank you.
We have a homework at the university and my teacher wrote this program with insert sort and it worked. The homework for us was to rewrite it to shell sort (which I think I did correctly) and maybe expand it further more to sort strings and numbers from files.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
int int_cmp(const void *p1, const void *p2){
return *(int*)p1 - *(int*)p2;
}
int float_cmp(const void *p1, const void *p2){
if (*(float*)p1 == *(float*)p2){
return 0;
}
else if (*(float*)p1 < *(float*)p2){
return -1;
}
else {
return 1;
}
}
void shell_sort(void *v, int nr, int size, int(*p_cmp)(const void*, const void*)){
int i, j;
void *pv, *pi, *pj;
pv = malloc(size); //this is where I get segmentation fault
for (int gap = nr/2; gap > 0; gap/=2){
for (i = gap; i < nr; ++i){
for (j = i-gap; j >= 0; j-=gap){
pi = v;
pi = (char*)pi+j*size;
pj = (char*)pi+gap*size;
if (p_cmp(pi, pj) > 0){
memcpy(pv, pi, size);
memcpy(pi, pj, size);
memcpy(pj, pv, size);
}
else {
break;
}
}
}
free(pv);
}
}
int main(){
int a[20], n = 20;
float b[25], m = 25;
srand(time(NULL));
printf("Original integer array: ");
for (int i = 0; i < 20; ++i){
a[i] = rand() % 100;
printf("%i ", a[i]);
}
printf("Original float array: ");
for (int j = 0; j < 25; ++j){
b[j] = (float)rand()/(float)(RAND_MAX)*50;
printf("%f ", b[j]);
}
shell_sort(a, n, sizeof(int), int_cmp);
shell_sort(b, m, sizeof(float), float_cmp);
printf("\nInteger array after sort: ");
for (int i = 0; i < 20; ++i){
printf("%i ", a[i]);
}
printf("\nFloat array after sort: ");
for (int j = 0; j < 25; ++j){
printf("%f ", b[j]);
}
return 0;
}

free(pv); move to outside for-loop(after loop).

Related

How to use `malloc/calloc` inside a function? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to create a function fill_table to fill a table dynamically. The tail n of the table is declared in the main() function,
int n = 0;
float* *T;
void show_table(int n) {}
void fill_table(int n) {}
int main() {
printf(" Table dimension: ");
scanf("%d", &n);
fill_table(n);
show_table(n);
return 0;
}
I always get this error:
segmentation fault ./a.out
I try mes functions like that:
int n = 0;
float* *T=0;
void show_table(int n){
printf("Show Table: \n");
for (int i = 0; i < n; i++) {
printf("%f ", *(T + i));
}
}
void fill_table(int n) {
*T=(float*)calloc(n, sizeof(float));;
if (!T){
printf("Memoire not allowe\n");
exit(0);
} else {
for (int i = 0; i < n; i++) {
printf("\nT[%d]= ", i+1);
scanf("%f", (T + i));
}
}
}
After a lot of trial and error I found this solution:
must declare table as
float* T; //not float* *T;
function fill_table:
void fill_table(int n) {
T=(float*)calloc(n, sizeof(float));;
if (!T){
printf("Memoire not allowe\n");
exit(0);
} else {
for (int i = 0; i < n; i++) {
printf("\nT[%d]= ", i+1);
scanf("%f", (T + i));
}
}
}
function show_table:
void show_table(int n){
printf("Show Table: \n");
for (int i = 0; i < n; i++) {
printf("%f ", *(T + i));
}
}

C program to calculate Binomial Coefficient with factorial method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm pretty sure it's only a small mistake but I don't know why it is not working..?
I would really appreciate if you could help me out!
#include <stdio.h>
int fak(int n) {
int fak_n;
if (n <= 1) {
fak_n = 1;
}
for (int i = 2; i <= n; i++) {
fak_n = fak_n * i;
}
return fak_n;
}
int bin(int n, int k) {
return fak(n) / (fak(k) * fak(n - k));
}
int main() {
int n;
int k;
scanf("%d%d", &n, &k);
printf("%d\n", bin(n, k));
return 0;
}
fak_n is used without initialization when n is larger than 1. The initialization should be done unconditionally like this:
int fak(int n) {
int fak_n=1;
for(int i=2; i<=n; i++){
fak_n=fak_n*i;
}
return fak_n;
}
There are 2 problems in your code:
fak_n is not initialized in the fak() function. You should write:
int fak(int n) {
int fak_n = 1;
for (int i = 2; i <= n; i++) {
fak_n = fak_n * i;
}
return fak_n;
}
type int has a limited range, typically 31 bits, so it can only represent factorials up to fak(12), so computing bin(13,1) which is 13, will cause an arithmetic overflow and produce an incorrect value. You should use a larger type such as unsigned long long and use a different formula.
Here is a modified version:
#include <stdio.h>
unsigned long long fak(int n) {
unsigned long long fak_n = 1;
for (int i = 2; i <= n; i++) {
fak_n = fak_n * i;
}
return fak_n;
}
unsigned long long bin(int n, int k) {
// check definition domain
if (k < 0 || k > n || n < 0)
return 0;
// use factorials for small values
if (n <= 20)
return fak(n) / fak(k) / fak(n - k);
// use Pascal triangle for larger ones
return bin(n - 1, k - 1) + bin(n - 1, k);
}
int main() {
int n, k;
if (scanf("%d%d", &n, &k) == 2) {
printf("%lld\n", bin(n, k));
}
return 0;
}

How to change iterations to recursion [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've got a problem. I've tried to write program. This is command:
The user specifies a whole number n>0.
Program:
Allocates two arrays of numbers of type int size n+1
Using only these arrays and a small number of statically allocated variables, the program calculates recursively the n line of the Pascal triangle (all binomial symbols with an upper parameter equal to n)
Prints out the calculated values
Memory slowing down
Example
input: 5
output: 1 5 10 10 5 1
I wrote iteration, but I have no idea how change this for recursion.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,k;
int * array_1;
int * array_2;
scanf("%d",&n);
if(n=='0') printf("%d", 1);
if(n=='1') printf("%d %d", 1, 1);
array_1 = (int*)calloc(n+1,sizeof(int));
array_2 = (int*)calloc(n+1,sizeof(int));
array_1[0] = 1;
array_1[1] = 1;
k=1;
while(k!=n)
{
for(i=0; i<=k+1; i++)
{
if(i==0)
{
array_2[0] = 1;
}
else if(i==n)
{
array_2[i] = 1;
}
else
{
array_2[i] = array_1[i] + array_1[i-1];
}
}
for(i=0; i<=n; i++)
{
array_1[i] = array_2[i];
array_2[i] = 0;
}
k++;
}
for(i=0; i<=n; i++)
{
printf("%d ", array_1[i]);
}
free(array_1);
free(array_2);
return 0;
}
The recursive version could look something like the following, with the actual work being left to fill-in under the two /* ... */ comments. The missing code essentially exists in the iterative version as posted, it just needs to be retrofitted here.
void recurse(int k, int n, int *array_1, int *array_2)
{
/*
print previously calculated k-th row in array_1
*/
// nothing left to do
if (k == n + 1) return;
/*
calculate next (k+1)-th row in array_2
*/
// swap arrays and repeat
recurse(k + 1, n, array_2, array_1);
}
int main()
{
int n, *array_1, *array_2;
if(scanf("%d", &n) != 1) return 1; // input error
if (n < 0) return 1; // invalid input
array_1 = (int*)calloc(n + 1, sizeof(int));
array_2 = (int*)calloc(n + 1, sizeof(int));
array_1[0] = 1;
recurse(1, n, array_1, array_2);
free(array_1);
free(array_2);
return 0; // done
}
Thanks everyone for answer :). This is my code:
#include <stdio.h>
#include <stdlib.h>
void recurse (int k, int n, int *array_1, int *array_2)
{
int i;
if(k==n+1) return;
for(i=1; i<=k+1; i++) array_2[i] = array_1[i] + array_1[i-1];
recurse(k+1, n, array_2, array_1);
}
void output(int n, int *array_1, int *array_2)
{
int i;
if(n%2!=0)
for(i=0; i<=n; i++) printf("%d ", array_1[i]);
else
for(i=0; i<=n; i++) printf("%d ", array_2[i]);
}
int main()
{
int n;
int * array_1;
int * array_2;
scanf("%d",&n);
if(n=='0')
{
printf("%d", 1);
return 0;
}
else if(n=='1')
{
printf("%d %d", 1, 1);
return 0;
}
array_1 = (int*)calloc(n+1,sizeof(int));
array_2 = (int*)calloc(n+1,sizeof(int));
array_1[0] = array_1[1] = array_2[0] = 1;
recurse(1, n, array_1, array_2);
output(n, array_1, array_2);
free(array_1);
free(array_2);
return 0;
}

Radix Sorting Algorithm in c [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have been tried to write radix sort in c.when i run my code with the static array it works well. but when i am trying to take random inputs from file it gives me an "Segmentation fault" at run time.help Please just help to modify this code
here is my code:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
int getMax(int arr[], int n)
{
int mx = arr[0];
int i;
for (i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp,int base)
{
int output[n];
int i;
int count[base];
memset(count, 0, sizeof count);
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%base]++;
for (i = 1; i < base; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%base ] - 1] = arr[i];
count[ (arr[i]/exp)%base ]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void radixsort(int arr[], int n,int base)
{
int m = getMax(arr, n);
int exp;
for (exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp , base);
}
void print(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ",arr[i]);
}
int main(int argc,int argv[])
{
int base=atoi(argv[1]);
int num,i;
FILE *fp1=fopen("myFile1.txt","r");
int arr[50];
while(fscanf(fp1,"%d",&num)==1)
{
arr[i]=num;
i++;
}
int n = sizeof(arr)/sizeof(arr[0]);
radixsort(arr, n ,base);
print(arr, n);
fclose(fp1);
return 0;
}
You are assuming that the compiler has set the initial value of i to 0. However, this is not guaranteed. While many compilers reset variables to 0, many others just leave the contents of memory set at whatever it happened to be at compile time or at load time. You need to initialize the value before using it.
Additionally, you do not test to ensure that you are not overrunning the arr buffer. For example consider what would happen to arr[] if you happen to open a file that has 51 entries. You would attempt to add an entry to arr[50] which overruns the buffer.
You need to initialize i to 0 and make sure to break out if i becomes too great.
The calculation of n is always 50 because arr is 50 ints. You should use i as the count of how many entries have been read in.
int main(int argc,int argv[])
{
int base=atoi(argv[1]);
// int num,i; // This is the line that causes the error
int num;
int i = 0; // This needs to be initialized before use.
FILE *fp1=fopen("myFile1.txt","r");
int arr[50];
// You need to ensure that i does not overrrun the buffer.
while(fscanf(fp1,"%d",&num)==1 && (i < 49))
{
arr[i]=num;
i++;
}
// Since i was defined before the while, it should have the correct count
// This calculation of n is wrong if fewer than a full buffer is read
int n = sizeof(arr)/sizeof(arr[0]);
radixsort(arr, n, base);
print(arr, n);
fclose(fp1);
return 0;
}

QuickSort Algorithm in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to write a recursive program for a QuickSort algorithm. Here's my code so far:
void QuickSort(int *array, int first, int last){
int q;
if (first<last){
q = partition(array,first,last);
QuickSort(array,first,q-1);
QuickSort(array,q+1,last);
}
}
void partition(int *A, int p, int r){
int value = A[r];
i = p-1;
int tmp;
for (j = p; j<=r; j++){
if (A[j] <= value) {
i++;
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
return(i);
}
int main(){
int numArray[8] = {30,15,11,40,75,80,70,60};
int i;
printf("Before sorting: \n");
for (i = 0; i<8; i++) printf("numArray[%d] = %d\n", i, numArray[i]);
int first = 0;
int last = sizeof(numArray)/sizeof(numArray[0]);
QuickSort(numArray,first,last-1);
printf("After sorting: \n");
for (i = 0; i<8; i++) printf("numArray[%d] = %d\n", i, numArray[i]);
}
My problem is that when I run this, I get stuck in an infinite loop when I get to the first recursion call (just after partition). Also, what I've noticed is that the recursion call is ran on the array values of A[0] - A[5], even though it should be on A[0]-A[3]. I'm not expecting a complete answer, but maybe a hint on why the program is stuck in that infinite would be extremely helpful.
Here's what I did with your outline and it seems to be working and sorting fine.
I replaced the void with an int, and declared int's as necessary
#include<iostream>
using namespace std;
int partition(int *A, int p, int r){
int value = A[r];
int i = p-1;
int tmp;
for (int j = p; j<=r; j++){
if (A[j] <= value) {
i++;
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
return(i);
}
void QuickSort(int *array, int first, int last){
int q;
if (first<last){
q = partition(array,first,last);
QuickSort(array,first,q-1);
QuickSort(array,q+1,last);
}
}
int main(){
int numArray[8] = {30,15,11,40,75,80,70,60};
printf("Before sorting: \n");
for (int i = 0; i<8; i++) printf("numArray[%d] = %d\n", i, numArray[i]);
int first = 0;
int last = sizeof(numArray)/sizeof(numArray[0]);
QuickSort(numArray,first,last-1);
printf("After sorting: \n");
for (int i = 0; i<8; i++) printf("numArray[%d] = %d\n", i, numArray[i]);
}

Resources