Jump to content
Sign in to follow this  
Kamikaze_Badger

Meditations On Printing Character Arrays and Loops

Recommended Posts

So, way back when, I was challenged with the basic task to write a program that had an output like so:

 

  *   
 *** 
***** 
*******

 

Tonight I revisited my poor knowledge of C++ and came up with the following code. I warn you, it may be so terribly written as to cause serious mental trauma.

 

#include <iostream>

using namespace std;

int prevSpaces = 2; //Number of the space before the star
int postSpaces = 4; //Number of the space after the star
int prevStars; //Used to set pyramid array value to "*"
int postStars;

char pyramid[] = { ' ', ' ', ' ', '*' , ' ', ' ', ' ', '/0'};

int main() {
   for(int i = 3; i > 0; i--) {
             pyramid[prevSpaces] = ' ';
             pyramid[postSpaces] = ' ';

             prevStars = prevSpaces;
             postStars = postSpaces;

             pyramid[prevStars] = '*';
             pyramid[postStars] = '*';
             prevSpaces--;
             postSpaces++;

             cout << pyramid << endl; };
   system("pause");
             return(0);
             };

 

Right now, I'm getting an output of this:

 

  ***  0
*****  0
*******0

 

I come to those of you with a better grasp on the language than I, as I'm trying to figure out how to have null not display, and get those top two levels in there. Thanks in advance for assistance.

Share this post


Link to post
Share on other sites

This is how I would do it. I wouldn't bother with c-strings, I think it's just easier to modify the counters and indexes.

 

#include <iostream>
using namespace std;

int main()
{
 int spacesNum = 3;
 int starsNum = 0;

 for (int i = 1; i <= 4; i ++)
 {
   for(int j = spacesNum; j > 0; j--) //print spaces
     cout<<" ";
   for(int k = 0; k < 1 + (2*starsNum); k++) //print stars
     cout<<"*";
   cout<<endl;
   spacesNum--;
   starsNum++;
 }

 return 0;
}

 

I don't have access to a compiler right now so you'll have to tell if it works or not.

Share this post


Link to post
Share on other sites

About those "0"s the syntax is

"\0"

.

Edited by CowKing

Share this post


Link to post
Share on other sites

If you want to do it with c-strings then I think this is probably the best way.

 

#include <iostream>
using namespace std;

int prevSpaces = 2; //Number of the space before the star
int postSpaces = 4; //Number of the space after the star
int prevStars; //Used to set pyramid array value to "*"
int postStars;
char pyramid[] = { ' ', ' ', ' ', '*' , ' ', ' ', ' ', '\0'};

int main() 
{
 for(int i = 1; i <= 4; i++) {  //You don't use i in any way so why bother making the for complicated
                                //your old loop only ran three times; see what I mean?
   //pyramid[prevSpaces] = ' '; 
   //pyramid[postSpaces] = ' ';  these are unneccessary

   cout << pyramid << endl; //you need to print pyramid before you modify it

   prevStars = prevSpaces; 
   postStars = postSpaces;

   pyramid[prevSpaces] = '*';
   pyramid[postStars] = '*';
   prevSpaces--;
   postSpaces++;
 }
 return(0);
}

Share this post


Link to post
Share on other sites

After thinking about what you said about loops, I have the following code. It compiles and runs, but it's not printing the characters. Any idea as to why? Also, using functions to keep the code in main a little tidier.

 

EDIT: I'm an idiot. I was terminating the while loop before it even got a chance to start. Updated code below. Now just working on fixing up the lopsided pyramid.

EDIT: Excuse my while I go facepalm. userInput - 1 was all I needed, not /2.

#include <iostream>

using namespace std;

int i; //First loop counter
int j; //Second loop counter
int h; //Third loop counter
int userInput = 0;
int numSpaces = 0;
int numStars = 0;
char space = ' ';
char star = '*';

void funcDrawSpaces ();
void funcDrawStars ();


void funcDrawSpaces () {
   for(i = numSpaces; i>0; --i) {
         cout << " ";
         }
         }

void funcDrawStars () {
   for(j = numStars; j>0; j--) {
         cout << "*";
         }
         }

int main () {
   cout << "Enter number of levels: ";
   cin >> userInput;
   numSpaces = (userInput-1); //Provided that user inputs an odd number, this will work
   numStars = 1;
   cout << "\n";
   while(userInput > 0) {
         funcDrawSpaces();
         funcDrawStars();
         funcDrawSpaces();
         numSpaces--;
         numStars++;
         numStars++;
         cout << endl;
         userInput--;
         }
   system("pause");
   return 0;
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...