-5

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)
High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
user391967
  • 3
  • 1
  • 1
  • 4
  • 9
    This is your second homework problem in the last few minutes. Maybe you should spend some time on your own trying to work these out before asking for help. – Steven Sudit Jul 14 '10 at 19:42
  • I guess i should. I'll come back again tomorrow. – user391967 Jul 14 '10 at 19:46
  • Nice catch and good call Steven. – Darcy Jul 14 '10 at 19:48
  • Dang, I wish the internet existed when I was doing homework! (_sigh_ ... _ponders for a moment thinking back to all those hours spent in front of blinking dumb terminal_) – MrWhite Jul 14 '10 at 19:55
  • @Dang yeah, I remember the days coding without internet. I still love my first amiga basic book. :) – InsertNickHere Jul 14 '10 at 19:59
  • @Darcy: It doesn't help that, despite having the correct answer to the last question handed him on a platter, he ended up using an incorrect answer. – Steven Sudit Jul 14 '10 at 20:00

3 Answers3

3

With a text editor.

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

Caleb Hearth
  • 3,315
  • 5
  • 30
  • 44
0

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
  • 1,210
  • 2
  • 11
  • 24
0

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
  • 56,849
  • 17
  • 98
  • 154