C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 6th, 2004, 02:56 AM
skamen skamen is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 9 skamen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help Using For Loop To Display Ordered Numbers

Hi, I'm working on a program that will allow the user to input an integer between 1 and 999 and display the numbers as shown by the example of inputing 3 which would result in the output below.

000
001
002
003

I've already written the part of the program that takes the imput from the user and splits the three digit integer into the 3 parts to be used in the for loop (for example 123 would be split into 1, 2 and 3.

We're required to use three nested for loops in order to display the numbers in the form I've listed above. Below is the code I have already written that takes in the integer from the variable and splits it up. I've racked my brain trying to figure out how to use the for loops to do what I need to do but I'm helpless. I'm not asking you to code my homework for me (which I know is a pet peeve of you forum goers) but if you could even give me an idea of how to set up the for loops I would be eternally grateful.

Here is the code I have written:

/* Name: Scott Kamen
Student Number: 9054
Assignment number and descrition: Lab 02 Assignment (Program to out put all numbers
up to the number inputed by the user; ex 2 is input: 000, 001, 002 is displayed)
Lab Instructor: Huma Kamal
Lab Day and Time: Wed at 6:00 */

#include <iostream>
using namespace std;

int main() {

*// Declare integer variable to store user input in
*int userinteger;

*// While loop to read integers inputed by user until an appropriate
*// one is input
*while (userinteger < 0 || userinteger > 999) {

* *// Prompt user to input integer and then store input in userinteger
* *// variable
* *cout << "Input a positive integer between 0 and 999: ";
* *cin >> userinteger;

* *// Test variable userinteger to see if it's inside desired range
* *// and display error message if it's not
* *if (userinteger < 0 || userinteger > 999)
* * *cout << "Error: Please input an integer between 0 and 999" << endl;
*}

*// Mod Algorithm to split up variable userinteger
*int firstdigit = (userinteger/100)%10;
*int seconddigit = (userinteger/10)%10;
*int thirddigit = userinteger%10;

*// For Loops to Display Digits in Correct Format (This is where I am stuck...)

*return 0;
}

Below is a link to the lab page if you would care to look at it.

http://www.cs.wmich.edu/~nelson/CS111FALL04/lab02/lab02.html

thanks so much,

-Scott

Reply With Quote
  #2  
Old October 6th, 2004, 04:21 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
I recommend starting off with a simple example for yourself so you can see what the result of nesting for loops is. So try running -

for (int row=0; row<10; row++)
for (int col=0; col<10; col++)
cout << col;

Once you have run that work out where you need to add a space to put one space in between each output number. Following that work out where you need to put a newline in order to put each block of numbers on a new line. Next add another for loop so it prints the entire block 10 times.

Once you have gone through these simple examples for yourself you should have a good understanding of how nesting for loops work and the above problem will become fairly trivial to figure out.

In any case like this take the time to break the problem down into simpler things to do for yourself. Solve each small bit at a time and move on when you understand how it works. Don't be afraid to try new things and break it, but when this does happen spend some extra time understanding why it has broken as this will be invaluable experience for other assignments.

-KM-

Reply With Quote
  #3  
Old October 6th, 2004, 04:32 AM
skamen skamen is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 9 skamen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by kode_monkey
I recommend starting off with a simple example for yourself so you can see what the result of nesting for loops is. So try running -

for (int row=0; row<10; row++)
for (int col=0; col<10; col++)
cout << col;

Once you have run that work out where you need to add a space to put one space in between each output number. Following that work out where you need to put a newline in order to put each block of numbers on a new line. Next add another for loop so it prints the entire block 10 times.

Once you have gone through these simple examples for yourself you should have a good understanding of how nesting for loops work and the above problem will become fairly trivial to figure out.

In any case like this take the time to break the problem down into simpler things to do for yourself. Solve each small bit at a time and move on when you understand how it works. Don't be afraid to try new things and break it, but when this does happen spend some extra time understanding why it has broken as this will be invaluable experience for other assignments.

-KM-


I understand your example and I do have a fairly decent understanding of for loops in general (understand your example fairly easily and used them in Java, but I'm no pro anything) but I'm drawing a complete blank as far as how to get the for loops for my assignment to work. I'm not sure what the best way to deal with having the zeros dispalyed etc. From looking at it, it seems like nested for loops aren't very logical for writing this type of program but they probably are and I'm just missing out a a very simple and or elegant solution to this problem using them. If someone could help me out in that area, it would be great.

Thank you for your help and time,

-Scott

Reply With Quote
  #4  
Old October 6th, 2004, 11:56 AM
howzat117 howzat117 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 3 howzat117 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Hi scott,

i realise that this doesnt have all the loops...but it works.

#include <iostream>
using namespace std;
int main()
{
bool userOk = true;
char choice;
int userInt;
int firstDig;
int secDig;
int thirdDig;
while (userOk == true)
{
cout << "Enter a number between 0 and 999:\n";
cin >> userInt;
//now check the number
if((userInt < 0) || (userInt > 999))
{
cout << "Out of range!! Try again";
}
else if ((userInt > 0) || (userInt < 999))
{
cout << "Number entered was: " << userInt << endl;



//print the number loop
for (int i = 0;i <= userInt ; i++)
{
firstDig = (i/100)%10;
secDig = (i/10)%10;
thirdDig = i%10;

cout << firstDig << secDig << thirdDig << endl;
}



//want to continue?
cout << "Would you like to continue?\n'y' or 'n'...\n";
cin >> choice;
//check
if (choice == 'y')
{
//do nothing
}
else if (choice == 'n')
{
userOk = false;
return false;
}
else
{
//just break if user pressed radnom key
userOk = false;
return false;
}
}
}
}

hope this was of some help.URLURL

Reply With Quote
  #5  
Old October 6th, 2004, 04:00 PM
Kernel Mustard Kernel Mustard is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 32 Kernel Mustard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
you should try just writing out the 3 nested loops, with SINGLE digit upper bounds (eg for (int i=0; i<9; i++)), then look at the output and the relationship of the variable in each of the for loops to the output.

Code:
 for (int i=0; i<=X; i++) {
  for (int j=0; j=Y; j++) {
   for (int k=0; k<Z; k++) {
   }
  }
 }


look at the number formed by XYZ, there's still a little something else to do, but I'm sure you can figure it out.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help Using For Loop To Display Ordered Numbers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT