Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a program that sums the sequence of integers, as well as the smallest in the sequence

Tags:

c

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

like image 572
Robert Avatar asked Feb 02 '09 23:02

Robert


2 Answers

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.

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

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:

#include <stdio.h>
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", &numCount);
    if ((numCount < 1) || (numCount > 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 < numCount; i++) {

        // Get the number.

        printf("%2d> ", i+1);
        scanf("%d", &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 < 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;
}

And here is the output from your sample data:

pax> ./qq
How many numbers will be entered (max 50)? 5

Enter 5 numbers then press enter after each entry:
 1> 100
 2> 350
 3> 400
 4> 550
 5> 678
The sum of the numbers is: 2078
The smallest number is:    100

pax> ./qq
How many numbers will be entered (max 50)? 5

Enter 5 numbers then press enter after each entry:
 1> 40
 2> 67
 3> 9
 4> 13
 5> 98
The sum of the numbers is: 227
The smallest number is:    9

pax> ./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.

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.

like image 95
paxdiablo Avatar answered Jan 21 '23 15:01

paxdiablo


Read:

Assume that the first integer read with scanf specifies the number of values remaining to be entered

so it's not part of the sequence...

for the rest, it's your homework (and C...)

like image 45
florent Avatar answered Jan 21 '23 15:01

florent