Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to get coordinates of a polygon in input using arrays

+3 votes
asked May 30, 2019 by anonymous
Vertices of polygon?4

vertex 1? 0 0

vertex 2? 0 1

vertex 3? 1 0

vertex 4? 1 1

2 Answers

0 votes
answered Mar 26 by Vipul Kumar (280 points)

I have given in both C and Python with three three code each

C

#include <stdio.h>

#include <stdlib.h>

void main(){

    int i,n,*x,*y,*z;

    printf("Enter the no. of edges or no. of vertices of the polygon : ");

    scanf("%d",&n);

    x=(int*)malloc(n*sizeof(int));

    y=(int*)malloc(n*sizeof(int));

    z=(int*)malloc(n*sizeof(int));

    for(i=0;i<n;i++){

        printf("Enter x cordinate of vertex %d :",i+1);

        scanf("%d",&x[i]);

        printf("Enter y cordinate of vertex %d :",i+1);

        scanf("%d",&y[i]);

        printf("Enter z cordinate of vertex %d :",i+1);

        scanf("%d",&z[i]);

    }

    printf("Following are the vertex of the polygon :\n");

    for(i=0;i<n;i++){

        printf("v%d(%d,%d,%d)\n",i+1,x[i],y[i],z[i]);

    }

}

PYTHON

d={}
n=int(input("Enter the number of edges or vertices: "))
for i in range(n):
    a=int(input("Enter the x-coordinate of vertex"+str(i+1)+" : "))
    b=int(input("Enter the y-coordinate of vertex"+str(i+1)+" : "))
    c=int(input("Enter the z-coordinate of vertex"+str(i+1)+" : "))
    d[i]=[a,b,c]
print("Following are the coordinates of each vertex of the polygon:")
for i in range(n):
    print("V",i+1,"(",d[i][0],",",d[i][1],",",d[i][2],")")
commented Mar 29 by Peter Minarik (101,360 points)
In C, you did not free your allocated memory.
Also, the poster seemed to have worked in the 2D space, but you provided an answer in 3D space.
0 votes
answered Mar 26 by Vincent Prof. (140 points)
#include <iostream>  // 引入 iostream 標頭檔,提供輸入/輸出功能
#include <vector>    // 引入 vector 標頭檔,提供動態陣列功能
#include <iomanip>   // 引入 iomanip 標頭檔,提供格式化輸出功能
using namespace std; // 使用 std 命名空間

int main()
{
    int n;  // n 用來儲存多邊形的頂點數量

    // ================================================================
    // 第 1 步:詢問使用者多邊形有多少個頂點
    // ================================================================
    cout << "請輸入多邊形的頂點數量:";
    cin >> n;

    // 檢查頂點數量是否有效
    if (n <= 0)
    {
        cout << "錯誤:頂點數量必須大於 0!" << endl;
        return 1;
    }

    // ================================================================
    // 第 2 步:宣告三個 vector(動態陣列),分別儲存 x、y、z 座標
    // ================================================================
    // 每個頂點需要 x、y、z 三個座標,因此用三個 vector 分別儲存
    // 使用 vector<double> 可以動態分配記憶體,不需要預先知道大小
    vector<double> x(n);  // x 座標陣列,大小為 n
    vector<double> y(n);  // y 座標陣列,大小為 n
    vector<double> z(n);  // z 座標陣列,大小為 n
    // 註:使用 double 而非 int,讓座標可以接受小數

    // ================================================================
    // 第 3 步:輸入每個頂點的座標
    // ================================================================
    for (int i = 0; i < n; i++)
    {
        cout << "\n頂點 " << (i + 1) << " 的座標:";
        cout << "\n  輸入 x 座標:";
        cin >> x[i];  // 將第 i 個頂點的 x 座標存入陣列

        cout << "  輸入 y 座標:";
        cin >> y[i];  // 將第 i 個頂點的 y 座標存入陣列

        cout << "  輸入 z 座標:";
        cin >> z[i];  // 將第 i 個頂點的 z 座標存入陣列
    }

    // ================================================================
    // 第 4 步:顯示所有頂點的座標資訊
    // ================================================================
    cout << "\n========================================" << endl;
    cout << "以下是多邊形的所有頂點座標:" << endl;
    cout << "========================================" << endl;

    for (int i = 0; i < n; i++)
    {
        // 使用 setw 和 setprecision 讓輸出更整齊
        cout << "頂點 " << setw(2) << (i + 1) << ": ("
             << setprecision(2) << fixed << x[i] << ", "
             << y[i] << ", "
             << z[i] << ")" << endl;
    }

    cout << "========================================" << endl;

    return 0;  // 程式正常結束
}
commented Mar 29 by Peter Minarik (101,360 points)
You check if n <= 0, but what about n = 1 or n = 2? Those are not valid polygons.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...