Multiple Loops

Multiple loops with increasingly complex equations

Page 1 of 1

3 Replies - 4210 Views - Last Post: 30 October 2008 - 01:19 PM Rate Topic: -----

#1 Lattimer24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-October 08

Multiple Loops

Posted 29 October 2008 - 10:10 PM

[font=Arial]Greetings all! I am new to this forum and a beginner in C++. I have utilized this site many times to answer simple questions for past assignments and most members have been thoughtful and detailed in their answers. Currently, I am working on an assignment that involves multiple loops, each computing a simple mathematical statement. I am stuck on Part d, "Output the sum of the square of odd numbers between firstNum and secondNum. Use a loop of your choice." Part d is labeled in my code as //Begin Part d. The output I receive seems to only square the last odd number in the sequence and does not sum the previous squares. I have tried other loop types with no success. Here it is, please go easy on the newb! :D

/*************
Program: Numbers.cpp
Author: Doug Mainwaring
Date: October 26, 2008
Description: Write a program that uses loops to perform the following steps. Note that each part must use the specified
looping technique. Prompt the user for two integer numbers: name them firstNum and secondNum (firstNum must be less than
secondNum—tell the user this!). HINT - use % operator to detemine odd and even.
·		 Part a. Output all odd numbers between firstNum and secondNum. Use a while loop.
·		 Part b. Output the sum of all even numbers between firstNum and secondNum. Use a do while loop.
·		 Part c. Output the numbers and their square between one and 10. Use a for loop.
·		 Part d is optional—bonus points for this. Output the sum of the square of odd numbers between firstNum and
secondNum. Use a loop of your choice.
·		 Part e is optional- bonus points for this. Output all uppercase letters from A to Z, using a loop of your choice.
You must create these letters by using a loop with a character variable. Refer to the ASCII Character Set on page 1002 of
your text; it shows the alphabet and decimal equivalents.
See sample data and output below. Please note in your code (with comments) as well as your output which part you are completing.
New Concepts: string_cast, multiple loops
Challenges:
**************/

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

// Declare constants

// Function Prototypes


int main()
{
	// Declare variables below here

	int number1 = 1, number2 = 65, firstNum, secondNum, oddNum, evenNum, setNum, resultB = 0, resultC = 0, oddSquare = 0, square1;
	char letter;

	// Initialization Section for real number output
	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(2);
	// Begin your "main processing" below here

	cout << "Welcome to the Number Functions Program\n";
	cout << endl;
	cout << endl;
	cout << "Please input two whole numbers and press Enter\n";
	cout << "(Note: The first number must be less than the second number)\n";
	cin >> firstNum >> secondNum;
	cout << endl;

	setNum = firstNum % 2;
	if (setNum == 0)
	{
		oddNum = firstNum + 1;
		evenNum = firstNum;
	}
	else
	{
		oddNum = firstNum;
		evenNum = firstNum + 1;
	}

//Begin Part a

	cout << "Part a - Odd integers between " << firstNum << " and " << secondNum << " are: ";

	while (oddNum < secondNum)
	{
		cout << oddNum << " ";	
		oddNum = oddNum + 2;
	}

	cout << endl;
	cout << endl;

//Begin Part b

	cout << "Part b - Sum of even integers between " << firstNum << " and " << secondNum << " = ";

	do
	{
		resultB = resultB + evenNum;
		evenNum = evenNum +2;
	}
	while (evenNum <= secondNum);

	cout << resultB;
	cout << endl;
	cout << endl;

//Begin Part c

	cout << "Part c:\n";
	cout << endl;
	cout << "Number	Square\n";

	for (int number1 = 1; number1 <= 10; number1++)
	{
		square1 = number1 * number1;
		cout << "  " << number1 << "		 " << square1 << endl;
	}
	
	cout << endl;

//Begin Part d

	cout << "Part d - Sum of the squares of odd integers between " << firstNum << " and " << secondNum  << " = ";

	while (oddNum <= secondNum)
	{
		oddSquare = oddNum * oddNum;
		resultC = resultC + oddSquare;
		oddNum = oddNum + 2;
	}
		
	cout << resultC;
	cout << endl;
	cout << endl;
		

	return 0;
}



Thank you in advance for any assistance!

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple Loops

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5818
  • View blog
  • Posts: 12,669
  • Joined: 16-October 07

Re: Multiple Loops

Posted 30 October 2008 - 04:12 AM

In part A, you did this oddNum = oddNum + 2;, so your oddNumber is already where you don't want it.

You're doing way too much in main. Make some functions. Also, make a number check function and be done with it. Here's something to give an idea.

bool isEven(int num) { return (num % 2)==0; }
bool isOdd(int num) { return !isEven(num); }

void partA(int firstNum, int secondNum) {
	cout << "Part a - Odd integers between " << firstNum << " and " << secondNum << " are: ";
	int n = firstNum; // just for readability
	while (n<=secondNum) {
		if (isOdd(n)) {
			cout << n << " ";
		}
		n++;
	}
	cout << endl;
}



Using the code from partA function, you can easily do part d by just replacing what's in the if.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 Lattimer24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-October 08

Re: Multiple Loops

Posted 30 October 2008 - 12:44 PM

View Postbaavgai, on 30 Oct, 2008 - 04:12 AM, said:

In part A, you did this oddNum = oddNum + 2;, so your oddNumber is already where you don't want it.

You're doing way too much in main. Make some functions. Also, make a number check function and be done with it. Here's something to give an idea.

bool isEven(int num) { return (num % 2)==0; }
bool isOdd(int num) { return !isEven(num); }

void partA(int firstNum, int secondNum) {
	cout << "Part a - Odd integers between " << firstNum << " and " << secondNum << " are: ";
	int n = firstNum; // just for readability
	while (n<=secondNum) {
		if (isOdd(n)) {
			cout << n << " ";
		}
		n++;
	}
	cout << endl;
}



Using the code from partA function, you can easily do part d by just replacing what's in the if.

Hope this helps.


I appreciate the help, however, all that is mentioned in your reply is beyond what we have covered in class so far. The extent of my C++ knowledge is what you see in the code I provided. For this assignment, a loop is to be used for each part of the overall problem and should stay within the bounds of the code I have already written. Anything beyond that is definitely foreign to me. If someone can provide me with some advice on my existing code and how to make Part d work, that is what I'm looking for. Thank you for your reply though!
Was This Post Helpful? 0
  • +
  • -

#4 Lattimer24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-October 08

Re: Multiple Loops

Posted 30 October 2008 - 01:19 PM

View Postbaavgai, on 30 Oct, 2008 - 04:12 AM, said:

In part A, you did this oddNum = oddNum + 2;, so your oddNumber is already where you don't want it.

You're doing way too much in main. Make some functions. Also, make a number check function and be done with it. Here's something to give an idea.

bool isEven(int num) { return (num % 2)==0; }
bool isOdd(int num) { return !isEven(num); }

void partA(int firstNum, int secondNum) {
	cout << "Part a - Odd integers between " << firstNum << " and " << secondNum << " are: ";
	int n = firstNum; // just for readability
	while (n<=secondNum) {
		if (isOdd(n)) {
			cout << n << " ";
		}
		n++;
	}
	cout << endl;
}



Using the code from partA function, you can easily do part d by just replacing what's in the if.

Hope this helps.


baavgai,

I've been going over your reply several times when your first statement reached out and smacked me upside the head. I failed to realize that oddNum has already reached the highest odd number of the sequence hence that being the only square to be completed for Part d. I looked ahead in our syllabus and we will be coming to functions next and it seems to make complex equations easier to manage. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1