unf0rged
unf0rged

Reputation: 19

c scaning for 2D arrays

I am quite new to c language and have run into a problem that I think has some relations with pointers(could be wrong).The first input is obviously the number of inputs which (currently) also determines the width and highth of an array. Next few inputs should read my own coordinates for the array and value for it. on the last bit I am trying to print out the array. The outpus is very very wrong. Any tips on where I did wrong The scaning part or printing or maybe both?

(If st_objects was 5 my max input for x_cord and y_cord was 4) I am only trying to change a few values to something other than 0.First i need to fill the arrays with 0 values?

Something like:

0 0 0 2 0

0 2 3 0 0

0 0 0 0 2

0 0 0 1 2

0 0 2 0 3

Ps: would it be better to use getchar function for inputs?

My code is:

 #include <stdio.h>

int main(){
  int st_objects;
  scanf("%d",&st_objects);

  int x_cord;
  int y_cord;
  int array[st_objects][st_objects];

  for(int i = 0; i < st_objects; i++){
    scanf("%d",&x_cord);
    scanf("%d",&y_cord);
    scanf("%d",&array[x_cord][y_cord]);

  }
  for(int i = 0; i < st_objects; i++){
    for(int j = 0; i < st_objects; j++){
      printf("%d",array[i][j]);
      }
     printf("\n");
   }

  return 0;
 }

Upvotes: 0

Views: 53

Answers (3)

VH97
VH97

Reputation: 13

There are 2 mistakes in your code.. 1)In the first for loop i must be less than total number of elements in the multi-dimensional array i.e i<(st_objects*st_objects). 2)Minor mistake in for loop..

    for(int j=0;j<st_objects;j++)

This is how your program can be modified as(it works fine for 2x2):

    #include <stdio.h>
    int main(){
    int st_objects;
    scanf("%d",&st_objects);
    int x_cord;
    int y_cord;
    int array[st_objects][st_objects];
    for(int i = 0; i < (st_objects*st_objects); i++){
    printf("Enter x,y coordinate:\n");
    scanf("%d",&x_cord);
    scanf("%d",&y_cord);
    if(x_cord<st_objects&&y_cord<st_objects)
    {printf("Enter value at x,y:\n");
     scanf("%d",&array[x_cord][y_cord]);
    }else printf("\nWrong coordinate\n");
    }
    for(int i=0;i<st_objects;i++)
    {
     for(int j=0;j<st_objects;j++)
     {
      printf("%d\t",array[i][j]);
     }
     printf("\n");
    }

    return 0;
    }

I hope this helps :)

Upvotes: 0

user6398437
user6398437

Reputation:

I think you do not fully understand the 2-D arrays and their elements' positions.

Another thing, You may go out of your matrix boundaries, so try to make a check on x, and y positions.

#include <stdio.h>

int main(){
              int st_objects;
              printf("Square Matrix length \n");

              scanf("%d",&st_objects);

              int x_cord;
              int y_cord;
              int array[st_objects][st_objects];

              for(int i = 0; i < st_objects*st_objects; i++){
              printf("x-position  \n");

             scanf("%d",&x_cord);
             printf("y-position   \n");

            scanf("%d",&y_cord);
            scanf("%d",&array[y_cord][x_cord]);

                                     }

      for(int i = 0; i < st_objects; i++){
           for(int j = 0; j < st_objects; j++){
                                printf("%d ",array[i][j]);
                                                   }
                 printf("\n");
                                }

          return 0;
       }

Upvotes: 0

Mirza Younus
Mirza Younus

Reputation: 66

Your scanning loop only executes until st_object times (in this case 5). So you can only take 5 inputs. But if you see, the array contains 5*5=25 elements. So that's one place of going wrong.

Next, there are better ways of scanning the array elements, like this

for(int i = 0; i < st_objects; i++)
   for(int j = 0; j < st_objects; j++)
         scanf("%d",&array[i][j]);

Upvotes: 1

Related Questions