473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

c program for frequency of occurences

dear all
i want and as fast as possible a working C program for the following problem.
i have an array values[ ] consisting of 'n' float values say ranging from 0 - 18. i want to get the frequencies of the values in class intervals of 0 - 1.99, 2 - 3.99, 4 - 5.99 etc. My attempts do not give me the proper results. especially for the first class interval 0 - 1.99. HELP !!!


My program
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #define maxrl 10
  3. #define bins 10
  4.  
  5. main()
  6. {
  7.   FILE *fp1,*fp2;
  8.     float value[maxrl];
  9.     int i;
  10.     int group[bins];
  11.  
  12.     for(i=0;i<bins;i++)
  13.     group[bins] = 0;
  14.     fp1 = fopen("RL.dat","r");
  15.     fp2 = fopen("RL_freq.dat","w");
  16. for(i=0;i<maxrl;i++){
  17.   fscanf(fp1,"%f",&value[i]);
  18.   ++group[(int)(value[i]/2)];
  19.  
  20. }
  21. fprintf(fp2,"\n");
  22. fprintf(fp2,"no. of bins\tbins\t frequency\n");
  23.  
  24. for(i=0;i<bins;i++)  {
  25.  
  26.     fprintf(fp2,"%d\t\t %2d\t\t%d \n",i+1,2*i,group[i]);
  27. }
  28. fclose(fp1);
  29. fclose(fp2);
  30. }
Apr 10 '07 #1
2 2428
sicarie
4,677 Expert Mod 4TB
Please use code tags around your code, they help greatly with readability.

Ok, you have a few issues here - you have a for loop with no { } statement, an undefined variable (bins), and it doesn't look like you have started the part where you count the ranges. If this is for a class assignment, I would recommend sitting down with your teacher for a few hours - there's some fundamental stuff here that needs to be fixed, and you need help with the algorithm/logic of the program.
Apr 10 '07 #2
dear all
i want and as fast as possible a working C program for the following problem.
i have an array values[ ] consisting of 'n' float values say ranging from 0 - 18. i want to get the frequencies of the values in class intervals of 0 - 1.99, 2 - 3.99, 4 - 5.99 etc. My attempts do not give me the proper results. especially for the first class interval 0 - 1.99. HELP !!!


My program
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #define maxrl 10
  3. #define bins 10
  4.  
  5. main()
  6. {
  7.   FILE *fp1,*fp2;
  8.     float value[maxrl];
  9.     int i;
  10.     int group[bins];
  11.  
  12.     for(i=0;i<bins;i++)
  13.     group[bins] = 0;
  14.     fp1 = fopen("RL.dat","r");
  15.     fp2 = fopen("RL_freq.dat","w");
  16. for(i=0;i<maxrl;i++){
  17.   fscanf(fp1,"%f",&value[i]);
  18.   ++group[(int)(value[i]/2)];
  19.  
  20. }
  21. fprintf(fp2,"\n");
  22. fprintf(fp2,"no. of bins\tbins\t frequency\n");
  23.  
  24. for(i=0;i<bins;i++)  {
  25.  
  26.     fprintf(fp2,"%d\t\t %2d\t\t%d \n",i+1,2*i,group[i]);
  27. }
  28. fclose(fp1);
  29. fclose(fp2);
  30. }
Among other things, you're not zeroing your group bins because you're not using 'i' in your 'for' loop. So your numbers will be completely wacky.

You really should also check the returns from your fopens, etc... and be very careful with floats generally.
Apr 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: malv | last post by:
Simple case: In this list, how to find all occurences of intervals of n adjacent indexes having at least one list-member with a value between given limits. Visualizing the list as a...
2
by: RadiationX | last post by:
How does this program work? // fig06_07.c -- COP2220 Example -- 040209 (sjd) // Student poll program #include <stdio.h> #include <stdlib.h> #define RESPONSE_SIZE 40 //...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
0
by: bsn525 | last post by:
Consider the marks scored by students of the programming module. Allow the user to enter the number of students. The user shall then enter the marks for each student. Thus if there are 10 students,...
18
by: Neehar | last post by:
Hello For one of the interviews I took recently, I was given an offline programming quiz. In 30 minutes I had to write code in C++ to counts the number of times each unique word appears in a...
8
by: Andrew Savige | last post by:
I'm learning Python by reading David Beazley's "Python Essential Reference" book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not...
5
by: Larry | last post by:
Dear all, I'm new to Python. I have a file (an image file actually) that I need to read pixel by pixel. It's an 8-bit integer type. I need to get the statistics like mean, standard deviation,...
1
by: Sejoro | last post by:
I fixed the operand problem (thanks) and now a new one has risen up. I have to compile it with a makefile which I have made and I am getting more strange errors I don't know how to fix. Well,...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.