Robert
Robert

Reputation: 133

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

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

Upvotes: 7

Views: 17290

Answers (8)

paxdiablo
paxdiablo

Reputation: 881323

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.

Upvotes: 19

Robert
Robert

Reputation: 133

#include <stdio.h>

main ()
{
    int num1, num2, num3, num4, num5, num6, i;
    int smallestnumber=0;
    int sum=0;
    int numbers[50];
    int count;
    num1 = 0;
    num2 = 0;
    num3 = 0;
    num4 = 0;
    num5 = 0;
    num6 = 0;

    printf("How many numbers will be entered (max 50)? ");
    scanf("%d", &count);

    printf("\nEnter %d numbers then press enter after each entry:  \n", count);

    for (i=0; i < count; i++) {
        printf("%2d> ", i+1);
        scanf("%d", &numbers[i]);
        sum +=  numbers[i];
    }

    smallestnumber = numbers[0];
    for (i=0; i < count; i++) {
        if ( numbers[i] < smallestnumber)
        {
            smallestnumber = numbers[i];
        }
    }

    printf("the sum of the numbers is: %d\n", sum);
    printf("The smallest number is: %d", smallestnumber);
}

Upvotes: 1

Jonas K&#246;lker
Jonas K&#246;lker

Reputation: 7837

wasn[']t looking for you guys to do the work

Cool. People tend to take offense when you dump the problem text at them and the problem text is phrased in an imperative form ("do this! write that! etc.").

You may want to say something like "I'm stuck with a homework problem. Here's the problem: write a [...]. I don't understand why [...]."

Upvotes: 1

Aximili
Aximili

Reputation: 29464

First you read the number of values (ie. 5), then create an array of int of 5 elements, read the rest of the input, split them and put them in the array (after converting them to integers).

Then do a loop on the array to get the sum of to find the smallest value.

Hope that helps

Upvotes: 1

Adam Davis
Adam Davis

Reputation: 93565

Since the list of integers is variable, I'd be tempted to use strtok to split the string up into individual strings (separate by space) and then atoi to convert each number and sum or find minimum on the fly.

-Adam

Upvotes: 1

florent
florent

Reputation: 801

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...)

Upvotes: 11

Adam Hawes
Adam Hawes

Reputation: 5449

Jeebus, I'm not doing your homework for you, but...

Have you stopped to scratch this out on paper and work out how it should work? Write some pseudo-code and then transcribe to real code. I'd have thought:

  • Read integer
  • Loop that many times ** Read more integers ** Add ** Find Smallest

IF you're in C look at INT_MAX - that will help out finding the smallest integer.

Upvotes: 3

No. 5 is the number of integers you have to read into the list.

Upvotes: 4

Related Questions