Solved

Please help with writing this simple prog.

Posted on 1998-11-05
3
163 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 93
Using unmanaged DLL from managed application 14 294
C language IDE – Compilers installation 14 74
smtp c source code 7 85
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

790 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