From While's to For's...

I cannot seem to switch all of these WHILE loops over to FOR loops without creating some sort of reoccurring problem with the formula processing. I could post a screenshot of the problem I am having, but honestly it would be just as easy to explain it.. If I change them to FOR loops, it changes all of my numbers within the entire function. Maybe I am not doing it right, but I need some major help. (And yes, this is for school, which is why I have provided as much of my own work as possible.) Thanks for any help provided!

*If anything else is wrong I would appreciate the help, but my teacher is fully satisfied with the following, I just also need this converted for a separate assignment.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int firstNum, secondNum, secondNum1, secondNum2;
	int sumEvenNum = 0;
	int sumSquareOddNum = 0;

	char chCounter;

	int counter;

	//AAAAAAAA

	cout << "Enter Number One: (Lower #)      ";
	cin >> firstNum; 
	cout << "\n\n";
	cout << "Enter Number Two: (HIgher #)     ";
	cin >> secondNum;
	cout << "\n\n";

	while (firstNum < secondNum) {


		//BBBBBBBB

		if (firstNum % 2 == 0)
			counter = firstNum + 1;
		else
			counter = firstNum + 2;

		secondNum2 = secondNum;

		secondNum1 = secondNum - 1;

		cout << "Odd numbers between " << firstNum << " and "
			<< secondNum2 << " are... \n\n" << endl;

		while (counter <= secondNum1)
		{
			cout << counter << " ";
			counter = counter + 2;
		}
		cout << "\n";
		cout << endl;

		//CCCCCCCC
		if (firstNum % 2 == 0)
			counter = firstNum;
		else
			counter = firstNum + 1;

		while (counter <= secondNum)
		{
			sumEvenNum = sumEvenNum + counter;
			counter = counter + 2;
		}

		cout << "Sum of even integers between " << firstNum << " and "
			<< secondNum << " = " << sumEvenNum << "\n" << endl;

		//DDDDDDDD
		cout << "The Square of Number's ONE through TEN. \n" << endl;
		counter = 1;
		while (counter <= 10)
		{
			cout << setw(4) << counter << "     = " << setw(5)
				<< counter * counter << endl;
			counter++;
		}

		cout << endl;
		cout << "\n";

		//EEEEEEEE
		if (firstNum % 2 == 0)
			counter = firstNum + 1;
		else
			counter = firstNum;

		while (counter <= secondNum)
		{
			sumSquareOddNum = sumSquareOddNum + counter * counter;
			counter = counter + 2;
		}

		cout << "Sum of the squares of odd numbers between "
			<< firstNum << " and " << secondNum << ": "
			<< sumSquareOddNum << endl;
		cout << "\n";

		//FFFFFFFF
		cout << "UPPER CASE LETTERS...  ";

		chCounter = 'A';
		while (chCounter <= 'Z')
		{
			cout << chCounter << " ";
			chCounter++;
		}
		cout << endl;
		cout << "\n\n";
		break;
	}


	while (secondNum <= firstNum) {
		cout << "First Number MUST be less than second #... \n";
		cout << endl;
		break;
	}

	return 0;
}


********************************************************************************


EDIT on 11/8/2014:

I was able to figure this out moments after I posted the problem. For anyone else who may be having trouble with it, here is the solution to the switch from WHILE to FOR loops.

*Also, this is question # 10 out of Malik's Textbook, C++ PROGRAMMING:
FROM PROBLEM ANALYSIS TO PROGRAM DESIGN..

The exact question is :

