PDA

View Full Version : Another C++ programming question.


samandhi
03-01-2004, 10:25 PM
I have recently found an AWESOME home/type study course called "The Complete C++ Training Course" and I am totally loving it, as it is by far the best I have seen amoung the combination of home study and beginners guides.... The question I have is because of the following:

At the end of every chapter, there are (what you might expect) exercises to prove that you really paid attention in the reading. They are not as easy (at least so far as I have noticed), as the ones in other books. I am having trouble with the 36th exercise, because I am not totally sure that The question covers something I have learned in that chapter... In all actuallity, the book has yet to go in depth on anything related to C++ language itself (read the syntax), but instead, has covered more about WHAT it is and where it came from, and what OOP and OOD (along with what UML) are..

The reason I am asking this, is because it doesnt have the answers to the questions in the exercises, as they are used in colleges and the like....

The exercise question is this: Write a program that inputs a five-digit number, separates the number into its individual digits and prints the digits separated from one another by three spaces each. (Hint: Use the integer division and modulus operators.) For example, if the user types in 42339 the program should print:

4 2 3 3 9 Now, Im not totally sure that I have enough information (without the knowledge of the language) to do this.. Can someone help me out here and let me know, so I can store it in my brainpan? The idea I have behind this goes back to my reading from "Who's Afraid of C++?", where it talks about how variables are stored in chunks of memory. Each chunk of memory that is tagged as THAT stack for that particular variable has a memory address. I would imagine it is just a matter of telling the computer to print address 1001, then 1002 and so on... But I have no idea how to do this, as I have not reached any kind of syntax that would do that... You'll notice that this thought has NOTHING to do with division and modulous calculations, which is my problem. I guess I am not seeing how they are thinking ( or thinking the way they are), enough to figure this one out.. :(

Derision
03-01-2004, 10:50 PM
http://people.uncw.edu/tompkinsj/112/assisgnments/P1_36solution.cpp

// Exercise 1.36 Solution
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
int num;

cout << "Enter a five-digit number: ";
cin >> num;

cout << num / 10000 << " "; //prints the left most digit (5th)
num = num % 10000; //assigns a new value to num -four digits
cout << num / 1000 << " "; //prints the left most digit (4th)
num = num % 1000; //assigns a new value to num -three digits
cout << num / 100 << " "; //prints the left most digit (3rd)
num = num % 100; //assigns a new value to num -two digits
cout << num / 10 << " "; //prints the left most digit (2nd)
num = num % 10; //assigns a new value to num -one digit
cout << num << endl; //prints the last digit of num and a newline

return 0;
}

samandhi
03-01-2004, 11:39 PM
Sweet, thank you very much... That was the ONLY one in the exercises that I had any problem with...