c - Program working fine for smaller values but throwing Segmentation Fault for bigger values -


in following program when value of n less 100 program executing bigger values of n showing segmentation fault.i sit because of less memory or wrong program??

#include<stdio.h>   int main()  { int n,iteration,max_iteratons;  int i,j,k,n,index,boundary1,boundary2;  double h[2][100][100];  int current = 0;  int next = 1;  printf("enter number of points\n ");  scanf("%d", &n);  boundary1=0.4*n;  boundary2=(0.6*n);  printf("the boundary between %d , %d    .\n",boundary1,boundary2);  for(k=0;k<2;k++)  {  for(i=0;i<n;i++)  {      for(j=0;j<n;j++)         {              if((i==0)&&(j>=boundary1&&j<boundary2))             h[k][i][j]=100;             else             h[k][i][j]=20;          }  }  }  printf("initial values\n");  index = n/10;  for(i=0;i<n;)  {     for(j=0;j<n;)      {         printf("%.2f\t",h[0][i][j]);          j=j+index;      }      i=i+index;      printf("\n");   }   } 

when n > 100, h accessed index greater 100, inside nested loop

h[k][i][j]=100; 

but h defined

double h[2][100][100]; 

you going out of bounds h

if want n greater 100 need redefine h or malloc it.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -