Solved

Please help with writing this simple prog.

Posted on 1998-11-05
3
162 Views
Last Modified: 2010-04-15
I have a file which contain lots of numbers. I want to find the frequancy of each range.Can somebody help me with the strategy to write this program.
-the file contain numbers say 1-1000 so the min and max are known in advance
-the user input the width eg. 10 so there should be 1000/10 =100 number of classes (width=user input variable)
the program should search and find the frequency between numbers b/w 0-199, 200-299, 300-399 until 1000. So the result should be
class       Lower limit    upper limit  frequency      Prop
0           0.000           199.000      120          0.0987

thanks alot for you help
0
Comment
Question by:Paris
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 1254060
I would approach it with arrays. The overall range of the numbers is 1 - 1000. Get the width from the user. If he enters 10 there will be 100 classes (which would be 0 - 9, 10 - 19 etc), if he enters 100 there will be 10 (0 - 99, 100 - 199 etc.). In general then the number of classes will be 1000/width. (Or you could ask them to enter the number of classes they want, and deduce the "width" instead).

Once you have the number of classes allocate an array of that length using malloc or calloc:

int *class;

class=calloc(classnum,sizeof(int));

Then read through the file and increment the appropriate class for each number:

/* num is number from file */
++class[num/classnum];

When you have processed all the number the frequency will be contained in each array element and each class can be conceptually printed as:

/* i is the class number */

class i:
Lower limit  i*width
Upper limit  (i+1)*width-1
frequency class[i]

I'm not sure what the Prop  represents.

0
 
LVL 16

Expert Comment

by:imladris
ID: 1254061
P.S. note that if the range is in fact 1 - 1000 (as opposed to 0 - 999) you will have to account for 1000 appearing. As it stands ++class[1000/width] would address an element one past the end of the array. You could check, and force it into the last element instead, or allocate one extra element, or subtract 1 from all the numbers (thus converting it from 1 - 1000 to 0 - 999). It will be easy to handle, as long as you're aware of the issue.

0
 

Author Comment

by:Paris
ID: 1254062
I 'll try it and get back to you. Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 145
libcurl and C++ - Post JSON Data 8 1,295
Problem to ASCII 1 176
How to learn Linux? 10 60
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question