Subtraction and storing decimal numbers in an array

hey all
I am trying to perform a subtraction operation on two decimal numbers declared as double in a file. The result of each of these two numbers should be stored for later use. Doesnot that require the use of arrays

here's how far I have gone ... could u plz guys guide me through it I am having a really hard time as my subtraction results are incorrect and I don't know what I am doing wrong ?
Thanx in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
    char c;
    string line;
    int numlines = 0, sum = 0, h;
    double b, time = 0.0;
    string m,d,e,f,g,q,i,o,j,k,l,y,n,p,r,z,x,w;
    
ifstream myfile ("Spr.tr");
if(myfile.is_open()) {
while (getline(myfile,line)) {
	numlines++;
	istringstream input(line);
    input >> c >> b >> m >> d >> e >> f >> g >> h >> q >> i >> o >> j>> k;
	
   
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(9);
	cout << "b=" << b << ".\n";
        time = b- time ;
        cout << time <<  ".\n";
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}

s 18.112906208 _1_ MAC --- 1047 tcp 1118 [13a 9 1 800] ------- [1:1 9:0 32 9] [64 0] 0 0.
r 18.121850295 _9_ MAC --- 1047 tcp 1060 [13a 9 1 800] ------- [1:1 9:0 32 9] [64 0] 1 0.
D 18.123812660 _9_ MAC COL 0 RTS 44 [256e 9 1 0] .
D 18.130992027 _9_ MAC COL 0 RTS 44 [256e 9 1 0] .
s 18.205094269 _1_ MAC --- 1048 tcp 1118 [13a 9 1 800] ------- [1:1 9:0 32 9] [65 0] 0 0.
r 18.214038356 _9_ MAC --- 1048 tcp 1060 [13a 9 1 800] ------- [1:1 9:0 32 9] [65 0] 1 0.
s 18.229139602 _1_ MAC --- 1049 tcp 1118 [13a 9 1 800] ------- [1:1 9:0 32 9] [66 0] 0 0.
r 18.238083689 _9_ MAC --- 1049 tcp 1060 [13a 9 1 800] ------- [1:1 9:0 32 9] [66 0] 1 0.

The following is a snap shot of my trace file I am working on
here 's what i need to do
Received time - sent time
T1 = 18.121850295 -18.112906208=
T2 =18.214038356 -18.205094269 =
store each result and then i need to sum them all up
Total time = T1+ T2 + ....

Last edited on
You need to check whether the line is type 's' or 'r' and process accordingly.

If it is an 's', 
    read the number and store it temporarily.

if it is an 'r', 
    read the number, 
    subtract the previously stored s value and 
    add the result to the total.


A switch-case structure might be useful.

As for storing the results in an array, do you need to do any other processing apart from calculating the difference of each s-r pair? If there is more to be done, then an array, or better a vector would be useful. But it depends on the required processing.

Thank u so much for your reply ... well after I get my time difference between my received time and sent time . Taking for example the above trace file
T1 = received time1 - sent time1=
T1 = 18.121850295 - 18.112906208 = T1
T2 =received time2 - sent time 2=
T2 = 18.214038356- 18.205094269 = T2
The same applies to the rest T3 ,T4, T5 , .....

I should get the total of these times that's the extra operation I am performing
Total Time = T1 + T2 + T3 + T3 .....
The total time will be in a decimal value .. the problem is that I don't know the size of the array as I don't know how many T's will I get , the file is long so does that mean I should be using vectors.. plz correct me if I mistaken
thanx in advance
Well, as I see it, the total is just a single number. There's no need for an array. Simply add each value T1 etc. to the total at the point in the program where you first obtain it.
Cudn't thank u enough for your useful hints ...well I have written the code and I have tested it and finally its compiling correctly . Here's my code.... plz if you have any comments on enhancing it or doing it in a better way than mine plz let me know.
And cud u plz clarify if I do have more than a single value what should I do
thank u so much :)

[/code]

