How do I read a character array and an integer array parallely in C ?

Revision en1, by ghoshsai5000, 2017-03-29 06:26:49

I was working on the problem Anton and Chess, and I have noticed that the entire output never gets read. Here is the code which deals with input — The input loop doesn't run as many times as it's supposed to and always takes one input less. This problem disappears if the character input line is removed.

#include <stdio.h>
#include <stdlib.h>

void get_piece_coordinates(char *, long *, long *, unsigned int);

int main()
{

        unsigned int no_of_black_pieces;
        long king_x_coordinate, king_y_coordinate;

         scanf("%u", &no_of_black_pieces);
         scanf("%lu %lu",&king_x_coordinate, &king_y_coordinate);

         long *x_coordinate = malloc(no_of_black_pieces*sizeof(long));
        long *y_coordinate= malloc(no_of_black_pieces*sizeof(long));
       char *piece= malloc(no_of_black_pieces *sizeof(char));

        get_piece_coordinates(piece, x_coordinate,y_coordinate,no_of_black_pieces);

       free(piece);
       free(x_coordinate);
       free(y_coordinate);
       return 0;
  }

  void get_piece_coordinates(char *piece, long *x_coordinate, long *y_coordinate, unsigned int no_of_black_pieces)
 {
      unsigned int i;

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

            scanf("%c",(piece + i));
             scanf("%lu %lu", (x_coordinate + i), (y_coordinate+ i));
       }
  }
Tags input

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English ghoshsai5000 2017-03-29 06:26:49 1570 Initial revision (published)