| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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- |
|
#3
|
|||
|
|||
|
Quote:
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 |
|
#4
|
|||
|
|||
|
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 |
|
#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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help Using For Loop To Display Ordered Numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|