don't know why the program is crashing [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have written a code to find nth ugly number(a number which has at least one prime factor grater than 5) where n is a given input. my program runs well if the user inputs something less than 240. But if input gets bigger than that, program crashes!. My question is if it is a time consuming problem then it should take time but why the program crashes? I have used double everywhere so it might not be the matter of variable capacity!!
my code is below:
#include<stdio.h>
#include<math.h>
double primes[1000]={2,3,5};
int serial=3;
double next_prime()
{
double f=primes[serial-1]+2;
int count;
for(count=1;primes[count]<=(sqrt(f)+1) && count<serial;count++){
if(fmod(f,primes[count])==0){
f+=2;
count=1;
}
}
return primes[serial++]=f;
}
int main()
{
double ugly_serial=12,ugly_number=16,j;
int c,count,loop,input;
scanf("%d",&input);
while(ugly_serial<input)
{
loop=0;
for(c=3;primes[c-1]<=sqrt(ugly_number);c++){
j=next_prime();
}
for(count=3;count<c;count++){
if(fmod(ugly_number,primes[count])==0){
loop=1;
break;
}
}
if(loop==0){ugly_serial++;}
ugly_number++;
}
printf("%.0lf",ugly_number);
return 0;
}

I have compiled and run your code. The program works fine with all the input I have tried, including 56565.
Are you sure you are running the most recently compiled version of your program?

Related

What is wrong with my code [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have to write a program in c language and this is the code : The problem is that when I try to compile it it says : syntax error before return .Where is my error?
#include <stdio.h>
int main (void)
{
char i,c2,j;
int c=4;
i=j=3;
while (++i <=c)
{
int j=1;
printf("\n Nr1=%c Nr2=%d",64+i,c2);
} do;
return 1;
}
Remove the do from your code. Just while (++i <= c) { /* ... */ };
You are redeclaring j inside the while loop. Remove int j=1;
and remove the do; at the end of your while
There is no such thing as while ... do loop in C. There are while loops or do ... while loops.

Frequency of values of an array [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am trying to create a program that will count the frequency of 10 numbers that i will choose but when i am trying to run it it doesn't even run the printf and closes.Any ideas? thanks
#include <stdio.h>
int main()
{
int i,j,A[10]={0},C[10]={0};
for(i=0;i<10;i++)
{
scanf("%d /n",&A[i]);
}
for(j=0;j<10;j++)
{
if(A[i]==j)
{
C[j]=C[j]+1;
printf(" %d ",C[j]);
break;
}
}
getch();
}
The line
if(A[i]==j)
looks wrong - i was the counter for a previous loop and is now 10 (so beyond the bounds of your array). Did you mean
if(A[j]==j)
// ^
instead?
Changing this makes the program run for me. I don't think it does what you want yet. The break statement causes execution to halt the first time you find any match.
Hopefully this is enough hints to allow you to investigate how to count frequency of the numbers then print them all out yourself.
You need to again loop over the A array in order to check the value of each element:
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(A[i]==j)
{
C[j]=C[j]+1;
printf(" %d ",C[j]);
break;
}
}
}
If you know that the values in A will range from 0-9 then you could completely remove the inner loop and just do C[A[i]]++;
After this your C array will contain a count of each number the user input. e.g. C[5] will contain the number of 5s found, so you can output this as you see fit

What is causing this weird output? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have the following code:
void doPlayerMove(void)
{
bool moved = false;
while (!moved)
{
printf("\nWhere is the piece you want to move?(rc):");
int r = getchar() - '0';// gets the row number
int c = getchar() - '0';// gets the column number
printf("%d:%d", r, c);// prints the chosen row/column
clearInput();
printf("\nWhere is the space you want to move to?(rc):");
int r2 = getchar() - '0';
int c2 = getchar() - '0';
printf("%d:%d", r2, c2);
...
}
}
void clearInput(void)
{
while(getchar() != '\n');
}
this is the output i get:
Where is the piece you want to move?(rc):51
5:1
Where is the space you want to move to?(rc):40
4:00
Whats up with the extra 0? Does anyone see where the problem is?
As the OP says in a comment:
Problem solved, it was some output from some function i was calling in
the ... sorry for the false alarm!

runtime error sigsegv [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
i am getting runtime error sigsegv, i don't understand the problem with this code.
this is a program to match initial substring with with array of string having maximum priority.
#include<stdio.h>
#include<limits.h>
int main() {
int T,i,N;
char si[T][1000];
long vi[T];
scanf("%d",&T);
for(i=0;i<T;++i)
scanf("%s%ld",&si[i],&vi[i]);
scanf("%d",&N);
while(N--) {
char str[1000];
scanf("%s",str);
int j,maxPindex=-1;
long maxPriority=LONG_MIN;
int l=strlen(str);
for(j=0;j<T;++j) {
if(strlen(si[j])>=l && strncmp(str,si[j],l)==0 && vi[j]>maxPriority) {
maxPriority=vi[j];
maxPindex=j;
}
}
//free(str);
if(maxPindex==-1) printf("NO\n");
else printf("%s\n",si[maxPindex]);
}
return (0);
}
It crashes already when it tries to create the variable "si". A C variable inside a function, such as the variable "T", starts with random garbage as its value. For example, it could contain 918128238. Then, when trying to create "si", this would be a very large array, and it doesn't fit.
You need to read a value for "T" before "si" and "vi" are created. That is, move your scanf before the declarions of "si" and "vi".

Can someone help me put my brain to rest "C Language" [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Ok this is a simple C code but for some unknown reason the program refuse to compile and give segmentation fault 11 Please help me
#include <stdio.h>
typedef struct {
int P_answer[9];
int number;
} BOX;
int main()
{
BOX Matrix[8][8];
int i,j;
int k;
for(i=0;i<9;i++){
for(j=0;j<9;j++){
Matrix[i][j].number=0;
Matrix[i][j].P_answer[0]=1;
Matrix[i][j].P_answer[1]=2;
Matrix[i][j].P_answer[2]=3;
Matrix[i][j].P_answer[3]=4;
Matrix[i][j].P_answer[4]=5;
Matrix[i][j].P_answer[5]=6;
Matrix[i][j].P_answer[6]=7;
Matrix[i][j].P_answer[7]=8;
Matrix[i][j].P_answer[8]=9;
}
}
}
Matrix is an 8-by-8 array; each of your loops goes through 9 iterations.
The indexes of an array go from 0 to (size-1).
In your for-loops you go from 0 to size.
That's the reason of your segmentation fault.

Resources