Write a program that sums the sequence of integers as well as the smallest in the sequence. Assume that the first integer read with scanf specifies the number of values remaining to be entered. For example the sequence entered:
Input: 5 100 350 400 550 678
Output: The sum of the sequence of integers is: 2078
Input: 5 40 67 9 13 98
Output: The smallest of the integers entered is: 9
This is a daily problem I am working on but by looking at this, Isnt 5 the smallest integer? I have no idea how to write this program. Appreciate any help
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.
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).
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.
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.
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 me :-).
And before anyone picks holes in my algorithm, this is for education. I’ve left at least one gotcha in it to help train the guy – there may be others and I will claim I put them there intentionally to test him :-).
Update:
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:
And here is the output from your sample data:
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.