-5

I have to read varying no. of input that is reading two integer in one case, 4 integer in another case, etc.

Below is the type of input I am trying to achieve. This example has 6 rows.

Sample Input

1 1 1 2
1 1 2 5
1 2 3 4
2 3
2 4
1 5 5 4

My Code

#include <stdio.h>

typedef long long int LLI;

int main(void) 
{


 LLI n,q,i,j,w,x,y,z,t;
 scanf("%lld %lld",&n,&q);
 LLI d[n],s[n];
 for(i=0;i<q;i++)
 {  
      // I don't know how to do this? The code below is what I have tried:
      scanf("%lld %lld %lld %lld",&w,&x,&y,&z); 
      printf("%lld %lld %lld %lld \n",w,x,y,z);
 }  
 return 0;
}

Sample Input

5 14

1 1 1 2

1 1 2 5

1 2 3 4

1 2 4 7

2 3

2 4

1 5 5 4

2 5

2 6

1 7 5 8

2 7

2 8

2 9

2 10
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Nitin Jain
  • 27
  • 2
  • 5

3 Answers3

2

You use fgets() (or getline() if your code runs only on POSIXy systems like Linux, BSDs, and Mac OS X) to read each line. The first line will contain the number of lines that contain data.

To process each line (that has a varying number of values), you can use sscanf() with %n to determine the number of characters consumed, strtol(), strtoul(), or strtod(). In all cases, you try to convert one value, keeping track of how much a successful conversion consumes of the original string, advancing the start of the string for the next conversion accordingly. Repeat until the conversion fails. (You can then check if the conversion failed due to end of string, or whether the line contained non-value data, i.e. garbage or a comment.)

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Nominal Animal
  • 38,216
  • 5
  • 59
  • 86
0

Using int array you can read multiple input. Reference link of stack overflow How can I read a varying amount of integer input in C?

Sample example :

#include <stdio.h>

int main()
{
    int ele,num,i;
    printf("Enter the elements...\n");
    scanf("%d",&ele);
    int arr[ele];
    for (i=0; i<ele;i++)
    {
            scanf("%d",&num);
            arr[i] = num;
    }
}
Community
  • 1
  • 1
msc
  • 33,420
  • 29
  • 119
  • 214
0

At first you can dynamically allocate an array of pointers to int.

For example

size_t nRows = 0;

scanf( "%zu", &nRows );

int **a = malloc( nRows * sizeof( int * ) );

Then you can for each "row" in the array to allocate one pointer that will point to an object that will contain the number of elements in the row

for ( size_t i = 0; i < nRows; i++ )
{
    a[i] = calloc( 1, sizeof( int ));
} 

And at last when you will read numbers in a row you need to use realloc and increase the first element.

That is each row will have this struture

number_of_elements_in the_row, number1, number2, ...

Another approach is to use a dynamically allocated array of structures with a flexible array. for example

struct row { size_t n; int values[]; };
size_t nRows = 0;

scanf( "%zu", &nRows );

row *a = malloc( nRows * sizeof( row ) );
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335