#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
char c;
string line;
int numlines = 0, sum = 0, h,ze;
double b, S, T,time = 0.0;
string m,d,e,f,g,q,i,o,j,k,l,y,n,p,r,z,x,w;
ifstream myfile ("T1-9.tr");



if(myfile.is_open()) {
while (getline(myfile,line)) {
numlines++;
istringstream input(line);
input >> c >> b >> m >> d >> e >> f >> g >> h >> q >> i >> o >> j>> k;


cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(9);
//cout << "b=" << b << ".\n";

if (c== 'r')
sum = sum + h;
cout << "sum =" << sum << ".\n";

switch (c)
{
case 's':
T = b;
//cout << "T(ze) =" << T << ".\n";
break;

case 'r' :
S = b- T;
cout << "S(ze)= " << S << ".\n";
time = time + S;
cout << "Total Time =" << time << ".\n";
break;
}

}
cout << "Total number of bytes received = " << sum << "Bytes" <<".\n";
cout << "Total Time =" << time << ".\n";
double Tp = 0.0, Thput = 0.0,Throughput = 0.0;
Tp = sum * 8;
cout << "Bits =" << Tp << ".\n";
Thput = Tp /(time);
cout << "Throughput =" << Thput << "bps" << ".\n";
Throughput = Thput/1000000;
cout << "Throughput =" << Throughput << "Mbps" << ".\n";



myfile.close();
}

else cout << "Unable to open file";


return 0;
}

Last edited on
Your code looks quite similar to what I had in mind. A couple of general comments. Some of the includes don't look correct for C++.
1
2
3
#include <string.h>
#include <math.h>
#include <stdio.h> 

these should be:
1
2
3
#include <string>
#include <cmath>
#include <cstdio> 

The versions ending with .h are either part of C rather than C++ or may in some cases (compiler dependent) be obsolete and non-standard.

In this case you don't actually need an array or a vector. But here's an example of how they might be used, by way of illustration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>

using namespace std;
    
int main()
{
    ifstream fin("T1-9.tr");
    if (!fin)
    {
        cout << "file not open" << endl;
        return 1;
    }

    vector<double> times;
    vector<int>    bytes;
    
    double sendTime;
    double recvTime;
    
    int byte;
    
    string s1, s2, s3, s4, s5;
    string line;
    
    while (getline(fin, line))
    {
        istringstream ss(line);
        char type;
        
        ss >> type;
        
        switch (type)
        {
            case 's':
                ss >> sendTime; 
                break;
                
            case 'r':
                ss >> recvTime >> s1 >> s2 >> s3 >> s4 >> s5 >> byte;
                double diff = recvTime - sendTime;
                times.push_back(diff);
                bytes.push_back(byte);
        }            
    }

    double totalTime = 0;
    int totalBytes   = 0;

    for (unsigned int i=0; i<times.size(); i++)
        totalTime += times[i];

    for (unsigned int i=0; i<bytes.size(); i++)
        totalBytes += bytes[i];
    
    cout << fixed << setprecision(9);      
    cout << "Total Time  = " << totalTime << endl;    
    cout << "Total Bytes = " << totalBytes << endl;    
    
    return 0;
}
Thank u Chervil for your quick reply and the useful information you are providing me with. Well I have a couple of questions regarding your code, but before doing so , I need to ask a question regarding the program I wrote. I want to add a part where I need to read the time every 10 s (For example ) and perform the above calculations. The total time of my simulation is 150s, therefore, I need to split the total time into 10secs and then calculate the throughput and print out the time and the throughput in a separate file and then calculates the throughput for the next 10 secs and do the same procedure ... as if I am taking snap shots every 10s in the file and getting the throughput through the above calculations in order to have a file that will contain time vs throughput for plotting purpose .. does that require the use of a for loop, putting into consideration that my time is a decimal number . here's a link to the trace file I am working on.
http://www.mediafire.com/view/7qab18ooahl0a2r/RN1-9.tr

