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.
Related
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.
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'm beginner in C programming language, i have piece of code written in C language on linux Platform my code is :
#include <stdio.h>
int main(int argc, char* argv[]){
printf("Count Of Args %d \n",argc);
int i = 0;
while(i < argc){
printf("%s \n",argv[i]);
printf("loop N: %d \n",i);
i++;
}
return 0;
}
While loop does not working and i don't know why... please show me where is problem? thanks.
Works perfectly:
$ ./program abc def
Count Of Args 3
./program
loop N: 0
abc
loop N: 1
def
loop N: 2
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'm trying to initialise all elements of a 3D array "A". The array consists of 2000x100x4 integer elements of the 3D array and is stored in row-major order. Each index at position [i,j,k] in "A" must be initialised with the value i*i*i + j*j*j.
How can I do this using for loops? Any suggestions? Thanks.
for(i=0;i<2000;i++)
for(j=0;j<100;j++)
for(k=0;k<4;k++)
A[i][j][k]= (i*i*i) + (j*j*j);
I hope I understood your question correctly. Or were you looking for something else?
It's not something hard to do:
int A[2000][100][4];
int i,j,k;
for (i=0;<2000;i++)
{
for (j=0;j<100;j++)
{
for (k=0;k<4;k++)
{
A[i][j][k] = i*i*i + j*j*j;
}
}
}
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?
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".