0

First I want to start out saying I am a student, and new to coding. So, I apologize in advance for my general lack of knowledge in this dept....

I am trying to write code that will read in integers and store them in a multidimensional array. The integers are points on a graph, x & y values. Also, I want the array size to be a variable the user will input (as in the number of points the user will be inputting -- points[numPoints][2]). This is what I have so far:

#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;
}

I have run it a few times now, and this is the output I am getting. It is either not storing the numbers correctly, or it is not printing them correctly.

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

Can anyone please tell me what I am doing wrong?

Thank you

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • you are declaring an empty array, look at the two lines after `main()` -> `points[0][2]`. You should, either declare the variable after the user enters the correct dimension or declare a pointer `**ptr` for `malloc()`. Try placing this line `int points[numPoints][2];` right after the first `scanf` – yeyo Feb 14 '14 at 02:19
  • http://stackoverflow.com/questions/9722632/what-happens-if-i-define-a-0-size-array-in-c-c – Jeyaram Feb 14 '14 at 02:19
  • Your code works fine on my machine...., I'm using `MingW` on windows xp and `gcc` 4.7.2; Though I'd recommend using a structure to store points and then add them into a link list, or queue. – shengy Feb 14 '14 at 02:28
  • I am not familiar with **ptr or malloc() . However, putting int points[numPoints][2]; right after the first scanf worked. Thank you! – MeCousi Feb 14 '14 at 02:31
  • I suppose I should have also mentioned that I am doing this on a mac running OS X, with xcode. – MeCousi Feb 14 '14 at 02:33

2 Answers2

0

I hope this code will help you:

  #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;
}
user3148898
  • 135
  • 2
  • 9
0

move

int points[numPoints][2];

to after

scanf(" %d", &numPoints);

E.g

scanf(" %d", &numPoints);
int points[numPoints][2];
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70