Write a program that uses FOR loops to perform the following steps:
a. Prompt the user to input two integers: firstNum and secondNum
(firstNum must be less than secondNum).
b. Output all odd numbers between firstNum and secondNum.
c. Output the sum of all even numbers between firstNum and
secondNum.
d. Output the numbers and their squares between 1 and 10.
e. Output the sum of the square of the odd numbers between firstNum
and secondNum.
f. Output all uppercase letters




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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int firstNum, secondNum, secondNum1, secondNum2;
	int sumEvenNum = 0;
	int sumSquareOddNum = 0;

	char chCounter;

	int counter;

	//AAAAAAAA

	cout << "Enter Number One: (Lower #)      ";
	cin >> firstNum;
	cout << "\n\n";
	cout << "Enter Number Two: (HIgher #)     ";
	cin >> secondNum;
	cout << "\n\n";

	for (; firstNum < secondNum;) {


		//BBBBBBBB

		if (firstNum % 2 == 0)
			counter = firstNum + 1;
		else
			counter = firstNum + 2;

		secondNum2 = secondNum;

		secondNum1 = secondNum - 1;

		cout << "Odd numbers between " << firstNum << " and "
			<< secondNum2 << " are... \n\n" << endl;

		for (; counter <= secondNum1;)
		{
			cout << counter << " ";
			counter = counter + 2;
		}
		cout << "\n";
		cout << endl;

		//CCCCCCCC
		if (firstNum % 2 == 0)
			counter = firstNum;
		else
			counter = firstNum + 1;

		for (; counter <= secondNum;)
		{
			sumEvenNum = sumEvenNum + counter;
			counter = counter + 2;
		}

		cout << "Sum of even integers between " << firstNum << " and "
			<< secondNum << " = " << sumEvenNum << "\n" << endl;

		//DDDDDDDD
		cout << "The Square of Number's ONE through TEN. \n" << endl;
		counter = 1;

		for (; counter <= 10; counter++)
		{
			cout << setw(4) << counter << "     = " << setw(5)
				<< counter * counter << endl;
		}

		cout << endl;
		cout << "\n";

		//EEEEEEEE

		if (firstNum % 2 == 0)
			counter = firstNum + 1;
		else
			counter = firstNum + 2;

		secondNum2 = secondNum;

		secondNum1 = secondNum - 1;

		for (; counter <= secondNum1;)
		{
			sumSquareOddNum = sumSquareOddNum + counter * counter;
			counter = counter + 2;
		}

		cout << "Sum of the squares of odd numbers between "
			<< firstNum << " and " << secondNum2 << ": "
			<< sumSquareOddNum << endl;
		cout << "\n";

		//FFFFFFFF
		cout << "UPPER CASE LETTERS...  ";

		chCounter = 'A';
		for (; chCounter <= 'Z'; chCounter++)
		{
			cout << chCounter << " ";

		}
		cout << endl;
		cout << "\n\n";
		break;
	}


	for (; secondNum <= firstNum;) {
		cout << "First Number MUST be less than second #... \n";
		cout << endl;

	}

	return 0;
}
Last edited on
You should use "if" instead of "for" to check a condition, "for" will do the action you want to do. So you should start with :
1
2
3
4
5
if (number1 < numbre2){
cout << """First Number MUST be less than second #... \n";
}else{
//you start the all the work here
}


\n and" cout<< endl;" do the same thing.

you do not need multiple "for" loops, you can do all the work within one, for example :
1
2
3
4
5
6
7
8
9
10
11
for(int x=0; x < 10; x++){
if (x  > 5){
numberOfNumSmaller5 = numberOfNumSmaller5 +1  //count the number who are < 5
}

if (x > 4){
sum4Higher = sum4Higher + x; // get the sum of all the number who are > 4 
}

sum = sum + x // sum of all the numbers between 0 to 9
}


Last edited on
moufou:
you do not need multiple "for" loops, you can do all the work within one, for example :
I agree with the point. But in this case, the assignment needs at least three different loops: one from firstNum to secondNum, one from 1 to 10, and one from 'A' to 'Z'. Also if the parts of the assignment have to be output in the order given, then it also does not work.

jrick1229: Since it is an exercise in for loops, this code:
1
2
3
4
5
6
7
8
9
10
		if (firstNum % 2 == 0)
			counter = firstNum;
		else
			counter = firstNum + 1;

		for (; counter <= secondNum;)
		{
			sumEvenNum = sumEvenNum + counter;
			counter = counter + 2;
		}

could be:
1
2
3
4
5
6
7
8
9
10
		if (firstNum % 2 == 0)
			counter = firstNum;
		else
			counter = firstNum + 1;

		for (; counter <= secondNum; counter = counter + 2)
		{
			sumEvenNum = sumEvenNum + counter;
			
		}

or:
1
2
3
4
5
6
7
8
9
10
		if (firstNum % 2 == 0)
			counter = firstNum;
		else
			counter = firstNum + 1;

		for (; counter <= secondNum; counter += 2)
		{
			sumEvenNum = sumEvenNum + counter;
			
		}

or:
1
2
3
4
5
6
		for ( counter = (firstNum % 2 == 0) ? firstNum : firstNum + 1;  counter <= secondNum; counter += 2)
		{
			sumEvenNum = sumEvenNum + counter;
			
		}

or:
1
2
3
4
5
6
		for ( counter = firstNum + (firstNum % 2); counter <= secondNum; counter += 2)
		{
			sumEvenNum = sumEvenNum + counter;
			
		}
Unless I am missing something, they will all achieve the same result.
Last edited on
Oh sorry I didn't not read completely .
Last edited on
Topic archived. No new replies allowed.