C++ Programming Nublet Part2
OttoDestruct
Join Date: 2002-11-08 Member: 7790Members

in Off-Topic
Eh.. once again I'm stumped <!--emo&???--><img src='http://www.unknownworlds.com/forums/html/emoticons/confused.gif' border='0' style='vertical-align:middle' alt='confused.gif'><!--endemo--> . This time I got no friggin clue where to even start programming. The assignment is to take an input up to 32,000 something (i know how to do this) and then print it on the screen like this, with three spaces between each number.
Input number: 1234
0 1 2 3 4
1 2 3 4
2 3 4
3 4
4
Teacher wants 0's to appear if theres not a number. I'm loooost and theres NO help in the book.
Input number: 1234
0 1 2 3 4
1 2 3 4
2 3 4
3 4
4
Teacher wants 0's to appear if theres not a number. I'm loooost and theres NO help in the book.
Comments
That's if your not allowed to use strings, of course, otherwise just store that input into a string and use a for loop as such:
for int j=0; j<=string.length(); ++j
{
for int i=j; i<=string.length(); ++i;
cout << string[i] << " ";
cout << '\n';
}
might have to tweak it a bit
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->for( i=5; i>0; i-- )
{
for( j=0; j<i; j++ )
{
if( j>( 5-string.length() ) )
cout << "0 ";
else
cout << string[i] << " ";
}
cout << "\n";
}<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
didn't think about it too hard so there might be more to it (mainly including the input code and stuff but you're supposed to learn that stuff for yourself anyways =3 )
<b>edit:</b> fixed the formatting... silly code tags =P
um... well... uh... loops are really just coding timesaver, you could just do it line by line by hand... it'd just take ages.
the string commands are literally just stuff like nameofstring.length() or strlen(nameofstring). depends on how you're doing it, but it should be covered in the C++ help that comes with MVSC++ (if you're not using visual studio to compile then eep! )
like 404 said though you could just use the remainder command (%) to split the number up; I had to do that when I was making damage numbers float off things for my playstation game in university ^^
imagine you have the number 680, if you divide it by 100 you'll have it fall into 6 neat little 100 chunks but there's an annoying 80 left over right?
what the remainder function does is basically give you the remainder.
so if you have 456 and throw %100 at it you'll get 56 out.
if you throw 456 through %50 you'll get a 6 out =3
bleh, not the clearest idea but I can do a better post if you need it, I just gotta run do stuff just now.
Hope it helps <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
number % 10000
number % 1000
number % 100
number % 10
number % 1
Ok I guess thats the beginning of it but I still don't understand how to rip it apart and space it.
well, this is a <b>lot</b> like my damage program (I had to rip the individual numbers out so I could find the right graphic for each digit).
the % is what you use to knock off the 'front' part of the number once it's used; so when you want the last 3 digits in your 5 digit sequence you can just say MyBigIntegerThingy % 1000. after that I'm afraid you'll have to divide the remainder to find out how much it has.
basically lets try this with 345 and imagine it's supposed to be a 3 digit showout instead of your 5.
first you've got nothing in front so you don't need the % for the start, you just divide it by 100 and stick the result in an integer. The great thing about integers for stuff like this is that there's no decimals or rounding so all the gubbinz (that's a technical term don'tcha know =3) gets lost.
anyways, dividing 345 in integers will give you a 3; that's your first number right there.
for digit number two you're going to have to chop off the 300, so you just hit it with %100 which gives you 45. now you can just divide that by 10 and it'll give you a lovely 4 =D
for the last one just do %10 and the answer is your last digit ^^
hopefully I managed to make that relatively clear =s
As for lining them up like that...
int number;
cout << "ENTER THY NUMBER: ";
cin >> number;
cout << number % 10000;
...and so forth?
I'm probably just as noobie as you. <!--emo&::marine::--><img src='http://www.unknownworlds.com/forums/html/emoticons/marine.gif' border='0' style='vertical-align:middle' alt='marine.gif'><!--endemo-->
<!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin--><!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
// Program by Aaron Friedley
// September 24, 2003
// Program takes input up to 32,767 and formats it to single numbers in a pyramid
#include <iostream.h>
int main(void)
{
//Declarations
short int pyramid;
int num1,
int num2,
int num3,
int num4,
int num5;
//Input
cout << "Program by Aaron Friedley MWF 10:00"
<< "\n\nInput positive number up to 32,767: ";
cin >> pyramid;
// Calculations
pyramid / 10000 = num1
pyramid % 1000 = num2
pyramid % 100 = num3
pyramid % 10 = num4
pyramid % 1 = num5
// Output
// First line
cout << "\n\n" << num1 << " " << num2 << " " << num3
<< " " << num4 << " " << num5;
// Second line
cout << "\n" << num1 << " " << num2 << " " << num3
<< " " << num4;
// Third line
cout << "\n" << num1 << " " << num2;
// Fourth line
cout << "\n" << num1 << "\n";
return 0; // Terminate main
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2--><!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
Obviously its not completely done, but I think you can see basically what I'm doing. Is this the right track?
*Edit*
Updated code
the calculations should be...
if(pyramid >= 10000) num1 = pyramid / 10000;
else num1 = 0;
if(pyramid >= 1000) num2 = (pyramid % 10000) / 1000;
else num2 = 0;
if(pyramid >= 100) num3 = (pyramid % 1000) / 100;
else num3 = 0;
if(pyramid >= 10) num4 = (pyramid % 100) / 10;
else num4 = 0;
num5 = pyramid % 10;
cxx: Error: hwk04.cxx, line 26: expression must be a modifiable lvalue
pyramid / 10000 = num1;
--------^
cxx: Error: hwk04.cxx, line 27: expression must be a modifiable lvalue
pyramid % 1000 = num2;
--------^
cxx: Error: hwk04.cxx, line 28: expression must be a modifiable lvalue
pyramid % 100 = num3;
--------^
cxx: Error: hwk04.cxx, line 29: expression must be a modifiable lvalue
pyramid % 10 = num4;
--------^
cxx: Error: hwk04.cxx, line 30: expression must be a modifiable lvalue
pyramid % 1 = num5;
--------^
cxx: Info: 5 errors detected in the compilation of "hwk04.cxx".
And just before that it wouldnt let me use the variables num1 num2 etc without giving them an initial value of 0, said it was trying to use it before it was defined, wth.
you're trying to make 'pyramid / 1000' store the value of 'num1' and stuff, check my post out and you'll see that I put them the right way around ^^
int main(void)
{
cout << "/me slams head on table\n"
<< "DOH";
return 0;
}
*Edit*
Uh now I need to figure out how to place zero's in place without using if statements. She doesn't want us going ahead of the book, even though the book sucks.
*EditEdit*
Semicolons are the bane of my existance.
basically instead of saying
int num1;
you can say...
int num1 = 0;
...at the start and that makes 100% sure that it starts off with a value of '0' instead of whatever random nonsense was in the memory address it takes when it got initialised <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
like I said though, doesn't matter too much this early on =P
<b>edit:</b> lol, nice coincidence considering your edit XD
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
// Program by Aaron Friedley
// September 24, 2003
// Program takes input up to 32,767 and formats it to single numbers in a pyramid
#include <iostream.h>
int main(void)
{
//Declarations
short int pyramid;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
//Input
cout << "Program by Aaron Friedley MWF 10:00"
<< "\n\nInput positive number up to 32,767: ";
cin >> pyramid;
// Calculations
num1 = pyramid / 10000;
num2 = pyramid % 10000 / 1000;
num3 = pyramid % 1000 / 100;
num4 = pyramid % 100 / 10;
num5 = pyramid % 10;
// Output
// First line
cout << "\n\n" << num1 << " " << num2 << " " << num3
<< " " << num4 << " " << num5;
// Second line
cout << "\n" << num2 << " " << num3 << " " << num4;
// Third line
cout << "\n" << num3 << " " << num4;
// Fourth line
cout << "\n" << num5 << "\n";
return 0; // Terminate main
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
Me = <!--emo&::nerdy::--><img src='http://www.unknownworlds.com/forums/html/emoticons/nerd.gif' border='0' style='vertical-align:middle' alt='nerd.gif'><!--endemo-->
I still can't believe you've not been taught/are allowed to use logic statements but are supposed to do projects like this involving % operaters and stuff o.O
I didn't learn about the weird commands and stuff until looooong after I'd done logic branching to death, but then again I'm partially self-taught ^^;
heck, I prefer printf to that silly cout << stuff anyways ^~
Anyway, just to make you feel better, learning the language is only half the battle. You also need to learn all the APIs. That's the fun part!
I was a total coding nub at the time and tried to do it using intermediate strings; I didn't know about the bitwise operators at the time.
However, the more I think about it, the more I think how crap the question was. For one, there is no such thing as a 'number' in C. You've got ints, shorts, longs, floats, doubles but no numbers. And what about leading 0s ? Anyway, needless to say, I didn't get the job ! What I should have done is written some treatise on poorly worded requirements arf.