Question

In: Computer Science

Below is part of my code to sort table of x,y,z cordinates using x,y with qsort....

Below is part of my code to sort table of x,y,z cordinates using x,y with qsort. I can read all the data into *xyz part of the structure and allocate memory, I keep getting errors on line 7, "read access violation". I would take any help...Thanks

typedef struct

{

       int x;

       int y;

       int z;

} Coordinates;

typedef Coordinates * xyz;

int compare(const void *p1, const void *p2)

       {

       const xyz num1 = *(const xyz *)p1;

       const xyz num2 = *(const xyz *)p2;

7      if (num1->x < num2->x)    //read access violation

       {

              return -1;

       }

       else if (num1->x > num2->x)

       {

              return 1;

       }

       else

       {

              if (num1->y < num2->y)

              {

                     return -1;

              }

              else if (num1->y > num2->y)

              {

                     return 1;

              }

              else

              {

                     return 0;

              }

       }

Main

{

//allocates memory

Coordinates * xyz = (Coordinates *) malloc(sizeof(Coordinates) * numElem);

qsort(xyz, numElem, sizeof(xyz), compare);

}

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <stdio.h>

#include <stdlib.h>

typedef struct

{

    int x;

    int y;

    int z;

} Coordinates;

int compare(const void *p1, const void *p2)

{

    const Coordinates *num1 = (const Coordinates *)p1;

    const Coordinates *num2 = (const Coordinates *)p2;

    if (num1->x < num2->x) //read access violation

    {

        return -1;

    }

    else if (num1->x > num2->x)

    {

        return 1;

    }

    else

    {

        if (num1->y < num2->y)

        {

            return -1;

        }

        else if (num1->y > num2->y)

        {

            return 1;

        }

        else

        {

            return 0;

        }

    }

}

int main()

{

    //allocates memory

    Coordinates *xyz = (Coordinates *)malloc(sizeof(Coordinates) * numElem);

    

    qsort(xyz, numElem, sizeof(Coordinates), compare);

    return 0;

}


Related Solutions

In the python code below, describe the scope of the variables x, y, z and u....
In the python code below, describe the scope of the variables x, y, z and u. def my_func(x):         y = x - 2         return y z = 6 if z > 4:           u = my_func(6)          print(u)
Use the information for securities X, Y and Z in the table below to answer parts...
Use the information for securities X, Y and Z in the table below to answer parts a and b. Security X Security Y Security Z Expected return 8% 8% 17% Beta 0.7 1.3 2.5 The risk-free rate is 2% and the expected return of the market portfolio is 8%. According to the CAPM, are these securities overpriced, fairly priced or underpriced? Using the CAPM, calculate the abnormal return of a portfolio that takes a long position in security X by...
Use the information for securities X, Y and Z in the table below to answer parts...
Use the information for securities X, Y and Z in the table below to answer parts a and b: Security X Security Y Security Z Expected return 8% 8% 17% Beta 0.7 1.3 2.5 The risk-free rate is 2% and the expected return of the market portfolio is 8%. According to the CAPM, are these securities overpriced, fairly priced or underpriced? Using the CAPM, calculate the abnormal return of a portfolio that takes a long position in security X by...
Please explain this prolog code line by line. union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W). union([X|Y],Z,[X|W]) :- \+ member(X,Z),...
Please explain this prolog code line by line. union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W). union([X|Y],Z,[X|W]) :- \+ member(X,Z), union(Y,Z,W). union([],Z,Z).
For each of the formulas below, state whether it is true or false. a) pX,Y,Z(x,y,z)=pY(y)pZ∣Y(z∣y)pX∣Y,Z(x∣y,z)   ...
For each of the formulas below, state whether it is true or false. a) pX,Y,Z(x,y,z)=pY(y)pZ∣Y(z∣y)pX∣Y,Z(x∣y,z)       Select an option         True         False    b) pX,Y∣Z(x,y∣z)=pX(x)pY∣Z(y∣z)       Select an option         True         False    c) pX,Y∣Z(x,y∣z)=pX∣Z(x∣z)pY∣X,Z(y∣x,z)       Select an option         True         False    d) ∑xpX,Y∣Z(x,y∣z)=1       Select an option         True         False    e) ∑x∑ypX,Y∣Z(x,y∣z)=1       Select an option         True   ...
In table 1.1 below, information on three stocks (X, Y and Z) in the next period...
In table 1.1 below, information on three stocks (X, Y and Z) in the next period is stated. The stocks are traded on a financial market, where the usual assumptions for a normal market are satisfied. It is assumed, that a risk-free asset is traded on the financial market as well, which has a return of 5% (rf) in the next period. Problem: Determine the tangent portfolio of the three stocks (i.e. determine the weights that the three stocks have...
Using the table below, calculate the variance of X, variance of Y, standard deviation of X,...
Using the table below, calculate the variance of X, variance of Y, standard deviation of X, standard deviation of Y,   the covariance between X and Y, the correlation coefficient between X and Y, and find the regression line assuming X is the independent variable (find the slope and the intercept). Show your work Fill in the Blanks: X Y X-Xbar Y-Ybar (X-Xbar)^2 (Y-Ybar)^2 (X-Xbar)*(Y-Ybar) 5 3 9 6 12 15 17 20 21 23 Sum Calculate the following and show...
The full three-dimensional Schrödinger equation is −ℏ22m(∂2∂x2ψ(x,y,z)+∂2∂y2ψ(x,y,z)+∂2∂z2ψ(x,y,z))+U(x,y,z)ψ(x,y,z)=Eψ(x,y,z). By using the substitutions from the introduction, this becomes...
The full three-dimensional Schrödinger equation is −ℏ22m(∂2∂x2ψ(x,y,z)+∂2∂y2ψ(x,y,z)+∂2∂z2ψ(x,y,z))+U(x,y,z)ψ(x,y,z)=Eψ(x,y,z). By using the substitutions from the introduction, this becomes −ℏ22m(∂2∂x2ψxψyψz+∂2∂y2ψxψyψz+∂2∂z2ψxψyψz)+(Ux+Uy+Uz)ψxψyψz=Eψxψyψz What is ∂2∂x2ψxψyψz? To make entering the expression easier, use D2x in place of d2ψxdx2, D2y in place of d2ψydy2, and D2z in place of d2ψzdz2. Answer in terms of ψx,ψy,ψz,D2x,D2y,andD2z
The curried version of let f (x,y,z) = (x,(y,z)) is let f (x,(y,z)) = (x,(y,z)) Just...
The curried version of let f (x,y,z) = (x,(y,z)) is let f (x,(y,z)) = (x,(y,z)) Just f (because f is already curried) let f x y z = (x,(y,z)) let f x y z = x (y z)
F(x,y,z) = x^2z^2i + y^2z^2j + xyz k S is the part of the paraboloid z...
F(x,y,z) = x^2z^2i + y^2z^2j + xyz k S is the part of the paraboloid z = x^2+y^2that lies inside the cylinder x^2+y^2 = 16, oriented upward.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT