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;
}
Related
#include <stdio.h>
#include <math.h>
#define MAX_SIZE 10000
int main(void)
{
int a[MAX_SIZE];
int N;
int L; /* the current size of the list */
/* read in the upper limit. Keep reading until
a valid number between 3 and the maximum that
can be handled by the array is entered */
double b[10000];
int j, i;
L = 0;
printf("Enter the upper limit:\n");
do {
scanf("%d", &N);
} while (N<3 || N>MAX_SIZE+2);
int prime;
for (j = 1; j < N; j++)
{
prime = 1;
for (i = 2; i < j; i++)
{
if (j % i == 0)
{
prime = 0;
break;
}
}
if (prime)
{
a[i] = j;
L++;
}
}
/* write out the result - DO NOT CHANGE THIS */
for(i=0;i<L;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
Program needs to take an integer, calculate primes below that integer, print that list of primes.
I think my problem is related to the loops.
The program is calculating the primes but listing 0 if the number previously there isnt prime eg a[4] is now printing as 0
Any help is appreciated.
thanks.
Is this what you were trying to implement?
#include <stdio.h>
#define MAX_SIZE 100
int main(void)
{
int primes[MAX_SIZE];
int primes_found = 0;
int limit = 0;
while (limit < 3)
{
printf("Enter the upper limit:\n");
scanf("%d", &limit);
}
for (int candidate = 2; candidate <= limit && primes_found < MAX_SIZE; candidate++)
{
int divisor = 2;
int is_prime = 1;
while(is_prime && divisor < candidate)
is_prime = candidate % divisor++ != 0;
if (is_prime)
primes[primes_found++] = candidate;
}
for (int i = 0 ; i < primes_found ; i++)
printf("%d ", primes[i]);
printf("\n");
return 0;
}
What the program does:
Reads from a file a matrix (2d Array) with nrRows rows and nrColomns colomns.
All elements of the matrix are int numbers between [0,100).
The program has to rearrange all of the elements inside the matrix such that each element is equal to the index of the row it is in.
ex. 5 will be on line(row) 5, 58 on row 58.
The result is written in a file.
I need to use only 1 matrix and all memory is allocated dynamically.
If I have to add or erase elements, I also readjust the memory of the matrix.
Also, I need to keep the shape of a matrix.
ex. 3 rows with 2 colomns. NOT 3 rows where row 1 has 1 colomn, row 2 has 3 colomns, etc..
I think reallocation doesn't work in my c program. The program itself works fine.
Help?
#include<stdio.h>
#include<malloc.h>
unsigned short maxim(unsigned short A[100])
{
unsigned short max = 0;
for (unsigned short i = 0; i<100; ++i)
if (A[i] > max)
max = A[i];
return max;
}
void main()
{
FILE *pFile = fopen("test.txt", "r");
unsigned short nrRows, nrColomns;
/* If empty exit */
if (pFile == NULL)
return;
fscanf(pFile, "%d", &nrRows);
fscanf(pFile, "%d", &nrColomns);
/* Memory Allocation */
int** V = (int**)malloc(nrRows * sizeof(int*)); /* Number of lines */
for (unsigned short i = 0; i < nrRows; ++i)
V[i] = (int*)malloc(nrColomns * sizeof(int)); /* Number of colomns */
/* Read the elements */
for (unsigned short i = 0; i < nrRows; ++i)
for (unsigned short j = 0; j < nrColomns; ++j)
fscanf(pFile, "%d", &V[i][j]);
/* Find max + array */
unsigned short A[100] = { '\0' }; unsigned short max = 0;
for (unsigned short i = 0; i < nrRows; ++i)
for (unsigned short j = 0; j < nrColomns; ++j)
{
/* How many times each value between [0 and 100) is found inside the matrix */
A[V[i][j]]++;
/* Find the biggest element */
if (V[i][j] > max)
max = V[i][j];
}
/* Memory Reallocation */
unsigned short maxA = maxim(A); unsigned short ok = 0;
if (maxA > nrColomns){
nrColomns = maxA;
ok++;
}
if (max + 1 > nrRows){
nrRows = max + 1;
ok++;
}
//if (ok != 0)
//{
*V = realloc(*V, nrRows * sizeof(int*));
for (unsigned short i = 0; i < nrRows; i++)
V[i] = (int*)realloc(V, nrColomns * sizeof(int));
//}
/* Rearrange Values */
unsigned short bool = 1;
while (bool != 0)
{
bool = 0;
for (unsigned short i = 0; i < nrRows; ++i)
{
for (unsigned short j = 0; j < nrColomns; ++j)
{
if (V[i][j] != i)
{
/* Swap elements */
unsigned short k = 0;
while (k < nrColomns)
{
if (V[V[i][j]][k] != V[i][j])
{
bool = 1;
/* Do the swapping */
int swap = V[V[i][j]][k];
V[V[i][j]][k] = V[i][j];
V[i][j] = swap;
break;
}
else k++;
}
}
}
}
}
/* Extra Reallocation */
if (maxA < nrColomns)
{
nrColomns = maxA;
for (unsigned short i = 0; i < nrRows; ++i)
V[i] = (int*)realloc(V, nrColomns * sizeof(int));
}
/* Print Result into file */
pFile = fopen("out.txt", "w");
fprintf(pFile, "%d %d \n", nrRows, nrColomns);
for (unsigned short i = 0; i < nrRows; ++i)
{
for (unsigned short j = 0; j < nrColomns; ++j)
fprintf(pFile, "%d ", V[i][j]);
fprintf(pFile, "\n");
}
fclose(pFile);
_getch();
/* Memory Deallocation */
for (unsigned short i = 0; i < nrRows; ++i)
free(V[i]);
free(V);
}
It's just wierd... I've lost enough hours on this problem.
Example of test.txt
4 3 1 2 2 0 0 0 1 1 3 5 3 2
I always hated realloc. If you are ok to avoid it, here's what I suggest :
Some points about your code, regardless of the problem :
in C it is better to declare all the variables at the start of any function. If possible, never declare a variable in middle of the code.
You can declare variable inside for block though. However it doesn't work in all version of C. Depends on the compiler.
Unless you will deploy this code on some device with really limited memory, you don't necessary have to use unsigned short. Classic integers would do.
It is recommended to check if every malloc went well. And if not, to do the necessary actions to avoid crash.
Code :
#include <stdio.h>
#include <stdlib.h>
int min(int a, int b) {
return (a < b) ? a : b;
}
void updateDimensions(int*** currentArray, unsigned short currentRowsNb, unsgined short currentColumnsNb, unsigned short newRowsNb, unsigned short newColumnsNb) {
int i;
int j;
int error;
int** res;
if(*currentArray != NULL && (newRowsNb != currentRowsNb || newColumnsNb != currentColumnsNb)) {
error = 0;
/* Memory allocation */
res = (int**)malloc(newRowsNb* sizeof(int*));
if(res != NULL) {
for(i=0 ; i < newRowsNb; i++) {
res[i] = (int*)malloc(newColumnsNb* sizeof(int));
if(res[i] == NULL) {
error = 1;
fprintf(stderr, "allocation error"); // Optional
while(--i >= 0)
free(res[i]);
free(res);
}
}
} else {
fprintf(stderr, "Allocation error);
error = 1;
}
/* End of memory allocation */
if(!error) {
/* Copy of the array */
for(i=0 ; i < min(currentRowsNb, newRowsNb) ; i++)
for(j=0 ; j < min(currentColumnsNb, newColumnsNb) ; j++)
res[i][j] = (*currentArray)[i][j];
/* End of copy */
/* Free current array */
for(i=0 ; i < currentNrRows ; i++)
free((*currentArray)[i]);
free(*currentArray);
/* End of free */
*currentArray = res; // Assign new array to current one.
}
}
}
unsigned short maxim(unsigned short A[100]) {
unsigned short max = 0;
unsigned short i;
for(i = 0 ; i < 100 ; i++)
if(A[i] > max)
max = A[i];
return max;
}
void main() {
/* Variable declaration */
unsigned short i;
unsigned short j;
unsigned short k;
unsigned short ok;
unsigned short max;
unsigned short maxA;
unsigned short nrRows;
unsigned short my_bool;
unsigned short nrColomns;
unsigned short A[100];
int swap;
int newNrRows;
int newNrColumns;
int tempInt;
int **V;
FILE *pFile;
/* Variable initialization */
pFile = fopen("test.txt", "r");
if(pFile == NULL)
return;
fscanf(pFile, "%d", &nrRows);
fscanf(pFile, "%d", &nrColomns);
max = 0;
my_bool = 1;
for(i=0 ; i < 100 ; i++)
A[i] = '\0';
/* Memory Allocation */
V = (int**)malloc(nrRows * sizeof(int*)); /* Number of lines */
if(V == NULL) {
fprintf(stderr, "Allocation error"); // Writes in the error output (optional)
return;
}
for (i = 0; i < nrRows; ++i)
V[i] = (int*)malloc(nrColomns * sizeof(int)); /* Number of colomns */
/* If allocation error */
if(v[i] == NULL) {
fprintf(stderr, "Allocation error"); // Optional
/* The following is mandatory */
while(--i > 0) { // Free all cell already allocated
free(A[i]);
}
free(A);
return;
}
}
/* End of memory allocation */
/* Read the elements */
for (i = 0; i < nrRows; ++i)
for (j = 0; j < nrColomns; ++j)
fscanf(pFile, "%d", &V[i][j]); // That assume you only have digit (0 to 9) in your file.
/* If you have a delimiter between numbers (here i'll take ';' as example, use this */
fscanf(pFile, "%[^;]%*d", &v[i][j]);
/* If you have many delimiters (here ';', ' ' and '\n' as example) */
fscanf(pFile, "%[^;\n ]%*d", &v[i][j]);
/* End of reading
/* Find max + array */
for (i = 0; i < nrRows; ++i)
for (j = 0; j < nrColumns; ++j) {
/* How many times each value between [0 and 100) is found inside the matrix */
A[V[i][j]]++;
/* Find the biggest element */
if (V[i][j] > max)
max = V[i][j];
}
/* Memory Reallocation */
maxA = maxim(A);
if (maxA > nrColomns) {
newNrColumns = maxA;
ok++;
}
if (max + 1 > nrRows) {
newNrColumns = max + 1;
ok++;
}
if (ok != 0) {
updateDimensions(&V, nrRows, nrColumns, newNrRows, newNrColumns);
nrRows = newNrRows;
nrColumns = newNrColumns;
}
/* Rearrange Values */
while (my_bool != 0) {
my_bool = 0;
for (i = 0; i < nrRows; ++i) {
for (j = 0; j < nrColomns; ++j) {
if (V[i][j] != i) {
/* Swap elements */
k = 0;
while (k < nrColomns) {
if (V[V[i][j]][k] != V[i][j]) {
my_bool = 1;
/* Do the swapping */
swap = V[V[i][j]][k];
V[V[i][j]][k] = V[i][j];
V[i][j] = swap;
break;
} else {
k++;
}
}
}
}
}
}
/* Extra Reallocation */
if (maxA < nrColomns) {
newNrColumns = maxA;
updateDimension(&V, nrRows, nrColumns, newNrRows, newNrColumns);
}
/* Print Result into file */
fclose(pFile);
pFile = fopen("out.txt", "w");
if(pFile == NULL) {
fprintf(stderr, "Error while openning file");
} else {
fprintf(pFile, "%d %d \n", nrRows, nrColomns);
for (unsigned short i = 0; i < nrRows; ++i) {
for (unsigned short j = 0; j < nrColomns; ++j)
fprintf(pFile, "%d ", V[i][j]);
fprintf(pFile, "\n");
}
fclose(pFile);
}
_getch();
/* Memory Deallocation */
for (i = 0; i < nrRows; ++i)
free(V[i]);
free(V);
}
NB : I did not try to compile this, it might have errors (especially on things like : (*currentArray)[i][j] ). Also, sorry for bad English ^^'
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.
I am fairly new to C programming .
If the user enters a number lets say 123.
How can i print out 321?
I tried using bubble and selection sort but i find them very hard to understand at this initial stage!
I was hoping somebody could help me understand it by breaking it out !
Please help
Thank You
Here you go!
#include <stdio.h>
#include <string.h>
int main()
{
int n ;
int m ;
int i, j, jMax, max=0, res = 0;
int len = 0;
printf("Input number\n");
scanf("%d", &n);
m = n;
do // compute length of array that contains our input number
{
n /= 10;
len++;
}
while (n > 0);
n = m; // copy back input number
int * arr = malloc(len*sizeof(int)); // allocate memory for the array
len = 0; // initialize len for loop use
do // fill the array of input number
{
arr[len] = n%10;
n/=10;
len++;
}
while (n > 0);
//sort
int * sort = malloc(len*sizeof(int));
for(i = 0; i< len; i++)
{
for(j = 0; j<len; j++)
{
if(arr[j] > max)
{max = arr[j]; jMax = j;}
}
arr[jMax] = 0;
sort[i] = max;
max = 0;
}
// convert back from array to an int
j = 1;
for (i=0; i< len; i++)
{
res += sort[len-i-1]*j;
j *= 10;
}
printf(" %d", res);
return 0
}
You can take the user input as string, and print the string in reverse order, using a loop or recursion, but since you are new to c , I would say, go with chux's solution.
The below code does both reversing the entered number as well as arranging the entered number in the descending order.
You can try the below code:
int main()
{
int n,m,len,i,j,max=0,x;
printf("Enter the number\n");
scanf("%d",&n);
m = n;
len = 0;
printf("Number in the reverse order\n");
while(n>0)
{
n=n/10;
printf("%d",(n%10));
len++;
}
printf("\n");
printf("Numbers in descending order\n");
int *num = malloc(len*sizeof(int));
i =0;
while(m>0)
{
num[i++] = m%10;
m= m/10;
}
/* Any sorting technique can be used to sort the array */
for(i=0;i<len;i++)
{
max = num[i];
x = i;
for(j=i+1;j<len;j++)
{
if(num[j] > max)
{
max = num[j];
x = j;
}
}
num[x] = num[i];
num[i]= max;
}
/* Priting the entered number in descending order */
for(i=0;i<len;i++)
printf("%d",num[i]);
printf("\n");
return 0;
}
#include <stdio.h>
static inline char *max_c(size_t size, const char array[size]){
const char *p = array;
for(int i=1;i<size;++i){
if((unsigned char)*p < array[i])
p = array + i;
}
return (char*)p;
}
static inline void swap(char *a, char *b){
char tmp = *a;
*a = *b;
*b = tmp;
}
int main(void) {
char digits[32];
int len;
printf("input nums : ");
scanf("%31[0-9]%n", digits, &len);
for(int i = 0; i<len-1 ; ++i){
//swaped with the first element to select an element of maximum
swap(max_c(len - i, digits + i), &digits[i]);
}
printf("%s\n", digits);
return 0;
}
Just converting the int to a string using sprintf then reversing the string would probably be the easiest.
#include <stdio.h>
#include <string.h>
int main(){
int num = 123;
char str[15];
char backwards[15];
int i, j = 0;
sprintf(str,"%d",num);
for(i = strlen(str) - 1; i >= 0; i--){
backwards[j++] = str[i];
}
backwards[j] = '\0';
printf("%s\n",backwards);
return 0;
}
Then if you need to turn it back into an int then using atoi would do the trick.
//Remember to include this if you use atoi
#include <stdlib.h>
//Then just use this after you have reversed the string to
//convert it back to type int
num = atoi(&backwards);
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;
}