2014-02-14 46 views
0

首先,我想開始說我是一名學生,並且是編碼方面的新手。所以,我提前道歉,因爲我在這個部門的知識普遍缺乏......在變量2d數組中存儲整數

我想編寫的代碼將讀取整數並將它們存儲在多維數組中。整數是圖形上的點,y值。此外,我希望數組大小是用戶將輸入的變量(如用戶將輸入的點數 - points [numPoints] [2])。這是我到目前爲止:

#include <stdio.h> 
int main() 
{ 

int numPoints=0, row, colm; 
int points[numPoints][2]; 





printf("This program will read in a number of points, and calculate the distance between them.\n"); 
printf("It will also calculate the total distance between the first point, and the last point.\n"); 
printf("\nHow many points will you be entering?\n"); 
scanf(" %d", &numPoints); 
printf("Please enter each point individually.\nExample(x & y values shown):\nx y\n3 5\n-2 10\netc...\nPlease enter your points now (press 'Enter' after each point):\n"); 
for (row = 0; row<numPoints; row++) 
{ 
    for (colm = 0; colm<2; colm++) 
    { 
      scanf("%d", &points[row][colm]); 
    } 
} 
printf("These are the points recorded:\n"); 
for (row = 0, colm=0; row<numPoints; row++) 
{ 
    printf("(%d,%d)\n", points[row][colm],points[row][colm+1]); 
} 

return 0; 
} 

我已經運行了幾次現在,這是我得到的輸出。它要麼沒有正確存儲數字,要麼沒有正確打印。

This program will read in a number of points, and calculate the distance between them. 
It will also calculate the total distance between the first point, and the last point. 

How many points will you be entering? 
4 
Please enter each point individually. 
Example(x & y values shown): 
x y 
3 5 
-2 10 
etc... 
Please enter your points now (press 'Enter' after each point): 
1 2 
3 4 
5 6 
7 8 
These are the points recorded: 
(1,2) 
(3,4) 
(6,31) 
(1,8) 
Program ended with exit code: 0 

任何人都可以告訴我我做錯了什麼?

謝謝

+0

你正在聲明一個空數組,看看'main()' - >'points [0] [2]'後面的兩行。您應該在用戶輸入正確的維度後聲明變量,或者爲'malloc()'聲明'** ptr'指針。嘗試放置這行'int points [numPoints] [2];'緊接在第一個'scanf'之後# – yeyo

+0

http://stackoverflow.com/questions/9722632/what-happens-if-i-define-a-0-size -array-in-cc – Jeyaram

+0

你的代碼在我的機器上工作正常......,我在Windows XP和'gcc'上使用'MingW' 4.7.2;雖然我建議使用結構來存儲點,然後將它們添加到鏈接列表或隊列中。 – shengy

回答

0

我希望這個代碼將幫助您:

#include <stdio.h> 
    int main() 
{ 

    int numPoints =0,row, colm; 
    int points[10][2]; 

    printf("This program will read in a number of points, and calculate the distance between them.\n"); 
    printf("It will also calculate the total distance between the first point, and the last point.\n"); 
    printf("\nHow many points will you be entering?\n"); 
    scanf(" %d", &numPoints); 
    printf("Please enter each point individually.\nExample(x & y values shown):\nx y\n3 5\n-2 10\netc...\nPlease enter your points now (press 'Enter' after each point):\n"); 

for (row = 0; row < numPoints; row++) 
{ 
    for (colm = 0; colm<2; colm++) 
    { 
      scanf("%d", &points[row][colm]); 
    } 
} 
printf("These are the points recorded:\n"); 
for (row = 0, colm=0; row<numPoints; row++) 
{ 
    printf("(%d,%d)\n", points[row][colm],points[row][colm+1]); 
} 

return 0; 
} 
0

移動

int points[numPoints][2]; 

scanf(" %d", &numPoints); 

E.g

scanf(" %d", &numPoints); 
int points[numPoints][2];