No need to apologise, your questions are ok.
First, your program code.
The array usage is incorrect here. The three arrays all need to be allocated with at least 16 elements. The first element, subscript [0] is empty, simply because the input file has no data in that time range. Just because the program may seem to work with an array size of 15, doesn't mean it is correct.
At line 112,
for (num = 1; num <= 15; num++) {
the loop will access array element Te[15], which is the 16th element, therefore the array must be allocated with at least that many elements. The declaration at line 43
double Te[15]= {0};
should be
double Te[16]= {0};
As you pointed out,
Finally when computing the average I have to divide by my total throughput by num -1
because my num is displayed as num = 16 . |
that's because there are indeed 16 values, but the first one
Te[0]
is empty as there is no data.
Other comments. I could have mentioned earlier, your code is inconsistent in the way it uses the control structures. Choose either to use
1 2 3 4 5 6 7 8
|
if (type == 'r')
{
]
else if (type == 's')
{
}
|
or use instead
switch (type)
with corresponding case statements.
Your code does both, which is confusing to read and is over-complicated.
As for the three arrays, only
Te[]
is used after you have completed reading the file, so the other arrays
Sum[16]
and
total[16]
are not needed, they could be replaced by simply
int Sum = 0;
and
double total = 0.0;
, however you will need to remember to reset their values to zero at the end of each block, (somewhere around line 105). As a matter of readability, it would be better to use meaningful names, such as
totalTime
and
totalBytes
, and the same could be said of various other variables such as S and T which don't mean anything to a human reader.
There is no need to specify two different loop conditions at line 119,
for(Range =10,num=1; Range <=150, num <=15; Range+=10,num++){
the comma operator here means that only the expression
num <=15
to the right of the comma will be used, you should omit the
Range <=150,
As for the clarification of what the code is doing, your description sounded a bit muddled.
1 2
|
getline(fin, line);
ss.str(line);
|
Here the getline will read a line of text from the file into the string
line
, then assign that string to be the contents of the stringstream
ss
.
do not I need to close my file at the end |
It's good to ask that question, because all files should be closed when you are finished using them. However the C++ fstream automatically closes the associated file when the fstream object goes out of scope at the end of main() in this case.
if (getline(fin, line))
This checks if an incorrect string was read and clears the error bits so that you can parse new string.
|
Not quite. It attempts to read a line from the input file. If it was successful, the condition will be true so the body of the if statement will be executed. The stringstream
ss
is reset to a clean state by clearing the flags and loading the string
line
into it. Alternatively, at the end of the input file, the getline will fail and the
else
is executed, where I chose to set the fail flag of
ss
as a convenient way of signalling to the while loop at line 54 that there is no more data.
I'm sure I've missed something here but hope this helps.