Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

c++ - need to produce a function that detects the zeros in a array

I have this code but is not working , it keep giving me the following errors :

[Error] cannot convert 'float' to 'float*' for argument '1' to 'void zeroCrossing(float*, float*, int)'

[Error] cannot convert 'float*' to 'float' for argument '1' to 'bool getSign(float)'

[Error] cannot convert 'float*' to 'float' for argument '1' to 'bool getSign(float)'

[Error] invalid conversion from 'int' to 'float*' [-fpermissive]
#include <iostream>
#include <cstring>
using namespace std;

void zeroCrossing(float *data, float *zerCross, int nx);
bool getSign(float data);
float array[9] = {1,2,3,0,-1,-2,-3,0,1};
float *p = array;
float f1[9];
float *p2 = f1;
int bx= 2 ;

int main() {
    zeroCrossing(*p,*p2,bx);
    return 0 ;
}

/* zero crossing function */
/* data = input array */
/* zerCross = output zero crossing array */
void zeroCrossing(float *data[], float *zerCross[], int nx)
{
    int i;
    bool sign1, sign2;

    memset(zerCross, 0, nx*sizeof(float));//copies the 0  to the first characters of the string 
                                            //pointed to, by argument 

    for(i=0; i<nx-1; i++)     /* loop over data  */
    {
        sign1 = getSign(data[i]);
        sign2 = getSign(data[i+1]);
        if(sign1!=sign2)  /* set zero crossing location */
            zerCross[i+1] = 1;
    }
}
/* get sign of number */
bool getSign(float data)
{
    if(data>0)      /* positif data */
        return (1);
    else            /* negatif data */
        return (0);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you do coding, you should compile the code after some lines of codes, unless you are pro.

There are two things I could say about your codes

  1. Function declaration: void zeroCrossing(float *data, float *zerCross, int nx); and void zeroCrossing(float *data[], float *zerCross[], int nx) are not the same.

  2. Variable name: Should avoid names like array because they are used in the standard lib.

This is my fix to make your code compilable

#include <iostream>
#include <cstring>
using namespace std;


void zeroCrossing(float *data, float *zerCross, int nx);
bool getSign(float data);

float array_[9] = {1,2,3,0,-1,-2,-3,0,1};
float* p = array_;
float f1[9];
float *p2 = f1;
int bx= 2 ;

/* get sign of number */
bool getSign(float data)
{
    if(data>0)      /* positif data */
        return (1);
    else            /* negatif data */
        return (0);
}


/* zero crossing function */
/* data = input array */
/* zerCross = output zero crossing array */
void zeroCrossing(float *data, float *zerCross, int nx)
{
    int i;
    bool sign1, sign2;

    memset(zerCross, 0, nx*sizeof(float));//copies the 0  to the first characters of the string
    //pointed to, by argument

    for(i=0; i<nx-1; i++)     /* loop over data  */
    {
        sign1 = getSign(data[i]);
        sign2 = getSign(data[i+1]);
        if(sign1!=sign2)  /* set zero crossing location */
            zerCross[i+1] = 1;
    }
}

int main() {
    zeroCrossing(p,p2,bx);
    return 0 ;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...