Jump to content
Sign in to follow this  
markiemrboo

N00b's Programming Challenge #1

Recommended Posts

Are you psychic :P?

532108[/snapback]

 

:lol: yes! No.... I remember reading it in. ... ...the. .. I can't remember... . thread :). Some thread on these forums somewhere :P

Share this post


Link to post
Share on other sites
But you're not that bad either. You're logic and understanding seems to be pretty good - for someone of your age especially - if you ask me :P You're not too good at maths though? Was that what was holding you back from doing the for loops? Maths is frustrating :)

532106[/snapback]

 

I think a good understanding of math goes a LONG way in programming. Even if the programming isn't really about math, I think you use the same part of your brain for math that you do for programming. If you're not a math guy, that would explain why you don't like working with for loops and such. Luckily for me, I've always been good at math. I don't like doing it, but when I do I'm pretty good. Good for programming, but often I wish my talents lied elsewhere :P

 

Seriously though KB, from the few pieces of source that I've seen from you, I think you're doing very well (especially given your age and dislike for math :P). The problems that you had with your fight program were really just syntax stuff. At first, I wasn't going to bother making it actually work, but after looking at it all the logic was pretty sound, so I dove in. Just stick with it. I love going through code, and I think markie does too, so if you have questions just post em. I don't code nearly as much as I should these days, so I'm always up for an excuse to fire up my brain :)

Share this post


Link to post
Share on other sites

I suck at math, and it takes me forever to get the loops right. I think it took me 3 days before i got this program to work right. :(

 

its more of a trial and error kinda thing

Share this post


Link to post
Share on other sites
I suck at math, and it takes me forever to get the loops right.  I think it took me 3 days before i got this program to work right. :(

 

its more of a trial and error kinda thing

532178[/snapback]

 

A lot of it comes from experience too. If you get it right enough times you'll eventually be able to stop checking yourself. That's the way I was. I'd dump cout's all over my code at first just make sure it was working like I wanted, but now I'm pretty confident on the first run.

Share this post


Link to post
Share on other sites

Right, it sounds like I now have older copies of Archerzz and DEC's code in my PM inbox again because they've been changed / updated / whatever :P

 

Should I be expecting updated code, or shall I pick out of what I currently have?

Share this post


Link to post
Share on other sites

Well if that's it, then I think it's gonna have to be DEC's for .... being simple in the loops :)

 

I think comments lacked a bit in both personally! But then, i'm usually guilty of not commenting myself :P

 

Feedback time?

 

Archerzz:

while (levels <= 0)

 

Nearly, but where be the > 25 check? :) Was 1-25 remember (or at least I think that's what I said lol)!

 

if (i < 10)
       {
       cout << 0 << i << "   ";
       }
       else if (i >= 10)
       {
           cout << i << "   ";
       }
       i++;

 

The "else if" here was a little pointless really :) If i wasn't less than 10 then it *has* to be bigger than 10... sooo... you could have just had an "else" and not done any check. Pretty sure you could have also used (x + 1) instead of a whole new variable i if you wanted to aswell? But that one doesn't matter too much :P

 

DEC:

      levels=int(abs(levels));

 

Wasn't quite understanding why you were doing that? It worked far better without that, so I kindly commented it out for you :P

 

Other than that I envy your simplicity :) Well, the fact you had the effort to calculate things in to variables to make the loops easier to read! Not entirely sure why you decided to go backwards when printing the spaces:

 

//Output spaces
           for (int j = spaces; j > 0; j--){
               cout<<" ";
           }

 

But other than that, it's probably better than mine (though I do like to believe mine was a little more "clever", at the expense of simplicity perhaps lol)

 

#include <stdio.h>

int GetLevels(void);

int GetLevels(void) {
   int levels;

/* ask how many levels to draw */
   printf("How many levels to draw: ");
   scanf("%d", &levels);
/* flush input buffer */
   fflush(stdin);

/* if the input was less than 1 or bigger than 25 then it's invalid. ask again */
   while (levels < 1 || levels > 25) {
   /*
    * print out why it's asking again. if levels is less than 1 print out "less
    * than 1" in the error string, otherwise it's gotta be "bigger than 25"
    */
       printf("Sorry, %d is %s. Try again!\n", levels, (levels < 1) ? "less than 1" : "bigger than 25");
       printf("How many levels to draw: ");
       scanf("%d", &levels);
       fflush(stdin);
   }

   printf("\n");

   return levels;
}

