I have a problem in C++(complier dev 4.9.9.2). My program have complie with no error but out put is wrong and not run show with massage “ some intermediated data is missing”. In debugging its show massage “segmentation Error” .

#include<iostream>
#include<math.h>
#include<conio.h>
#include<fstream>
#include<dos.h>
#include<iomanip>
const int row=195;
const int col=235;
using namespace std;
float velocity (int,float,float,float,float);
int main()
{
 
ifstream in("data.txt");
ofstream out("vel.txt");
int **EL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **el= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **SA= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **RL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **LSD= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **ASP= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **SL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **X= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **Y= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
float **FC= (float**)malloc(row*col*sizeof(float) + col*sizeof(float*));
double **vel_g=(double**)malloc(row*col*sizeof(double) + col*sizeof(double*));
 
int i,m,n ,j,x,y,t,Max,k,k1;
 
float cvel,vel1,vel2,vel3,vel4,vel5,vel6,vel8,vel7,vel;
 
 
for(i=0;i<row;i++)
 
for( j=0; j<col;j++)
{
in>>el[i][j];
EL[i][j]=el[i][j];
in>>RL[i][j];
in>>SA[i][j];
in>>SL[i][j];
in>>FC[i][j];
in>>LSD[i][j];
in>>ASP[i][j];
}
 
for( k=0; k<row ;k++)
for( k1=0; k1<col;k1++)
{
Max=-2;
 
for(int i=0; i<row;i++)
for(int j=0; j<col;j++)
 
if(el[i][j]>Max)
{
Max=el[i][j];
x=j;
y=i;
}
 
X[k][k1]=x;
Y[k][k1]=y;
el[y][x]=-21;
cout<<k<<"\t"<<k1<<"\t"<<X[k][k1]<<endl; 
 
}
for(int a=0;a<row;a++)
for(int b=0;b<col;b++)
vel_g[a][b]= 0 ;
for(int k=0; k<row;k++)
for(int k1=0;k1<col;k1++)
{ 
 
n=X[k][k1];
m=Y[k][k1];
if(1==LSD[m][n]) 
{
while(SA[m][n]>=25)
{
vel = velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]); 
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
t=ASP[m][n]; 
switch(t)
{
case(1): {m= m-1;n=n; vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
if(m<0||m>row||n<0||n>col) {cout<<"error1\n";} break;}
case(2): {m= m-1;n=n+1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7)))); 
if(m<0||m>row||n<0||n>col) {cout<<"error2\n"; }break;}
case(3): {m= m;n=n+1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
if(m<0||m>row||n<0||n>col) {cout<<"error3\n"; }break;}
case(4): {m= m+1;n=n+1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7)))); 
if(m<0||m>row||n<0||n>col) {cout<<"error4\n"; }break;}
case(5): {m= m+1;n=n;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7)))); 
if(m<0||m>row||n<0||n>col) {cout<<"error5\n"; }break;}
case(6): {m= m+1;n=n-1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
if(m<0||m>row||n<0||n>col) {cout<<"error6\n"; }break;}
case(7): {m= m;n=n-1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7)))); 
if(m<0||m>row||n<0||n>col) {cout<<"error7\n"; }break;}
case(8): {m= m+1;n=n-1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
if(m<0||m>row||n<0||n>col) {cout<<"error8\n"; }break;}
 
 
}
 
} } 
 
 
} 
 
 
for(int c=0;c<row ;c++)
for(int d=0;d<col;d++)
{ out<<c<<"\t"<<d<<"\t";
out<<vel_g[c][d]<<'\n';
}
 
return 0;}
 
 
 
 
 
float velocity( int RL,float fric,float slope, float slopeangle, float ve)
{
int heg;
float D,b;
heg=RL;
b=fric;
if(ve>1000)
{
cout<<"\t"<<"heg="<<heg<<"\tb="<<b<<"\tve"<<ve<<"\t";
getch();
} 
const double g =9.781;
slopeangle=((slopeangle*(22/7))/180);
float x = (2*g*(heg-(b*slope*cos(slopeangle))));
if(x<0) x=0;
D=slope*cos(slopeangle);
float y = sqrt(x+(ve*ve));
if(y>100000)
{
cout<<"\td="<<D<<"\tfc="<<b<<"\tg="<<g<<"\theg="<<heg<<"\nve="<<ve<<"\ty="<<y<<"\n";
 
}
return y;
}

int **EL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));

EL[0] - this is first pointer in array
EL[0][0] - this is first pointer in array POINTED BY EL[0]

since you didnt initialize it (EL[0]), you have random data in there, so you access random memory - segmentation fault

you have to use EL[i*row+j] (or EL[i*col+j])

@Bharat
<math.h>//deprecated in C++, use <cmath>
<conio.h>//Non-Standard
<dos.h>//Non-Standard

> int **EL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
1. You didn't include stdlib.h
2. Since this seems to be C++, you should be using new anyway.
3. Since your sizes seem to be constant, why are you dynamically allocating anyway?

I see that lot adds up to at least a couple of MB, so that probably explains why you gave up on int EL[row][col]; There are several ways out, which don't involve a lot of mess.

1. Make them static, you get the scope without the stack space static int EL[row][col]; 2. Dynamically allocate in one go, since the minor size is constant. int (*EL)[col] = new int[row][col]; Note the placement of the ( ), very important you get these right.

3. Since this is C++, use a vector vector< vector<int> > EL;

thankyoufor your suggestions. I used static array as mentioned by you that solves my problem. But now I need to modify the program whose array size is to be defined by the running time and hence a dyanimic array has to be used. I would therefore like to know as how to use dynamic array since the minor size is not known unlike what u have suggested for the same in
int (*EL)[col] = new int[row][col];
your kind help would see me run the program succesfully.

thanks and regards
Bharat

Like this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int ** alloc2D ( int row, int col ) {
     int **p = malloc ( row * sizeof *p );      /* all the row pointers */
     int *q  = malloc ( col * row * sizeof *q );/* all the data */
     int r;
     for ( r = 0 ; r < row ; r++ ) {            /* set pointers for each row */
         p[r] = q;
         q += col;
     }
     return p;
}

void free2D ( int **arr ) {
     free( arr[0] );    /* free all the data */
     free( arr );       /* free the row pointers */
}
int main(void)
{
  int **arr = alloc2D ( 10, 20 );
  int r, c;
  for ( r = 0 ; r < 10 ; r++ ) {
      for ( c = 0 ; c < 20 ; c++ ) {
          arr[r][c] = 0;
      }
  }
  free2D( arr );
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.