2

I am quite newbie in programming and I want to print out a specific input format the exact way as shown below.

5 5
1
2 2 0 1

5 5
1
2 2 0 1

The top part is the input parameters and the below part which is the same as well is the output. How do I format my scanf and printf to achieve this format? This is actually a 2D array problem but I believe for this part I would not need a 2D array yet? As for stage 1, I am just suppose to print out this format of inputs.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Morde
  • 25
  • 1
  • 5

1 Answers1

5

Simple answer is to not use scanf but to use fgets instead to read the whole lines.

If you then need to use the separate values in the line, then you can use sscanf or strtok to get them.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Hi, thanks for the comment but I didn't learn all those stuff you mentioned. If i were constraint to just scanf, how do i do it? – Morde Oct 14 '15 at 08:06
  • 1
    @Morde `scanf` is not really designed to do it, but you could perhaps use the `"%[^"` format (see e.g. [this `scanf` and family reference](http://en.cppreference.com/w/c/io/fscanf)). – Some programmer dude Oct 14 '15 at 08:15
  • 1
    @Morde, if you were really constrained to just `scanf`, my answer gives one (pretty ugly) method. But, to be honest, if you were constrained that way, I'd be finding out how to get *unconstrained!* – paxdiablo Oct 14 '15 at 08:24