C++ Programming Nublet Part2

OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
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.

Comments

  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    store the input in a string instead of number then you can just use the string commands to disect it up like that easy =3
  • NumbersNotFoundNumbersNotFound Join Date: 2002-11-07 Member: 7556Members
    Or you could use Div and Mod (For pascal peeps) or "/" and "%" (respectively) in order to isolate the digets...

    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
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    edited September 2003
    lol, wouldn't it be more like...

    <!--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
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Uh.. I can't touch loops yet, and I don't know string commands. Professor sux0rs as a teacher.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    he expects you to do it without loops? o.O

    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 ^^
  • BurncycleBurncycle Join Date: 2002-11-24 Member: 9759Members, NS1 Playtester
    you can do if/else at least right? <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Its a she professor actually. And yes we talked about getting the number with % but I don't understand how to use it, it was never explained and the book doesn't explain it either.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    it's kinda weird but I'll sum it up as best as I can...

    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-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Yes that makes what she was saying clearer. She did this on the board:

    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.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    back!

    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
  • pardzhpardzh Join Date: 2002-10-25 Member: 1601Members
    I'm just learning to code in C++ too, and we've just gone over spacing and that business.

    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-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    edited September 2003
    Heres what I've got so far (btw thankies for all the help, I realize none of you HAVE to help me, its greatly appreciated)

    <!--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
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    edited September 2003
    almost...

    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;
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Not allowed to use that. And I'm getting crap during compile that says:

    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.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    lol, you've got the = the wrong way around =3
    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 ^^
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    edited September 2003
    #include <iostream.h>

    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.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    edited September 2003
    it's not important really for this or anything but it's also good programming practise to initialise your variables with a value.
    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
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    BWAHAHAH!! I PWNED THIS ASSIGNEMENT!! I R DONE!!!.. er... thanks for help.. heres the end code, didn't even have to mess with more code to get the zero's to display, does it automatically.

    <!--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-->
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    lol, congrats <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.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 ^^;
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    I picked up C a few years back from a two volume C for dummies, but I've forgotten most of it, so this is basically starting over. And even the stuff I do remember.. its C.. so its not a real use to me considering this is c++ <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    actually, all the old C commands still work in C++ so you can get away with printf and stuff quite happily <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
    heck, I prefer printf to that silly cout << stuff anyways ^~
  • CreepieCreepie Join Date: 2003-02-19 Member: 13734Members
    I never got on with cout and cin. Much prefer the printf family (except scanf - scanf is pooey). Only problem with printf is that it uses the ellipsis operator as formal argument giving rise to potential stack problems if you don't specify the correct number of arguments.

    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!
  • CreepieCreepie Join Date: 2003-02-19 Member: 13734Members
    Actually, I remember this nasty little question in an interview I had once. It was to reverse the binary digits of a number.

    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.
  • XodlikeXodlike Join Date: 2003-06-03 Member: 16985Members
    heh you see his professer post here saying WHY THE **** ARE U GeTTING HELP!!..
Sign In or Register to comment.