int main(void) {
   int i, k, levels;

/* Get the number of levels to draw from the user */
   levels = GetLevels();

/*
* loop from 1 to levels (inclusive)
*
* starts at 1 for use in inner calculations, otherwise on the first
* iteration 0 * 2 = 0, which would be wrong, wrong, wrong!
*/
   for (k = 1; k <= levels; k++) {
   /*
    * print out the current level
    * "field width" of 2 to pad out numbers < 10 (single digit numbers :-P)
    */
       printf("%2.0d ", k);

   /*
    * from k to levels - 1 prints spaces, and there should be ((k * 2) - 1) 
    * stars per iteration. < instead of <= takes care of one of the -1's;-)
    *
    * iteration 3 out of 3 levels:
    * (i = 3; i < 3 + (3 * 2) - 1; i++)
    * (i = 3; i < (3 + 6) - 1; i++)
    * (i = 3; i < 8; i++)
    * loop 3 to 7. i = 3 is equal to levels (3) which means
    * print out 5 stars (3,4,5,6,7)
    */
       for (i = k; i < (levels + (k * 2) - 1); i++) {
       /*
        * if i is bigger than or equal to levels (i >= levels == true) it's
        * time to start printing stars, otherwise keep printing spaces.
        */
           (i >= levels) ? printf("*") : printf(" ");
       }

       printf("\n");
   }

   system("pause");
}

 

Welldone to both of you though! I think especially to Archerzz because, well, I think he's still quite new to C/++? :)

 

5 points to DEC I guess :)

 

Oh and veggiemaster did gimme a java bit of code, but it was a little bare :P It worked a long the same lines as archerzz's but was a fixed amount of levels (10).

Share this post


Link to post
Share on other sites

Here is the code and exe to mine if anyone wants to take a look at it.

 

Stars.zip

 

//Created by: Dan Cleinmark on 8/19/2005
//
//This program is designed to output a certain number of levels of centered *'s
//based on the number of levels the user inputs

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <math.h>

int levels, level, spaces, stars;
char repeat = 'y';
bool exitloop = false;

using namespace std;

int main(int argc, char *argv[]){
   
   
while (repeat == 'y' || repeat == 'Y'){
     exitloop = false;
     
   //Get number of levels
   while(levels < 1 || levels > 25){     //while loop to force input of 1-25
       cout<<"How many levels (1-25) in your pyramid?"<<endl;
       cin>>levels;
       cout<<endl;
       
       levels=int(abs(levels));
       
       if (levels < 1 || levels > 25){
       cout<<"You entered an invalid number, please try again."<<endl<<endl;
       }
   }
   
   spaces = levels;
   
   //Output
   for (int i = 1; i <= levels; i++){
       stars = (i * 2)-1;  
       
       //Level number
       if(i < 10){
       cout<<"0";
       }
       cout<<i<<":";
        
       //Output spaces
       for (int j = spaces; j > 0; j--){
       cout<<" ";
       }
       
       //Stars
       for (int k =1; k<=stars; k++){
       cout<<"*";
       }
       cout<<endl;
       spaces--;
       cout<<endl;
   }
   
   //Reset variables for next time around
   levels=0, level=0, spaces=0, stars=0;
   
   //Ask user if they wish to repeat
   while (exitloop != true){
       cout<<"Do you wish to make another pyramid? (y or n)"<<endl;
       cin>>repeat;
       cout<<endl;
       if(repeat == 'y' || repeat == 'Y' || repeat == 'n' || repeat == 'N'){
       exitloop = true;
       }
       else{
       cout<<"You entered an invalid choice, please enter 'y' or 'n'"<<endl<<endl;
       }
   }
   system("cls");         //Looks good :)
}
   
   cout<<"Thanks for playing.  Press any key to exit.";
   getch();
   return EXIT_SUCCESS;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×