Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First thing, the 5 is not considered part of the list, it's the count for the list. Hence it shouldn't be included in the calculations.</p> <p>Since this is homework, here's the pseudo-code. Your job is to understand the pseudo-code first (run it through your head with sample inputs) then turn this into C code and try to get it compiling and running successfully (with those same sample inputs).</p> <p>I would suggest the sample input of "2 7 3" (two items, those being 7 and 3) as a good start point since it's small and the sum will be 10, smallest 3.</p> <p>If you've tried to do that for more than a day, then post your code into this question as an edit and we'll see what we can do to help you out.</p> <pre><code>get a number into quantity set sum to zero loop varying index from 1 to quantity get a number into value add value to sum if index is 1 set smallest to value else if value is less than smallest set smallest to value endif endif endloop output "The sum of the sequence of integers is: ", sum output "The smallest of the integers entered is: ", smallest </code></pre> <p>Stack Overflow seems to be divided into three camps, those that will just give you the code, those that will tell you to push off and do your own homework and those, like me, who would rather see you educated - by the time you hit the workforce, I hope to be retired so you won't be competing with <em>me</em> :-).</p> <p>And before anyone picks holes in my algorithm, this is for <em>education</em>. I've left at least one gotcha in it to help train the guy - there may be others and I <em>will</em> claim I put them there intentionally to test him :-).</p> <hr> <p><em>Update:</em></p> <p>Robert, after your (very good) attempt which I've already commented on, this is how I'd modify your code to do the task (hand yours in of course, not mine). You can hopefully see how my comments modify the code to reach this solution:</p> <pre><code>#include &lt;stdio.h&gt; int main (int argCount, char *argVal[]) { int i; // General purpose counter. int smallNum; // Holds the smallest number. int numSum; // Holds the sum of all numbers. int currentNum; // Holds the current number. int numCount; // Holds the count of numbers. // Get count of numbers and make sure it's in range 1 through 50. printf ("How many numbers will be entered (max 50)? "); scanf ("%d", &amp;numCount); if ((numCount &lt; 1) || (numCount &gt; 50)) { printf ("Invalid count of %d.\n", numCount); return 1; } printf("\nEnter %d numbers then press enter after each entry:\n", numCount); // Set initial sum to zero, numbers will be added to this. numSum = 0; // Loop, getting and processing all numbers. for (i = 0; i &lt; numCount; i++) { // Get the number. printf("%2d&gt; ", i+1); scanf("%d", &amp;currentNum); // Add the number to sum. numSum += currentNum; // First number entered is always lowest. if (i == 0) { smallNum = currentNum; } else { // Replace if current is smaller. if (currentNum &lt; smallNum) { smallNum = currentNum; } } } // Output results. printf ("The sum of the numbers is: %d\n", numSum); printf ("The smallest number is: %d\n", smallNum); return 0; } </code></pre> <p>And here is the output from your sample data:</p> <pre><code>pax&gt; ./qq How many numbers will be entered (max 50)? 5 Enter 5 numbers then press enter after each entry: 1&gt; 100 2&gt; 350 3&gt; 400 4&gt; 550 5&gt; 678 The sum of the numbers is: 2078 The smallest number is: 100 pax&gt; ./qq How many numbers will be entered (max 50)? 5 Enter 5 numbers then press enter after each entry: 1&gt; 40 2&gt; 67 3&gt; 9 4&gt; 13 5&gt; 98 The sum of the numbers is: 227 The smallest number is: 9 pax&gt; ./qq How many numbers will be entered (max 50)? 0 Invalid count of 0. [fury]$ ./qq How many numbers will be entered (max 50)? 51 Invalid count of 51. </code></pre> <p>By the way, make sure you always add comments to your code. Educators love that sort of stuff. So do developers that have to try to understand your code 10 years into the future.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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