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;
}
|