tags:

views:

857

answers:

8

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

+4  A: 

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

Jason Punyon
+3  A: 

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.

Adam Hawes
+11  A: 

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

florent
+1  A: 

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 Davis
I don't think the integers are going to be presented in a single string. I think they will be sent to the input one at a time, so that each scanf call is going to return one integer.
nezroy
scanf("%d") handles multiple values on a single line just fine, IIRC (I may be misremembering cin :-).
paxdiablo
@Pax: yes it does. For scanf, whitespace is whitespace. Doesn't matter if it's a space or a newline.
Martinho Fernandes
+1  A: 

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

Just out of interest, why an array? You can sum the numbers and keep the smallest just by processing one at a time.
paxdiablo
+1  A: 

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 [...]."

Jonas Kölker
+17  A: 

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.

paxdiablo
Thanks Pax, plenty here to chew on for a bit. Cheers to your approach, I wish my instructor had the same value on knowledge. No worries on me competing with anyone, Im a graphics multimedia guy back in school trying to earn my degree. Believe it or not, I want to teach H.S.Not programming of course.
Robert
@Pax, +1 for not directly giving him working code.
Simucal
Better than a +1 !!!!, I posted the code that I came up with. I think I may have learned something too.(darn...j/k) Anyway I truly appreciate the help and guidence and I promise to ask the right questions and do my own work.
Robert
+1 for the approach.
Martinho Fernandes
+1  A: 
#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);
}
Robert
PAX, what do you think of the programming? Thanks for the help. I think the numbers dont matter, it is for the user to choose right? The light bulb went off after your post. Love to get a reply and to thank you for your help!!!!
Robert
You can edit your post and put 4 spaces in front of the lines with code and it will format it for you.
lillq
Good effort! Points, a few at a time. (1) num1 through num6 aren't used for anything, I suspect they're a hangover from your earlier version. Get rid of them. (2) You may want to ensure count is set to 1 thru 50 before using it (print error and exit if not)...
paxdiablo
(3) You don't need an array but that's an implementation decision. I wouldn't but there's nothing wrong with doing so, especially since you've limited count.
paxdiablo
(4) Your last loop can start at 1 rather than 0 since you've already loaded up numbers[0].
paxdiablo
Don't be discouraged that I found 4 points. NONE of them stops your code from working (presumably you already know this since you compiled and ran it, yes?). They're just the difference between a new learner and a jaded cynical old mongrel that's been in the industry too long :-).
paxdiablo
I'll modify your code and put it in my answer as an example of how I'd write it.
paxdiablo
And I forgot to mention the gotcha - if they enter 0 or a negative number for count, all sorts of "interesting" things happen :-).
paxdiablo
I really wish I could express my gratitude to everyone. I have learned more today and with more support than I have in my $1500 class. I guess I cant expect to teach myself much in 5 weeks.Thank You
Robert
$1500? Where's my cut of that moola? :-)
paxdiablo
If you could give me a degree, I'd quit immediately. Besides I'd love to visit and golf in your area.
Robert