tags:

views:

91

answers:

4

How can i write the code to identify the smallest integer i entered and how many times it appeared in the list i key in? Can somebody please help?

#include<stdio.h>
#define constant-999

int main()
{
 int num, count;

 printf("Enter a list of integers (-999 to stop) : ");
 while(scanf("%d", &num) != -999)
A: 

Expand the while() loop to monitor this:

#define _WIN32_WINNT 0x0400
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <stdio.h>
#include <conio.h>

void main()
{
    int num = 0;
    int smallest = 12345;
    int smallest_count = 0;
    printf("Enter a list of integers (-999 to stop) : ");
    while(scanf("%d", &num) != -999)
    {
        if(num < smallest)
        {
            smallest = num;
            smallest_count = 1;
        }
        else if(num == smallest)
        {
            smallest_count++;
        }
    }
}
Poni
When supplying programs, please supply correct programs. The `main` function returns an `int`. Always.
Thomas Matthews
-1 for changing a correct thing to a wrong thing and including conio.h and windows.h
IVlad
A "thanks" would be enough. Besides, you're welcome to optimize.
Poni
@IVlad: It's a template VC project, and the essence of the solution is what important, obviously. Do what you want.
Poni
Please don't provide "complete solutions" for homework questions - he will learn a lot more if he gets the thing of his own. Learning means doing, doing it wrong in the first place, and then improve.
InsertNickHere
@InsertNickHere: Agree yet not completely. Maybe he needs to learn, besides syntax, a way to think. It's funny that I'm the only one who really gave an answer, and many other "experts" are popping with very irrelevant comments.
Poni
@Poni Experts...I feel like the "real" SO experts did not post here. :)
InsertNickHere
One last question, what should i type in for scanf("%d" ...)when i'm asking the user to key in a list of integers?was thinking how many %d shall i put in.@Poni thanks a lot for the solution.
A safe method is to use `fgets` to read in the input and `sscanf` to process the input. See http://stackoverflow.com/questions/2977553/if-one-complains-about-gets-why-not-do-the-same-with-scanfs
Thomas Matthews
@Poni - You didn't help anyone, so I don't see why you expect thanks. Your answer is wrong and won't even compile on a good compiler, so I don't see how you "really gave an answer". Not even bothering to correct it after two people told you it's wrong makes the -1 very well deserved.
IVlad
@IVlad: You know there's something called "proof of concept". I think the original question poster (and others to come) can appriciate that. Anyway you're most welcome to post your own cross-platform, compiler-correct, educative answer.
Poni
A: 

I agree with Steven's comment but as a hint, since you need to count occurrences you'll need to iterate through the entire thing anyway.

Luis
+3  A: 

With a text editor.

Notepad++ is quite nice, but really anything will do.

Caleb Thompson
I was going to recommend `copy con main.c`, but I guess it's ok if they use an editor.
Steven Sudit
What about... [ex](http://en.wikipedia.org/wiki/Ex_(text_editor)) ?
w3d
@w3d: Let's not spoil them.
Steven Sudit
A: 

A simple solution is to declare an array of numeric counts and using the number read as the index. You may have to offset the index if you deal with negative numbers:

unsigned int number_counts[1000] = {0};

//...

number_counts[num]++;

// or 
number_counts[num + 500]++;

If you are using a linked list, add a count field and increment that.

Thomas Matthews