I am compiling one sample code. But I am getting the answer when I am compiling that code for input = 5, If I change input from 5 to 6., then once answer came, but next time for same input, throwing a message "return value 3221226356". If again I change the input from 6 to 8, then again that message is coming.
#include <iostream>
#include <string>
#include <cmath>
void loop1(float *a, int &k ) \
{
int i = k / 2 ; \
for (int x = 0 ; x < i ; x++ ) \
{
a[x] = x ; \
std::cout<<"a = "<< a[x] <<'\n' ; \
}
}
void loop2(float *a, int &i, int &j ) \
{
for (int x = i ; x < j ; x++ ) \
{
a[x] = a[x-i] ; \
std::cout<<"a ["<<x<<"]= "<< a[x] <<'\n' ; \
}
}
void loop3(float *a, int &i)
{
for (int x = 0 ; x < i ; x++ )
{
a[x] = x * x * x ;
a[x] += a[x];
}
}
int main() \
{
int nx ; \
float* xpos = NULL; \
xpos = new float[nx]; \
std::cout<<"enter the value for nx " ; \
std::cin>>nx ; \
int a = nx/2 ; \
std::cout<<"a (= nx/2 )= "<< a <<'\n' ; \
loop1(xpos, nx ); \
loop2(xpos, a, nx); \
for (int x = 0 ; x < nx ; x++ ) \
{
std::cout<<"xpos = "<< xpos[x] <<'\n' ; \
}
for (int x = 0 ; x < nx ; x++ ) \
{
std::cout<<"new xpos = "<< xpos[x] <<'\n' ; \
}
return 0 ;
}
Result for Input = 5
enter the value for nx 5
a (= nx/2 )= 2
a = 0
a = 1
a [2]= 0
a [3]= 1
a [4]= 0
xpos = 0
xpos = 1
xpos = 0
xpos = 1
xpos = 0
new xpos = 0
new xpos = 1
new xpos = 0
new xpos = 1
new xpos = 0
--------------------------------
Process exited after 4.442 seconds with return value 0
Press any key to continue . . .
Result for Input = 6
enter the value for nx 6
a (= nx/2 )= 3
a = 0
a = 1
a = 2
a [3]= 0
a [4]= 1
a [5]= 2
xpos = 0
xpos = 1
xpos = 2
xpos = 0
xpos = 1
xpos = 2
new xpos = 0
new xpos = 1
new xpos = 2
new xpos = 0
new xpos = 1
new xpos = 2
--------------------------------
Process exited after 3.427 seconds with return value 0
Press any key to continue . . .
Result for Input = 8
<pre lang="c++">enter the value for nx 8
a (= nx/2 )= 4
a = 0
a = 1
a = 2
a = 3
a [4]= 0
a [5]= 1
a [6]=
--------------------------------
Process exited after 7.167 seconds with return value 3221226356
Press any key to continue . . .
Have you got any idea about what I did wrong ?
Is there any specific name of such error?
But, I can't find why that failed.
What I have tried:
I tried to vary the Input from one to other. But giving the message.