Regarding Your code
Can u plz clarify the part of the for loop
I really appreciate ur help .. thank u :)

i tried specifying the range using if condition
here's what I have done so far but there seems to be something wrong and the code will be so long , in addition my throughput is looped which something I don't want it to happen as I need to print it out only
10 throughput = according to the throughput calculation
20 throughput = according to the throughput calculation
30 ,etc till I reach the end of my time which is 150s and then I need to add the throughputs up at the end to get the average throughput
Total Throughput = Throughput 1+ Throughput 2 + Throughput 3 + ... / Divided by their number

can u plz help me out with any hint on how to do it in a better way and avoid the looping of the throughput . .. and is the if condition a correct way to specify a specific range
thanx in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>
using namespace std;

int main() {
    char c;
    string line;
    int numlines = 0, sum = 0,num, h,ze;
    double b, S, T,time = 0.0;
    string m,d,e,f,g,q,i,o,j,k,l,y,n,p,r,z,x,w;
    double Tp = 0.0, Thput = 0.0,Throughput = 0.0;
   
ifstream myfile ("RN1-9.tr");
ofstream MYFILE("TvThr.tr");
ofstream MF("Bcheck.tr");
//ifstream myfile ("New1-9.tr");
if(myfile.is_open()) {
while (getline(myfile,line)) {
	numlines++;
	istringstream input(line);
    input >> c >> b >> m >> d >> e >> f >> g >> h >> q >> i >> o >> j>> k;
	
   if (b > 11.0 && b <21.0 ) {
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(9);
	MF << "b=" << b << ".\n";
if (c== 'r')
	sum = sum + h;
//cout << "sum =" << sum << ".\n";
	switch (c)
	{
		case 's':
		T = b;
		break;
		
		case 'r' :
		S = b- T;
		cout << "S(ze)= " << S << ".\n";
		time = time + S;
		cout << "Total Time =" << time << ".\n";
		break;	
	}	
}

cout << "Total number of bytes received = " << sum << "Bytes" <<".\n";
cout << "Total Time =" << time << ".\n";
Tp = sum * 8;
cout << "Bits =" << Tp << ".\n";
Thput = Tp /(time); 
cout << "Throughput =" << Thput << "bps" << ".\n";
Throughput = Thput/1000000;
//cout << "Throughput =" << Throughput << "Mbps" << ".\n";
MYFILE << "10 " << " " << Throughput << "Mbps" <<".\n";


if (b > 21 && b <31.0 ) {
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(9);
	MF << "b=" << b << ".\n";
if (c== 'r')
	sum = sum + h;
//cout << "sum =" << sum << ".\n";
	switch (c)
	{
		case 's':
		T = b;
		break;
		
		case 'r' :
		S = b- T;
		cout << "S(ze)= " << S << ".\n";
		time = time + S;
		cout << "Total Time =" << time << ".\n";
		break;	
	}	
}

cout << "Total number of bytes received = " << sum << "Bytes" <<".\n";
cout << "Total Time =" << time << ".\n";
Tp = sum * 8;
cout << "Bits =" << Tp << ".\n";
Thput = Tp /(time); 
cout << "Throughput =" << Thput << "bps" << ".\n";
Throughput = Thput/1000000;
cout << "Throughput =" << Throughput << "Mbps" << ".\n";
MYFILE << "20 " << " " << Throughput << "Mbps" <<".\n";

if (b > 31 && b <41.0 ) {
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(9);
	MF << "b=" << b << ".\n";
if (c== 'r')
	sum = sum + h;
//cout << "sum =" << sum << ".\n";
	switch (c)
	{
		case 's':
		T = b;
		//cout << "T(ze) =" << T << ".\n";
		break;
		
		case 'r' :
		S = b- T;
		cout << "S(ze)= " << S << ".\n";
		time = time + S;
		cout << "Total Time =" << time << ".\n";
		break;	
	}	
}

cout << "Total number of bytes received = " << sum << "Bytes" <<".\n";
cout << "Total Time =" << time << ".\n";
Tp = sum * 8;
cout << "Bits =" << Tp << ".\n";
Thput = Tp /(time); 
cout << "Throughput =" << Thput << "bps" << ".\n";
Throughput = Thput/1000000;
cout << "Throughput =" << Throughput << "Mbps" << ".\n";
MYFILE << "30 " << " " << Throughput << "Mbps" <<".\n";
cout << "30 " << " " << Throughput << "Mbps" <<".\n";
 
}
myfile.close();
}

else cout << "Unable to open file";


return 0;
}

