Answer to Question #314898 in C++ for Seng

Question #314898

Write a simple c++ program that will display the area and circumference of a circle with a radius of 5. Note the program must implement copy constructors. 


1
Expert's answer
2022-03-20T14:59:18-0400
char* drawCircle(int R, int geom) {
    if (R > 200 || R < 1) return NULL;
    int S, i, Y = 2 * R + 2;
    if (geom)
        S = Y * (R + 1);
    else
        S = Y * (Y - 1);
    char* buf = malloc(S + 1);
    if (!buf) return NULL;
    memset(buf, ' ', S);
    buf[S] = 0;
    for (i = Y - 1; i < S; i += Y) buf[i] = '\n';
    inline void DrawPixel(int x, int y) {
        if (geom) {
            if (y % 2 == 0) buf[Y * y / 2 + x] = '*';
        }
        else {
            buf[Y * y + x] = '*';
        }
    }

    int x = R;
    int y = 0;
    int radiusError = 1 - x;
    while (x >= y) {
        DrawPixel(x + R, y + R);
        DrawPixel(y + R, x + R);
        DrawPixel(-x + R, y + R);
        DrawPixel(-y + R, x + R);
        DrawPixel(-x + R, -y + R);
        DrawPixel(-y + R, -x + R);
        DrawPixel(x + R, -y + R);
        DrawPixel(y + R, -x + R);
        y++;
        if (radiusError < 0) {
            radiusError += 2 * y + 1;
        }
        else {
            x--;
            radiusError += 2 * (y - x) + 1;
        }
    }
    return buf;
}


int main(int argc, char** argv) {
    int i, R;
    char* buf;
    for (i = 1; i < argc; i++) {
        if (!(buf = drawCircle(R = atoi(argv[i]), 1))) {
            cout << "Wrong parameter "<<argv[i] << endl;
            continue;
        }
        cout << "Circle with R = " << R << buf << endl;
        free(buf); buf = NULL;
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog