I am attempting to get this queue-based radix sort to work, but I can't seem to figure out what's wrong with it. It uses a text file as the input medium and throws tons of errors when I try to compile it and run it with the text file.
Any advice would be helpful at this point.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
#define SHOWPASS
//Compiled Using GNU GCC Compiler
void radixsort(int *a[], int n)
{
int i, b[MAX], m = *a[0], exp = 1;
for (i = 0; i < n; i++)
{
if (*a[i] > m)
m = a[i];
}
while (m / exp > 0)
{
int queue[10] =
{ 0 };
for (i = 0; i < n; i++)
queue[*a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
queue[i] += queue[i - 1];
for (i = n - 1; i >= 0; i--)
b[--queue[*a[i] / exp % 10]] = *a[i];
for (i = 0; i < n; i++)
*a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\nPASS : ");
radixsort(a, n);
#endif
}
}
int main( int argc, char *argv[] )
{
if ( argc != 3 )
{
printf("Need two input parameters in the following order: \n 1. Input file path \n 2. Number of elements in file\n");
return 0;
}
int num_elements = atoi(argv[2]);
int *input_arr = (int*) calloc (num_elements, sizeof(int));
int i;
FILE *fin; //File pointer to read input file
fin = fopen(argv[1], "r"); //Initialize file pointer
for(i=0; i<num_elements; i++)
{
fscanf(fin, "%d", &(input_arr[0]));
}
radixsort(input_arr[0], i);
printf ( "\nArray before sorting: \n") ;
for ( i = 0 ; i < num_elements ; i++ )
printf ( "%d\t", input_arr[0] ) ;
printf ( "\n\n");
return 0;enter code here
}
There are plenty of errors in your code. Firstly, the way you are taking input is incorrect.
fscanf(fin, "%d", &(input_arr[0]));
While taking input, the array input_arr is filled with a single input value at input_arr[0]. The rest of the input is over-written at input_arr[0].
Replace it with, fscanf(fin, "%d", &(input_arr[i]));.
Even you are displaying output in incorrect way.After sorting, the same output will be displayed num_elements times because of the following incorrect statement:
printf ( "%d\t", input_arr[0] ) ;
Again replace the above statement with printf ( "%d\t", input_arr[i] ).
As an impact of following incorrect statement,
fscanf(fin, "%d", &(input_arr[i])); ,
your program experiences a Segmentation fault, since in function radixsort, you are iterating from 0 to n-1(Number of elements).
for (i = 0; i < n; i++)
{
if (*a[i] > m)
m = a[i];
}
As only a[0] is filled with input value and rest of the array(from a[1] to a[n-1]) contains a garbage value, you will get a runtime error while executing your code.
There are lot of other bugs too, which i had fixed. This is the perfect running code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
// #define SHOWPASS
// Compiled Using GNU GCC Compiler
void radixsort(int a[], int n)
{
int i, b[MAX], m = a[0], exp = 1;
for (i = 0; i < n; i++)
{
if (a[i] > m)
m = a[i];
}
while (m / exp > 0)
{
int queue[10] = { 0 };
for (i = 0; i < n; i++)
queue[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
queue[i] += queue[i - 1];
for (i = n - 1; i >= 0; i--)
b[--queue[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\nPASS : ");
radixsort(a, n);
#endif
}
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf
("Need two input parameters in the following order: \n 1. Input file path \n 2. Number of elements in file\n");
return 0;
}
int num_elements = atoi(argv[2]);
int *input_arr = (int *)calloc(num_elements, sizeof(int));
int i;
FILE *fin; // File pointer to read input file
fin = fopen(argv[1], "r"); // Initialize file pointer
for (i = 0; i < num_elements; i++)
{
fscanf(fin, "%d", &(input_arr[i]));
}
radixsort(input_arr, i);
printf("\nArray before sorting: \n");
for (i = 0; i < num_elements; i++)
printf("%d\t", input_arr[i]);
printf("\n\n");
return 0;
}
Related
How do I get to write to 2D pointers where I have pnumber[2%4][2%4] and how can I get pnumber with more than 3 ciphers to be displayed?
I'm making a program to write pascals triangle in C.
When the pointer pnumbers[i][j] have both i and j = 2 mod 4, except for when i and j = 2, then my program won't write to the address and give the error message:
pascals triangle: malloc.c:2406: sysmalloc: Assertion '{old_top == initial_top (av) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =7;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
/*
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
*/
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
When I avoid writing to these addresses and just print them out I get some seemingly random integer at these memory addresses.
Also when avoid these addresses and just print out so many rows that I get some spots with a higher integer with more than 3 siphers, it seems to overflow - and I don't see the logic behind it.
The result of running the second code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =20;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
/*
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
*/
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
But row number 13 is still quite messed up.
Code is experiencing int overflow and thus undefined behavior (UB).
With 32-bit int and int factorial(int p), p > 12 oveflows the int range.
Code could use a wider integer type (long long works up to p==20), but improvements can be made at NchooseM() to avoid overflow for higher values.
Something like the below. Works up to int n = 30;
int NchooseM(int n, int m) {
// return factorial(n)/(factorial(n-m)*factorial(m));
int nm = 1;
int den = 1;
for (int i = m+1; i <= n; i++) {
assert(INT_MAX/i >= nm);
nm *= i;
assert(nm % den == 0);
nm /= den++;
}
return nm;
}
Tried unsigned long long and works up to int n = 62;
Edit: Another bug:
I "fixed" by initializing all to 1, yet I suspect something remains amiss in /* Calculating the value of pnumbers[k][l] */ for (i = 0; i < n; i++) { code.
pnumbers[i] = malloc((i + 1) * sizeof pnumbers[i][0]);
for (int j = 0; j < i + 1; j++) {
pnumbers[i][j] = 1;
}
Aside: rather than pnumbers[i] = (int *) malloc((i+1) * sizeof(int));, consider below with no unneeded cast nor trying to match the right type.
pnumbers[i] = malloc(sizeof pnumbers[i][0] * (i+1));
I'm wrote a program that prints a pyramid of binomial coefficients. The problem is that it prints them correctly when I compile and run the code using Xcode, but it prints wrong numbers when I do so in the terminal. I was hoping someone could tell me why this is happening. I'm running the latest version of Xcode on macOS Mojave.
UPDATE: After relaunching Xcode, I'm not getting the right numbers for the coefficients on Xcode either.
This is the code:
#include <stdio.h>
#include <stdlib.h>
/* Function prototype */
int C(int a, int b);
int main(){
int count =0;
int n = 0; /* The number of rows in the array (minus 1) */
/* Prompt user to input and validate */
printf("Please enter a positive integer:\n"
"n = ");
do
scanf("%d", &n);
while ( n <= 0 );
/* Allocate enough memory for the matrix */
int **matrix = (int**) malloc( (n+1) * sizeof(int*) );
/* Allocate enough memory for each array */
for (int i = 0; i < n+1; i++) {
matrix[i] = (int*) malloc( (i+1) * sizeof(int) );
}
/* Populate each array */
for (int i = 0; i < n+1; ++i) {
for (int j = 0; j < i+1; ++j) {
matrix[i][j] = count;
count++;
}
}
/* Print pyramid */
for (int i = 0; i < n+1; ++i) {
for (int j = 0; j < i+1; ++j) {
if ( (i==0) && (j==0) ) {
for (int k = 0; k < n+1-i; ++k) {
printf("\t");
}
printf("%d\n", matrix[i][j]);
}
else
if (j == 0) {
for (int k = 0; k < n+1-i; ++k) {
printf("\t");
} printf("%d\t", matrix[i][j]);
} else
if ( (0 < j) && (j < i) ) {
printf("\t%d\t", matrix[i][j]);
}
else
if (j == i) {
printf("\t%d\n", matrix[i][j]);
}
}
}
/* Free allocated memory */
free(matrix);
return 0;
}
/************************ Function Definition **********************/
/*
* Function: C
* -----------
* Computes the binomial coefficient via the factorial formula, using
* for-loops for the factorials.
*
* f_a = the factorial of the first number
* f_b = the factorial of the other number
* f_a_b = the factorial of the difference (a-b)
*
* returns: the binomial C = (a,b)
*/
int C(int a, int b) {
unsigned int f_a = 1;
unsigned int f_b = 1;
unsigned int f_a_b = 1;
for ( int i = 1; i < a; ++i ) {
f_a *= i;
}
for ( int i = 1; i < b; ++i ) {
f_b *= i;
}
for ( int i = 1; i < (a-b); ++i ) {
f_a_b *= i;
}
unsigned int C = f_a / ( f_b * f_a_b );
return C;
}
The way you compute the factorials is wrong:
for ( int i = 1; i < a; ++i )
should be
for ( int i = 1; i <= a; ++i )
since you have to include the last element. Thats the way the factorial is defined.
Also this function is more suitable to calculate binomial coefficients since the factorial overflows at 13 (with 32 bit int).
int C(int a, int b)
{
int bc = 1;
if (b > a-b)
{
b = a-b;
}
for (int i = 0; i < b; i++)
{
bc *= (a - i);
bc /= (i + 1);
}
return bc;
}
Task is next:
After you generate 20 random numbers and read informations from file tombula.txt which contains names of users and their 9 numbers,print the first user (his/her name) who has 3 numbers from own array equal to generated numbers.
This is my part of code where i generate numbers and read from file but I don't know how to find that first person who has 3 numbers from array equal to generated numbers,if you know how to write that please help:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
typedef struct
{ char name[50];
int num[8];
}someone;
void generating(int[]);
int check(int[], int);
int main(void)
{
someone *pK = NULL;
int i, j, count = 0, tip;
FILE *F;
int generated[30];
int brojac[5];
srand(time(NULL));
generating(generated);
for (int j = 0; j < 20; j++)
printf("%d ", generated[j]);
printf("\n\n\n");
F = fopen("tombula.txt", "r");
if (F == NULL)
{
printf("error!");
exit(1);
}
i = 0;
while (feof(F) == 0)
{
pK = ((someone *)realloc(pK, (i + 1)*sizeof(someone)));
fscanf(F, "%s", pK[i].name);
for (j = 0; j < 8; j++)
fscanf(F, "%d", &pK[i].num[j]);
i++;
count++;
}
i--;
count--;
for (int z = 0; z < count; z++){
printf("%s ", pK[z].name);
for (int k = 0; k < 8; k++){
printf("%d ", pK[z].num[k]);
}
printf("\n");
}
return 0;
}
void generating(int numbers[])
{
int i, tmp;
for (i = 0; i < 20; ++i)
{
tmp = rand() % 50 + 1;
if (check(numbers, tmp))
{
--i;
continue;
}
numbers[i] = tmp;
}
}
int check(int b[], int a){
int i;
int n = 8;
for (i = 0; i < 20; ++i)
{
if (b[i] == a)
return 1;
}
return 0;
}
1.You opened file in r mode.
2.In a loop (loop must work until EOF) read data stored in file using fscanf() .Store name into char array and number into integer array (As you defined in struct).
3.Inside loop check if the 3 numbers matches with the numbers generated and if they do print the persons name and use break to come out of loop.
4.After this close the file.
This question already has answers here:
Understanding memory allocation, test program crashing
(4 answers)
Closed 7 years ago.
i have this code and it crash in the middle of processing. System gives message "filename.exe stopped working. What is wrong here?
I declare array as global to be able to have so big number of elements, but still it doesn't work.
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000000
#define SHOWPASS
void print(int *a, int n) {
int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}
void radix_sort(int *a, int n) {
int i, b[MAX], m = 0, exp = 1;
for (i = 0; i < n; i++) {
if (a[i] > m)
m = a[i];
}
while (m / exp > 0) {
int box[10] = { 0 };
for (i = 0; i < n; i++)
box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\n\nPASS : ");
print(a, n);
#endif
}
}
int arr[MAX];
int main() {
//int arr[MAX];
int i, num;
printf("\nEnter total elements (num < %d) : ", MAX);
scanf("%d", &num);
printf("\ncreate array : ");
for (i = 0; i < num; i++)
arr[i]=rand()%10;
printf("\nARRAY : ");
print(&arr[0], num);
radix_sort(&arr[0], num);
printf("\n\nSORTED : ");
print(&arr[0], num);
return 0;
}
Here is another code i tried, this time i used malloc. But still it crashes before starting sort. everything is fine if number of elements is <100000.
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000000
#define SHOWPASS
void print(int *a, int n) {
int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}
void radix_sort(int *a, int n) {
int i, b[MAX], m = 0, exp = 1;
for (i = 0; i < n; i++) {
if (a[i] > m)
m = a[i];
}
while (m / exp > 0) {
int box[10] = { 0 };
for (i = 0; i < n; i++)
box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\n\nPASS : ");
print(a, n);
#endif
}
}
int i, num;
int main() {
int* arr = (int*)malloc(MAX * sizeof(int));
int i;
printf("\ncreate array : ");
for (i = 0; i < MAX; i++)
arr[i]=rand()%10;
printf("\nARRAY : ");
print(&arr[0], MAX);
radix_sort(&arr[0], MAX);
printf("\n\nSORTED : ");
print(&arr[0], MAX);
free(arr);
return 0;
}
The bug is here:
int i, b[MAX], m = 0, exp = 1;
Allocating a huge (1 million int) array on the stack is not possible on some if not most systems.
You should malloc the temporary array and allocate only the size needed for the sort, namely n * sizeof(int).
Another problem is this: your radix_sort cannot handle negative numbers.
Less important but worth mentioning: your implementation is not stable. Not a problem for simple int arrays, but potentially incorrect for larger structures.
Furthermore, your code is inefficient: you use division and modulo 10. It would be much faster to use shifting and masking.
Here is a more efficient implementation for large arrays:
#include <limits.h>
#include <string.h>
#include <stdlib.h>
static void radix_sort(int *a, size_t size) {
size_t counts[sizeof(*a)][256] = {{ 0 }}, *cp;
size_t n, i, sum;
unsigned int *tmp, *src, *dst, *aa;
dst = tmp = malloc(size * sizeof(*a));
src = (unsigned int *)a;
for (i = 0; i < size; i++) {
unsigned int v = src[i] + (unsigned int)INT_MIN;
for (n = 0; n < sizeof(*a) * 8; n += 8)
counts[n >> 3][(v >> n) & 255]++;
}
for (n = 0; n < sizeof(*a) * 8; n += 8) {
cp = &counts[n >> 3][0];
if (cp[0] == size) continue;
for (i = sum = 0; i < 256; i++)
cp[i] = (sum += cp[i]) - cp[i];
for (i = 0; i < size; i++)
dst[cp[((src[i] + (unsigned int)INT_MIN) >> n) & 255]++] = src[i];
aa = src;
src = dst;
dst = aa;
}
if (src == tmp) {
memcpy(a, src, size * sizeof(*a));
}
free(tmp);
}
I'm trying to implement the sieve algorithm where it'll ask for the size of a list of consecutive numbers and print out the prime numbers in that list, but I'm getting a seg fault: 11 error.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 1000000 /*size of integers array*/
int main(void){
char n[LIMIT];
//Reads the size of integer array
printf("Size of array:\n");
if((fgets(n, LIMIT, stdin)) == NULL) {
fprintf(stderr, "Could not read from stdin\n");
exit(1);
}
unsigned long long int i,j;
int *primes;
int z = 1;
primes = malloc(sizeof(n));
for (i = 2; i < sizeof(n); i++){
primes[i] = 1; //setting every number in list to 1
}
for (i = 2; i < sizeof(n); i++){
if (primes[i]){
for (j = i; (i*j) < sizeof(n); j++){
primes[i * j] = 0; //remove multiples
}
}
}
for (i = 2; i < sizeof(n); i++){
if (primes[i]){
printf("%dth prime = %llu\n",z++,i);
}
}
free(primes);
return 0;
}
char n[LIMIT];
With the value of LIMIT being 1000000, that's a really big array (one million bytes). It would cause stack overflow. You need to dynamically allocate memory for it:
char *n = malloc(LIMIT);
Code is using n in a curious fashion. Suggest simply scanning the the integer n and using n to end the loops rather than sizeof(n).
#include <stdio.h>
#include <stdlib.h>
int main(void){
unsigned long n;
//Reads the size of integer array
buffer[30];
printf("Size of array:\n");
if((fgets(buffer, sizeof buffer, stdin)) == NULL) {
fprintf(stderr, "Could not read from stdin\n");
exit(1);
}
if (sscanf(buffer,"%lu", %n) != 1) {
fprintf(stderr, "Unable to covert to a number\n");
exit(1);
}
unsigned long i,j;
int *primes;
// int z = 1;
primes = malloc(n * sizeof *primes);
for (i = 2; i < n; i++){
primes[i] = 1; //setting every number in list to 1
}
for (i = 2; i < n; i++){
if (primes[i]) {
for (j = i; (i*j) < n; j++){
primes[i * j] = 0; //remove multiples
}
}
}
for (i = 2; i < n; i++){
if (primes[i]){
printf("%dth prime = %llu\n",z++,i);
}
}
free(primes);
return 0;
}