Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you must use <code>scanf</code> to accept the input, I think you start with something a bit like the following. </p> <pre><code>int array[MAX]; int i, n; scanf("%d", &amp;n); for (i = 0; i &lt; n &amp;&amp; !feof(stdin); i++) { scanf("%d", &amp;array[i]); } </code></pre> <p>This will handle (more or less) the free-format input problem since <code>scanf</code> will automatically skip leading whitespace when matching a <code>%d</code> format.</p> <p>The key observation for many of the rest of your concerns is that <code>scanf</code> tells you how many format codes it parsed successfully. So,</p> <pre><code>int matches = scanf("%d", &amp;array[i]); if (matches == 0) { /* no integer in the input stream */ } </code></pre> <p>I think this handles directly concerns (3) and (4) </p> <p>By itself, this doesn't quite <strong>handle the case of the input<code>12a3</code></strong>. The first time through the loop, <code>scanf</code> would parse '12<code>as an integer 12, leaving the remaining</code>a3` for the next loop. You would get an error the next time round, though. Is that good enough for your professor's purposes?</p> <p>For <strong>integers larger than maxint</strong>, eg, "999999.......999", I'm not sure what you can do with straight <code>scanf</code>.</p> <p>For <strong>inputs larger than the input array</strong>, this isn't a <code>scanf</code> problem <em>per se</em>. You just need to count how many integers you've parsed so far.</p> <p>If you're allowed to use <code>sscanf</code> to decode strings after they've been extracted from the input stream by something like <code>scanf("%s")</code> you could also do something like this:</p> <pre><code>while (...) { scanf("%s", buf); /* use strtol or sscanf if you really have to */ } </code></pre> <p>This works for any sequence of white-space separated words, and lets you separate scanning the input for words, and then seeing if those words look like numbers or not. And, if you have to, you can use <code>scanf</code> variants for each part.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload