Thursday, 21 July 2016

Value of array changes while copying to another array

I looked into this problem, found i was using a local variable in a function so it was going out of scope, changed the code but the problem persists. The code :

#include <iostream>
#include <cmath>
#define SAMPLE_SIZE 12

class sine_curve
{
public:


int get(double amplitude, double time, double *x, double frequency, int sample)
{
    for(sample = 0; sample <= time; sample++)
    {
        x[sample] = amplitude * sin(2 * 3.142 * frequency * sample);
        std::cout << x[sample]<<"\t";
    }

    std::cout << std::endl;
return *x;    }

};

class quantisation_
{
public:

};

int main()
{

double amplitude, time, frequency, x[SAMPLE_SIZE], y[SAMPLE_SIZE];
int sample;

std::cout << "Enter amplitude: ";
std::cin >> amplitude;
std::cout << "Enter time: ";
std::cin >> time;
sine_curve sine;
sine.get(amplitude, time, x, frequency,sample);

for(sample = 0; sample <= time; sample++)
{
    std::cout << x[sample] << std::endl;
}

std::cout << std::endl;

*y = *x;
for(sample = 0; sample <= time; sample++)
{
    std::cout << y[sample] << std::endl;
}
}

The output :: Enter amplitude: 23
Enter time: 3
0 1.00344e-307 2.00687e-307 3.01031e-307
0
1.00344e-307
2.00687e-307
3.01031e-307

0
2.07377e-317
5.61259e-321
2.12203e-314

When I print the array y, the value changes. i followed this link and the rest I don't remember but their answer was also the same.



from Recent Questions - Stack Overflow http://ift.tt/29OsVoD
via https://ifttt.com/ IFTTT

No comments:

Post a Comment