Last edited on
@Chervil can u plz help me out in figuring it out :(
@Dero - I'll look at this if I get time, but it might not be today.
@chervil thank u for your reply... I did give it another try but its really long ,I think it can be written in a better way .... I've only done 3 intervals as an example though it should be 15 because I am taking the interval every 10s and my time is 150s
here's what I have come up with..
thanx in advance & sry for all the trouble .. I really appreciate your help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <iomanip>

using namespace std;

int main() {
    char c;
    string line;
    int numlines = 0, sum1 = 0,sum2 =0, sum3 =0 ,num, h,ze;
    double b, S, T,time = 0.0;
    string m,d,e,f,g,q,i,o,j,k,l,y,n,p,r,z,x,w;
    double Tp = 0.0, Thput = 0.0;
    double time1 = 0.0, time2 = 0.0, time3 =0.0; 
    double Throughput = 0.0;  
    double Te[2];
   
ifstream myfile ("RN1-9.tr");
ofstream MYFILE("Tjr.tr");
if(myfile.is_open()) {
while (getline(myfile,line)) {
	numlines++;
	istringstream input(line);
    input >> c >> b >> m >> d >> e >> f >> g >> h >> q >> i >> o >> j>> k;

if(b > 11.0 && b <= 21.0) {
	
	if (c== 'r')
		sum1 = sum1 + h;

	
	switch (c)
	{
		case 's':
		T = b;
		break;
		
		case 'r' :
		S = b- T;
		time1 = time1 + S;
		break;	
	}	

}

if(b > 21.0 && b <= 31.0) {
	
	if (c== 'r')
		sum2 = sum2 + h;
	
	switch (c)
	{
		case 's':
		T = b;
		break;
		
		case 'r' :
		S = b- T;
		time2 = time2 + S;
		break;	
	}	
}

if (b > 31.0 && b <= 41.0) {
		
	if (c== 'r')
		sum3 = sum3 + h;
		
	switch (c)
	{
		case 's':
		T = b;
		break;
		
		case 'r' :
		S = b- T;
		time3 = time3 + S;
		break;	
	}	

}

}
	
Tp = sum1 * 8;
double tt;
tt = Tp /(time1); 
Te[0] = tt/1000000;
MYFILE << fixed <<  setprecision(9); 
MYFILE << "10 " << " " << Te[0]<<".\n";

Tp = sum2 * 8;
double TTT;
TTT = Tp /(time2); 
Te[1] = TTT/1000000;
MYFILE << fixed <<  setprecision(9); 
MYFILE << "20 " << " " << Te[1] <<".\n";

Tp = sum3 * 8;
double Ttt;
Ttt = Tp/(time3); 
Te[2] = Ttt/1000000;
MYFILE<< fixed <<  setprecision(9);      
MYFILE << "30 " << " " << Te[2] << ".\n";

for (num =0; num<=2; num++){
	 Throughput+= Te[num];
 }
 
 
double average =0.0;
	cout << "num" << num << ".\n";
	average = Throughput /num ; 
	
cout << fixed << setprecision(9);
cout <<  "Average Throughput" <<  average << ".\n"; 
	
myfile.close();
}

else cout << "Unable to open file";


return 0;
}
I had another look at this now. I wanted to try to write some code for this myself before looking at your code - which I've attempted, but found I wasn't completely sure of the requirements. But anyhow, having had a go, I feel better equipped to read through your code which I'm now proceeding to do. Sorry it's taken so long for a response - a bit short of time these days.

Just a quick comment so far, if(b > 11.0 && b <= 21.0) I think code of this style is too specific, you need a more generalised solution in order to avoid having to re-write the code to suit a specific input file.
@chervil; thank u for your reply... well my requirements are to read a specific range in a file.. the range should be read from 10.0- 20.0 because my time is a double and it ends at 150.0 . here's a link to the file I am working on to give u a better picture
http://www.mediafire.com/view/7qab18ooahl0a2r/RN1-9.tr

and then perform the above calculations to get the throughput within each range and write it in a separate file as
10 Throughput1 = value from the above calculations
20 Throughput2 = value from the above calculations

finally I should average the throughputs I got.. I have already done that in the last code I have posted but somehow I am stuck in changing my code into a more generalized form so that I don't have to repeat the process 15 times .. sry for all the trouble but somehow I am stuck ... I really appreciate your help thanx in advance

Here's a possible way of breaking up the input into time ranges. I've used two variables, timelimit and timeincr to help with this. All records with a time less than timelimit are processed as the first range, then the timelimit is increased by the number of seconds timeincr and the next range is processed.

In this version, the detailed calculations have not yet been done, but it creates an output file. The purpose of the file is simply to verify that the file is being correctly divided into the required time intervals.

Hope this helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>

    using namespace std;

int main()
{
    ofstream fout("output.txt");
    ifstream fin("RN1-9.tr");

    if (!fin)
    {
        cout << "input file not open" << endl;
        return 1;
    }

    fout << fixed << setprecision(9);

    double timelimit = 0.0;    // ending time of first block of data
    double timeincr  = 10.0;   // duration of each block of data

    string s1, s2, s3, s4, s5; // dummy variables to hold unwanted data 
    string line;
    
    char   type;   
    double time;   
    int    bytes;  
    
    istringstream ss;


    //------- Priming read. Get first line.
    getline(fin, line);
    ss.str(line);
    ss >> type >> time;

    while (fin)
    {
        while (ss && time <= timelimit)
        {
            //------------- process the line here ------------------

            switch (type)
            {
                case 's':
                    fout << "send: " << time  << endl;
                    break;

                case 'r':
                    ss >> s1 >> s2 >> s3 >> s4 >> s5 >> bytes;
                    fout << "recv: " << time << " bytes: " << bytes << endl;
            }

            //------------- end of process, now read the next line -----

            if (getline(fin, line))
            {
                ss.clear();             // clear all flags
                ss.str(line);
                ss >> type >> time;
            }
            else
            {
                ss.clear(ios::failbit); // set the fail flag
            }
        }
        

        timelimit += timeincr; // increment limit to next value
        fout << "\n------------------\n";
        fout << "time limit: " << timelimit << endl;
    }

    cout << "Done" << endl;
    return 0;
}
@Chervil... thank u so much for all the help you have given me and taking the time to answer my question ... And sorry for my late reply.
well I just wanted to clarify some parts regarding the code and plz correct me If I was mistaken

getline(fin, line);
ss.str(line);

here the getline function will input the file and the line and then the str will return the line associated with string stream object.
 while (fin)
donot I need to close my file at the end as I did with the
if(myfile.is_open())
 if (getline(fin, line))
This checks if an incorrect string was read and clears the error bits so that you can parse new string.

Finally I have finished writing the code with all the calculations...

plz if you have any comments on what I have written plz let me know
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>

    using namespace std;

int main()
{
    ofstream fout("Check.tr");
    ifstream fin("RN1-9.tr");
    ofstream fOut("Throughput1-9.tr");

    if (!fin)
    {
        cout << "input file not open" << endl;
        return 1;
    }

    fout << fixed << setprecision(10);
    fOut << fixed << setprecision(10);
    cout << fixed << setprecision(10);

    double timelimit = 0.0;    // ending time of first block of data
    double timeincr  = 10.0;   // duration of each block of data

    string s1, s2, s3, s4, s5; // dummy variables to hold unwanted data 
    string line;
    
    char   type;   
    double time;   
    int    bytes;  
    double T,S=0.0; 
    int Sum[16]= {0};
    int num = 0; 
    double total[16] ={0.0};
    istringstream ss;
    double Tp =0.0;
    double Throughput= 0.0;
    double Te[15]= {0}; 
    int Bits = 0; 
    int Range;

    //------- Priming read. Get first line.
    getline(fin, line);
    ss.str(line);
    ss >> type >> time;

    while (fin)
    {
        while (ss && time <= timelimit)
        {
            //------------- process the line here ------------------
            if (type == 'r') {
                  ss >> s1 >> s2 >> s3 >> s4 >> s5 >> bytes;
                  Sum[num]= Sum[num] +bytes;
                  fout << "Total Bytes" << Sum [num]<< ".\n"; 
                  Bits = Sum[num] * 8;
                  fout << "Bits =" << Bits << ".\n";
                    
                      }
            switch (type)
            {
                case 's':
                    fout << "send: " << time  << endl;
                    T = time;
                    break;

                case 'r':
                    
                    fout << "recv: " << time << " bytes: " << bytes << endl; 
                    S = time - T; 
                    total[num] = total[num] + S; 
                    fout << "Subtraction of time" << S << ".\n";
                    fout << "Total Time " << total[num] << ".\n"; 
                    Tp = Bits/total[num];
                    Te[num] = Tp/ 1000000; 
                    fout << "Throghput = " << Te[num] << ".\n";
                    break;
                     
            }
         
            //------------- end of process, now read the next line -----

            if (getline(fin, line))
            {
                ss.clear();             // clear all flags
                ss.str(line);
                ss >> type >> time;
            }
            else
            {
                ss.clear(ios::failbit); // set the fail flag
         
            }
            
        }
        
        

        timelimit += timeincr; // increment limit to next value
        num++; 
        fout << "\n------------------\n";
        fout << "time limit: " << timelimit << endl;
        fout << "num = " << num << ".\n";
    }
    
cout << "num1= " << num << ".\n";
	 for (num = 1; num <= 15; num++) {
	 cout << "Te[num]" << Te[num] << ".\n";
	 Throughput+= Te[num];
	 //cout << Throughput << ".\n";
		}
 
//......... Writing the Range and the Throughput in a separate File ...
for(Range =10,num=1; Range <=150, num <=15; Range+=10,num++){
	cout << Range << ".\n";
    fOut << Range << "    " << Te[num] << ".\n";
    cout << "num2 = "<< num << ".\n"; 
		} 

double average =0.0;
	cout << "num3 =" << num << ".\n";
	
	average = Throughput /(num-1) ; 
	cout << num << Throughput << ".\n";
	cout <<  "Average Throughput" <<  average << ".\n"; 
 
    cout << "Done" << endl;
    return 0;
}


My code works perfectly fine when I specify my sum and total and throughput as
 int Sum[16]= {0};  double total[16] ={0.0};double Te[15]= {0};

Correct output

10    0.0000000000.
20    0.9476869944.
30    0.9481124302.
40    0.9481124351.
50    0.9481124311.
60    0.9481124303.
70    0.9481124297.
80    0.9481124292.
90    0.9481124288.
100  0.9481124323.
110  0.9481124323.
120  0.9481124292.
130  0.9481124313.
140  0.9481124329.
150  0.9481124292.


But if I use
 int Sum[15]= {0};  double total[15] ={0.0};double Te[14]= {0}; 

The last value contains garbage ...
150    137.9070970942.
...
I tried using the
sizeof 
so that I wouldnt have to define my size but I had errors when compiling.
Finally when computing the average I have to divide by my total throughput by
num -1
because my num is displayed as
num = 16.
not
 15
.

Extermely sorry for the long post
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.
Topic archived. No new replies allowed.