C/Call histogram
Expert: Zlatko - 10/12/2010
QuestionQUESTION: heyyyy...
let me tell u abt the whole problem....its a huge CDR file(of which i've only sent 2/3 call records to u).it contains the call records of 24 hours.i have to divide this 24 hours into 12 parts and have to see how many calls are generated every 2 hours.and then in a histogram i have to plot this informtion.
what i have figured out wt my knowldge in teletraffic....is that i only need the first line of every call record....
if thr ws ne way i cd show u the whl program then u wd have knwn the whole thng....
ANSWER: Hi Tana.
Ok, I have a simple program for you to try. I have put many comments into it. You should ask questions about what you don't understand.
Give the program a try and let me know how it works for you. Also let me know what operating system and compiler you are using.
Best regards
Zlatko
#define _CRT_SECURE_NO_WARNINGS /* this line is to stop certain warnings */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
/* text is used for user input and for input from file.
It should be long enough to hold a line of the file.
*/
char text[128];
/* the input file */
FILE* inFile;
/*
The call tallies
calls will be tallied in 2 hour segments
*/
int callTallies[12];
/*
clear call tallies.
*/
memset(callTallies, 0, sizeof(callTallies));
/* Open the file called CDR.txt
The file will have to be in the same folder as the program.
*/
inFile = fopen("CDR.txt", "r");
if (inFile == NULL)
{
printf("Cannot open file\n");
}
else
{
/* Read the file line by line */
while (1)
{
char day[32];
char month[32];
int dayOfMonth;
int hour;
int min;
int sec;
int year;
fgets(text, sizeof(text), inFile);
if(feof(inFile)) break; /* exit the loop if we have read all the lines */
/*
Format of the Date line is
Day Month DayOfMonth HH:MM:SS YYYY
Use only lines that match the format
Use the result of sscanf to check that the line is in the
correct format.
*/
if (sscanf(text, "%s %s %d %d:%d:%d %d",
&day, &month, &dayOfMonth, &hour, &min, &sec, &year) == 7)
{
printf("Found %s %s %d %d:%d:%d %d\n",
day, month, dayOfMonth, hour, min, sec, year);
/* use the hour divided by 2 as an index into
the tallies array.
We are using integer division here so the
fractional part is thrown away.
hour 0 and 1 when divided by 2 gives 0
hour 22 and 23 when divided by 2 gives 11
*/
++callTallies[hour / 2];
}
}
/* print out call tallies */
{
int tallyIndex;
printf("Call Tallies\n");
printf("Hour\t| Count\n");
printf("-------\t+ -----\n");
for(tallyIndex = 0; tallyIndex < 12; ++tallyIndex)
{
int hour = tallyIndex * 2;
printf("%2d - %2d\t| %d\n",
hour, hour+1, callTallies[tallyIndex]);
}
}
/* Close the file when done */
fclose(inFile);
}
}
---------- FOLLOW-UP ----------
QUESTION: hi Zlatko;
I'm doin my program in C.....have some questions??is while(1) an infinite loop???wen u write callTallies[12], does it mean u r defining an array??
y did u put paranthesis before the line int tallyindex??what id sscanf?????shud it be fscanf???can u xplain the last loop??
i tried to run the program but the user screen has gone nuts.....i can't see a thing....it like a rain of integers....wat do i do????
AnswerHello Tana.
The while(1) would normally be an infinite loop, but there is the line
if(feof(inFile)) break; /* exit the loop if we have read all the lines */
which will break out of the loop when the program tries to read past the end of file. It just happens that the end-of-file flag is not set until the program tries to read past the end, so the best pace to check for it is right after the fgets. Using while(1) with a break is the most compact way to write it.
The callTallies[12] is an array of 12 items. The tallyIndex into the array starts at 0 and goes to and includes 11.
At the code:
/* print out call tallies */
{
int tallyIndex;
I put the parentheses "{" in to make a code block. The code block allows me to declare the new variable tallyIndex just before it is used, instead of farther up in the main function. The C program makes you declare all variables before any executable lines but I like to use the C++ style of declaring variables close to where they are used, so I used the parentheses to put borders around a block of functionality. It also makes it easier to convert the code into a function. It is just a style of coding.
sscanf parses out items from memory. fscanf parses out a items from a file. It is better to read in an entire line from a file and then parse it out from memory. I should have thought of sscanf right from the start instead of bothering you with pointers. It really helped me when you told me exactly what your wanted to create and what data you wanted to analyze.
The last loop prints out the tallies for your histogram. It goes from 0 to 11. The hours for each tally are the tallyIndex x 2 and tallyIndex x 2 + 1. Those hour numbers are printed out along with the tally.
Sorry about the screen going nuts. It is because your CDR file is so large. Take out the statement
printf("Found %s %s %d %d:%d:%d %d\n",
day, month, dayOfMonth, hour, min, sec, year);
and the screen will stop. The program should finish with a tally of all the calls for the day in 2 hour segments. From that you can draw your histogram. I don't think you really need the program to draw the histogram. A windows program will be complicated, and a console program (what you have now) will make a sloppy picture.
If you have any more trouble with the program, please send me the CDR file for me to test with. Send it as an attachment to zlatko.c.help@gmail.com
Best regards
Zlatko