Solved

Please help with writing this simple prog.

Posted on 1998-11-05
3
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

695 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