Total members 11893 |It is currently Wed Nov 13, 2024 2:34 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





make a pyramid of characters based on input from user
cpp code
// Program to make a pyramid of characters based on input from user
// Purpose is to examine use of <iomanip.h> and how setw is used
// Created by Gary Paduana
// email : [email protected]
// Written on 4.15.01

# include <iostream.h>
# include <iomanip.h> // library that has the setw output manipulator

int main ()
{

char letter; // letter is the symbol or letter made into a giant triangle
int width; // width is how far to go into the center of screen
int base; // base is how many symbols are on bottom line
int a; // a is how many lines down the triangle is
int b = 1; // b is how many symbols are displayed on each line
int counter = 0; // counter is how many times the loop executed

cout<<"This program will make a triangle of the symbol entered."<<endl;
cout<<"It must be an odd number for the triangle to form properly."<<endl;
cout<<"If an even number is entered, it will be lowered to the previous odd."<<endl;


while(cin)// This allows the program to loop until the user closes the window
{

cout<<"Enter a symbol or character."<<endl;
cin>>letter;

cout<<"Enter the number of characters the base should have."<<endl;

cin>>base;
// This is how far into the center it should space until it starts outputting the symbol
width = (base / 2) + 5 - counter; // It must first be given a value before it enters the loop
a = 1;// a is how many lines down the triangle is, and naturally it starts on the first line
// It will loop and continue to output the symbol until it reaches 5 spaces from the left margin...
while(width > 5) // so that everything isn't jammed against the side
{
// This is how far into the center it should space until it starts outputting the symbol
width = (base / 2) + 5 - counter;
// setw is an output manipulator in the <iomanip.h> library. this tell the compiler how many lines
cout<<setw(width);
// to move until it first sends a character to the screen. It is currently set to move the value of
// "width" spaces to the right before it outputs.
// This while loop will continue to output the desired symbol to the current line until it is equal to 1
while(b > 0)
{
// outputs the letter or symbol entered
cout<<letter;
b--; // b is decremented so only so many letters are outputted per line
}
// an endl is used to jump to the next line to output the next part of the triangle
cout<<endl;
// the number of symbols per line is found using this equation
b = (a * 2) - 1;
// the width is decremented so that everything is spaced properly
width--;
// b is given 2 more symbols because it is on the next line, and every line has 2 more than the previous
b = b + 2;
a++; // this is how many lines into the triangle it is
counter++; // the counter is used to ensure proper spacing done by the width variable
}
// endl is used to add some space between each time the program is executed
cout<<endl<<endl;
b = 1; // b is returned to 1 because the program started over
counter = 0; // counter is returned to 0 because the program started over
}
return 0;
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Program to make a pyramid of characters based on input
 URL Characters and URL Encoding Values     -  
 Special Characters literals     -  
 Unicode, ASCII, UTF-16, and UTF-8 characters in java     -  
 Number of Occurrences of characters in a string     -  
 How to remove special characters from a string in Java?     -  
 continuously calculates the number of characters existing     -  
 Input-Output Operations     -  
 Only numeric input in JTextField     -  
 getting a full line input     -  
 input suggestion using Javascript     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com