change the loop

Write your question here.

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
// Program Looping uses a count-controlled loop to read and
02
// sum 10 integer values and an event-controlled loop to
03
// read and sum values until a negative value is found.
04
// The data is on file Looping.dat.
05
 
06
#include <iostream>
07
#include <fstream>
08
using namespace std;
09
 
10
int main ()
11
// The data is on file Looping.dat.
12
 
13
#include <iostream>
14
#include <fstream>
15
using namespace std;
16
 
17
int main ()
18
{
19
  ifstream  inData;
20
  int value;
21
  int counter;
22
  int sum;
23
  inData.open("looping.dat");
24
 
25
  counter = 1;
26
  sum = 0;
27
  do while (counter <= 10)
28
  { // Ten values read and summed
29
    inData  >> value;
30
    sum = sum + value;
31
    counter++;
32
  }
33
  cout << "The first sum is " << sum << endl;
34
 
35
  inData  >> value;
36
  sum = 0;
37
  while (value >= 0)
38
  { // Values are read and summed until a negative is read
39
    sum = sum + value;
40
    inData  >> value;
41
  }
42
  cout << "The second sum is " << sum << endl;
43
  return 0;
44
}


my answers are
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
// Program Looping uses a count-controlled loop to read and
02
// sum 10 integer values and an event-controlled loop to
03
// read and sum values until a negative value is found.
04
// The data is on file Looping.dat.
05
 
06
#include <iostream>
07
#include <fstream>
08
using namespace std;
09
 
10
int main ()
11
// The data is on file Looping.dat.
12
 
13
#include <iostream>
14
#include <fstream>
15
using namespace std;
16
 
17
int main ()
18
{
19
  ifstream  inData;
20
  int value;
21
  int counter;
22
  int sum;
23
  inData.open("looping.dat");
24
 
25
  counter = 1;
26
  sum = 0;
27
  do{

 while (counter <= 10)
28
  { // Ten values read and summed
29
    inData  >> value;
30
    sum = sum + value;
31
    counter++;
32
  }
33
  cout << "The first sum is " << sum << endl;
34
 
35
  inData  >> value;
36
  sum = 0;
37
  while (value >= 0)
38
  { // Values are read and summed until a negative is read
39
    sum = sum + value;
40
    inData  >> value;
41
  }
42
  cout << "The second sum is " << sum << endl;
43
  return 0;
44
}


is this correct?

my ques. is that the instructor asks me to change while statements to do-while statements and for statements as well. also, for statements implement the counter control ?

Last edited on
up up
closed account (48T7M4Gy)
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
// Program Looping uses a count-controlled loop to read and
// sum 10 integer values and an event-controlled loop to
// read and sum values until a negative value is found.
// The data is on file Looping.dat.

#include <iostream>
#include <fstream>

int main()
{
	int value;
	int counter = 1;
	int sum = 0;

	std::ifstream  inData;
	inData.open("looping.dat");

	while (counter <= 10)
	{ // Ten values read and summed
		inData >> value;
		sum = sum + value;
		counter++;
	}
	std::cout << "The first sum is " << sum << std::endl;

	inData >> value;
	while (value >= 0)
	{ // Values are read and summed until a negative is read
		sum = sum + value;
		inData >> value;
	}
	inData.close();
	std::cout << "The second sum is " << sum << std::endl;
		
	return 0;
}
Topic archived. No new replies allowed.