samedi 25 avril 2015

Wrong order of elements in list in Java


I use following code to replace a list's elements. After replace elements aren't in the same order in the edges list, than in myCoordinates list. Is it maybe some randomness or what causes the change of its sequence?

for (int i = 0; i < edges.size(); i++) {
   int j = i*4;
   Edge edge = edges.get(i);
   edge.p1.x = myCoordinates.get(j);
   edge.p1.y = myCoordinates.get(j+1);
   edge.p2.x = myCoordinates.get(j+2);
   edge.p2.y = myCoordinates.get(j+3);
}


RegEx to divide string by +*-/ and keep delimiter?


How can I split a string into pieces every time a "/*-+" appears and keep the delimiter?

I've tried

@new = split( [/\+/]|[/\-/]|[/\//]|[/\*/], $start   );

and this doesn't even work to split in Perl. To keep the delimiter I believe I need something like this

/(?<=\.)/

But I'm not sure how that works.


Trouble with dynamic array of pure base class c++


I have this programming assignment for an intro c++ class. The program is supposed to use virtual functions to input, calculate and display quantities associated with various shapes. The Shape class must contain all pure virtual functions: display, getdimensions, area, perimeter, and volume.

The program driver must allocate an array of 20 pointers to Shape then repeatedly fill the array with the objects entered.

My program is quitting when it gets to the display call. So I take this to mean that either my getDimensions or display functions are not working correctly. I've spent hours trying to sort it out and can't seem to find what exactly is wrong if anyone could point me in the right direction.

Here is my code:

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

const double PI=3.141592654;

class Shape
{
protected:
    double x, y;
public:
    Shape(): x(1.0),y(1.0){}
    Shape(double a, double b): x(a),y(b){}
    virtual void display()=0;
    virtual void getDimensions()=0;
    virtual double area()=0;
    virtual double perimeter()=0;
   virtual double volume()=0;
};

class Rectangle: public Shape
{
public:
    Rectangle():Shape(){}
    Rectangle(double a, double b):Shape(a,b){}
    void display();
    void getDimensions()
    {
        cout<<"Input for Rectangle - "
            <<"enter length and width: ";
        cin>>x>>y;
    }
    double area()
        {return x*y;}
    double perimeter()
        {return (2*x)+(2*y);}
    double volume()
        {return 0;}
};

class Circle: public Shape
{
private:
    double radius;
public:
    Circle(): radius(1.0){}
    Circle(double r): radius(r){}
    void display();
    void getDimensions()
    {
        cout<<"Input for Circle - enter radius: ";
        cin>>radius;
    }
    double area()
        {return PI*radius*radius;}
    double perimeter()
        {return 2*PI*radius;}
    double volume()
        {return 0;}
 };

class Triangle: public Shape
 {
private:
    double z;
public:
    Triangle():z(1.0), Shape(){}
    Triangle(double f, double g, double w):z(w), Shape(f,g){}
    void display();
    void getDimensions()
    {
        cout<<"Input for Triangle - "
            <<"enter side 1, side 2, side 3: ";
        cin>>x>>y>>z;
    }
    double area()
    {
        double half, underroot;
        half = perimeter()/2;
        underroot = half*(half-x)*(half-y)*(half-z);
        return sqrt(underroot);
    }
    double perimeter()
        {return x+y+z;}
    double volume()
        {return 0;}
};

class Box: public Shape
{
private:
    double p;
public:
    Box():p(1.0), Shape(){}
    Box(double a, double b, double c): Shape(a,b), p(c){}
    void display();
    void getDimensions()
    {
        cout<<"Input for Box - "
            <<"enter length, width, and height: ";
        cin>>x>>y>>p;
    }
    double area()
        {return (2*x*y)+(2*x*p)+(2*y*p);}
    double perimeter()
        {return 0;}
    double volume()
        {return x*y*p;}
};

class Can: public Shape
{
public:
    Can():Shape(){}
    Can(double r, double h):Shape(r,h){}
    void display();
    void getDimensions()
    {
        cout<<"Input for Can - "
            <<"enter radius and height: ";
        cin>>x>>y;
    }
    double area()
    {
        double base;
        base = PI*x*x;
        return (2*PI*x*y)+(2*PI*x*x);
    }
    double perimeter()
        {return 0;}
    double volume()
        {return PI*x*x*y;}
};

class Cone: public Shape
{
public:
    Cone():Shape(){}
    Cone(double r, double h):Shape(r, h){}
    void display();
    void getDimensions()
    {
        cout<<"Input for Cone - "
            <<"enter radius and height: ";
        cin>>x>>y;
    }
    double area()
    {
        double underroot;
        underroot = sqrt((x*x)+(y*y));
        return (PI*x*x)+(PI*x*underroot);
    }
    double perimeter()
        {return 0;}
    double volume()
        {return (1/3)*PI*x*x*y;}
};

class Ball: public Shape
{
private:
    double radius;
public:
    Ball():radius(1.0){}
    Ball(double r):radius(r){}
    void display();
    void getDimensions()
    {
        cout<<"Input for Ball - "
            <<"enter radius: ";
        cin>>radius;
    }
    double area()
        {return 4*PI*radius*radius;}
    double perimeter()
        {return 0;}
    double volume()
        {return (4/3)*PI*radius*radius*radius;}
};

int main()
{
    int cnt=0, cnt2=0, choice;
    char yorn;

    Shape *sPtr[20];

    while(cnt<20)
    {
        cnt++;
        cnt2++;
        cout<<"\nIt is time to enter your shape selection"
            <<" and dimensions.\n"
            <<"Enter the number of the shape type: \n"
            <<"\t1 - Rectangle\n"
            <<"\t2 - Circle\n"
            <<"\t3 - Triangle\n"
            <<"\t4 - Box\n"
            <<"\t5 - Can\n"
            <<"\t6 - Cone\n"
            <<"\t7 - Ball\n"
            <<"=> ";
        cin>>choice;
        while (choice<1||choice>7)
        {
            cout<<"Invalid selection. Re-enter: \n"
                <<"=> ";
            cin>>choice;
        }
        switch (choice)
        {
        case 1:
        {
            sPtr[cnt]=new Rectangle;
            sPtr[cnt]->getDimensions();
            break;
        }
        case 2:
        {
            sPtr[cnt]=new Circle;
            sPtr[cnt]->getDimensions();
            break;
        }
        case 3:
        {
            sPtr[cnt] = new Triangle;
            sPtr[cnt]->getDimensions();
            break;
        }
        case 4:
        {
            sPtr[cnt] = new Box;
            sPtr[cnt]->getDimensions();
            break;
        }
        case 5:
        {
            sPtr[cnt] = new Can;
            sPtr[cnt]->getDimensions();
            break;
        }
        case 6:
        {
            sPtr[cnt] = new Cone;
            sPtr[cnt]->getDimensions();
            break;
        }
        case 7:
        {
            sPtr[cnt] = new Ball;
            sPtr[cnt]->getDimensions();
            break;
        }
        }

        cout<<"\nSelect another shape? (y or n): ";
        cin>>yorn;
        if(yorn=='n'||yorn=='N')
            cnt=20;
    }
    for(int i=0; i<cnt2; i++)
    {
        sPtr[i]->display();
    }
    return 0;
}

void Rectangle::display()
{
    cout<<"\nRectangle: "
        <<"\nDimensions: "
        <<"Length: "<<x<<"\nWidth: "<<y
        <<"\nArea: "<<area()
        <<"\nPerimeter: "<<perimeter();
}

void Circle::display()
{
    cout<<"\nCircle: "
        <<"\nDimensions: "
        <<"\nRadius: "<<radius
        <<"\nArea: "<<area()
        <<"\nPerimeter: "<<perimeter();
}

void Triangle::display()
{
    cout<<"\nTriangle: "
        <<"\nDimensions: "
        <<"\nSide 1: "<<x
        <<"\nSide 2: "<<y
        <<"\nSide 3: "<<z
        <<"\nArea: "<<area()
        <<"\nPerimeter: "<<perimeter();
}

void Box::display()
{
    cout<<"\nBox: "
        <<"\nDimensions: "
        <<"\nLength: "<<x
        <<"\nWidth: "<<y
        <<"\nHeight: "<<p
        <<"\nSurface area: "<<area()
        <<"\nVolume: "<<volume();
}

void Can::display()
{
    cout<<"\nBox: "
        <<"\nDimensions: "
        <<"\nRadius: "<<x
        <<"\nHeight: "<<y
        <<"\nSurface area: "<<area()
        <<"\nVolume: "<<volume();
}

void Cone::display()
{
    cout<<"\nCone: "
        <<"\nDimensions: "
        <<"\nRadius: "<<x
        <<"\nHeight: "<<y
        <<"\nSurface area: "<<area()
        <<"\nVolume: "<<volume();
}

void Ball::display()
{
    cout<<"\nBall: "
        <<"\nDimensions: "
        <<"\nRadius: "<<radius
        <<"\nSurface area: "<<area()
        <<"\nVolume: "<<volume();
}


C# array accessiblity


Is there a way to prevent reinitialization of an array? (to force specific length)

example initial:

int[] array = new int[3]{0,0,0};

usage:

array = new int[5]{1,2,3,4,5};

The above usage will reinitialize the array with length of 5.

The array will always have 3 elements, but the values of the elements will always be changing.

I am trying to avoid doing the following to assign it's values:

array[0] = 1;
array[1] = 2;
array[2] = 3;


Windows phone 8 play sound according to bit array


I have a textbox in my app, and for example it contains: "Hello World!". I have a wav file that represents a beep(1 ms). In accordance to the "Hello World" bits representation I want to play the beeps in this manner: play beep if '1' or wait for 1 ms(silence). What is the best way to achive this? Thanks. (I develop in C#)


php - get top repetitions from an array


I have an array which is of the form

array(23) { [0]=> string(13) "Richmond Town" [1]=> string(11) "Tasker Town" [2]=> string(18) "Vittal Mallya Road" [3]=> string(15) "Shanthala Nagar" [4]=> string(19) "Sampangi Rama Nagar" [5]=> string(9) "Jayamahal" [6]=> string(15) "Shanthala Nagar" [7]=> string(19) "Sampangi Rama Nagar" [8]=> string(15) "Shanthala Nagar" [9]=> string(15) "Shanthala Nagar" [10]=> string(11) "Nehru Nagar" [11]=> string(13) "Richmond Town" [12]=> string(21) "Koramangala 1st Block" [13]=> string(14) "Cleveland Town" [14]=> string(22) "Yellappa Chetty Layout" [15]=> string(13) "HAL 2nd Stage" [16]=> string(15) "Shanthala Nagar" [17]=> string(11) "Koramangala" [18]=> string(11) "Koramangala" [19]=> string(19) "Koramangala 6 Block" [20]=> string(11) "5th A Block" [21]=> string(15) "Pulikeshi Nagar" [22]=> string(16) "Cambridge Layout" } 

Now I want the top 5 repetitions from this array. Please help me out.


Control array in runtime C#


I need an array of Timer component with unknown size. New Timer may be added or removed existing.

Timer[] timers= new Timer[];

Above line shows must have array size.

Any help?