Outputting a Square Shape C++ using '*' - c++

Hello I am very new to programming and my assignment is to output shapes. The first is the square:
int main(){
unsigned size;
cout <<"Size: ? ";
cin >>size;
for ( unsigned r = 0; r < size; r++ ){ // Square
for ( unsigned c = 0; c < size ; c++ )
if ( r == c )
cout <<'*';
cout <<endl;
}
cout <<endl;
}
When I input "5" after being prompted. The output results in:
5
*
*
*
*
*
Can anyone explain what is wrong with my code? I need to have both horizontal and vertical outputs. Thank you

You are only outputting a * on the diagonal, when r is the same as c. And you output nothing else but some endlines, so you end up with just a single star on each line.

#include <iostream>
using namespace std;
int main(){
unsigned size;
bool solid = true; //solid or hollow shape?
cout <<"Size: ? ";
cin >>size;
size = 5;
cout << endl;
for ( unsigned r = 0; r < size; r++ ){ // Square
for ( unsigned c = 0; c < size ; c++ ){
if(solid){
cout << " * ";
}
else{
if(r == 0 || r == size-1 || c == 0 or c == size-1){
cout << " * ";
}
else{
cout << " ";
}
}
}
cout <<endl;
}
cout <<endl;
}
Output Hollow:
* * * * *
* *
* *
* *
* * * * *
Solid:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Only time the * is printed is when r == c. What's the purpose of the if statement?
Try commenting the if statement and see the result.

Related

Print recursive * pattern

I have trouble printing the * pattern.
They should have 2 functions: printStars() and printLine(). printStars(int n), is used to print a line of n stars; the second one, printLines(int m), is to print m pairs of lines.
I have completed print half top but I could not reverse the pattern of the second half. You can't add more functions. There are only 2 functions printStars() and printLines() and must be recursive
The requirement:
* * * *
* * *
* *
*
*
* *
* * *
* * * *
Here is what I have done:
* * * *
* * *
* *
*
Code:
void printStars(int n){
if (n < 1) return;
cout << "*";
printStars(n-1);
}
void printLines(int m){
if (m < 1) return;
printStars(m);
cout << "\n";
printLines(m-1);
}
int main(int argc, const char * argv[]) {
int n;
cout << "n = ";
cin >> n;
printLines(n);
}
Hint from the question: Think in the way that the whole picture is generated in the following pattern:
here is the small change you need:
void printLines(int m){
if (m < 1) return;
printStars(m);
cout << "\n";
printLines(m-1);
printStars(m); // just add these two lines to print line again after all internal ones are printed
cout << "\n";
}

Modify to have a configurable number of stars "*"

I manage to print out the output for the pattern of stars I wanted but now I want to have a configurable number of stars "*" and automatically calculated corresponding number of rows.
I have tried several ways but the output seems to be off. If there is better way to display the output please guide me.
int i, j, k,l;
k = 1;
l = 11;
for (i = 0; i < 6; i++)
{
for (j = 0; j < k ; j++)
{
cout << "* ";
}
cout << "- ";
k += 2;
for (j = 0; j < l; j++)
{
cout << "* ";
}
l -= 2;
cout << endl;
}
* - * * * * * * * * * * *
* * * - * * * * * * * * *
* * * * * - * * * * * * *
* * * * * * * - * * * * *
* * * * * * * * * - * * *
* * * * * * * * * * * - *
If the number of rows is given, then the resulting number of columns can easily be calculated. Look at your pattern then you will see that the number of necessary coulmns is number of rows times 2 + 1.
Here one possible solution:
#include <iostream>
int main()
{
std::cout << "Enter the number of rows for the pattern: ";
unsigned int numberOfRows{ 0 };
std::cin >> numberOfRows;
// Number of columns is always number of rows * 2 + 1
unsigned int numberOfColumns{ numberOfRows * 2 + 1 };
unsigned int positionOfDash{ 1 };
// Print the pattern
for (unsigned int row = 0; row < numberOfRows; ++row) {
for (unsigned int col = 0; col < numberOfColumns; ++col) {
// Output dash in desired column or else star
std::cout << (col == positionOfDash ? '-' : '*') << ' ';
}
positionOfDash += 2;
std::cout << '\n';
}
return 0;
}
Please note: Of course there are tons of other possible solutions . . .

C++ Star Pattern Pyramid

I'm trying to print the following pattern:
*
* *
* * *
* *
*
.. but I can't figure it out.
This is the logic that I am following.
i variable is for the row number.
j variable is for the column number.
Using for loops to guide the row number and column number.
I am able to create an increasing triangle pattern using the above logic but can't figure out how to start decreasing the pattern to form a pyramid.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int i;
int j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j < i; j++)
{
cout << "*";
}
cout << endl;
for (i; i <= 6; i++)
{
for (j; j <= 0; j--)
{
cout << "*";
}
cout << endl;
}
}
return 0;
}
I'd really appreciate some guidance to this.
for (i; i <= 6; i++)
It has no effect to mention a variable (i) in the init-statement of a for loop if there is wheter a declaration nor an assignment.
#include <iostream>
int main()
{
int width = 8;
// raising flank:
for (int i = 0; i < width; ++i) {
for (int k = 0; k <= i; ++k) {
std::cout << "* ";
}
std::cout.put('\n');
}
// falling flank:
for (int i = width - 1; i; --i) {
for (int k = 0; k < i; ++k) {
std::cout << "* ";
}
std::cout.put('\n');
}
}
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

C++ asterisk loop program with space [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I wanted to make:
*
* *
* * *
* * * *
* * * * *
* * * * * *
but I don't know how to make the spacing, the closest I can get is by using:
#include <iostream>
#include <conio.h>
using namespace std;
int main(int argc, char** argv) {
int x, y;
for (y = 0 ; y <= 5 ; y ++){
for (x = 0 ; x < y ; x++) {
cout<<" * ";
}
printf("\n");
}
getch ();
return 0;
}
I'll help you.. but just because it's almost Christmas
int x, y;
for (y = 0; y <= 5; y++) {
for (x = 0; x < y; x++) {
for (int i = 0; x == 0 && i < (5 - y); ++i)
cout << ' ';
cout << " *";
}
cout << '\n';
}
Example
You start with 6 spaces and an asterix.
Next is 5 spaces and an asterix, followed by 1 space+asterix
Next is 4 spaces and an asterix, followed by 2 space+asterix
... and so on. Do you see the pattern?
#include <iostream>
int main(int argc, char* argv[])
{
for (int height = 6; height > 0; --height)
{
// Leading spaces
for (int i = 1; i < height; ++i)
{
std::cout << ' ';
}
// and the asterix
std::cout << '*';
// then trailing space+asterix
for (int i = height; i < 6; ++i)
{
std::cout << " *";
}
std::cout << std::endl;
}
}
Just output one space after the outputted asterisk
Here you are.
#include <iostream>
#include <iomanip>
int main()
{
while (true)
{
const char asterisk = '*';
std::cout << "Enter a non-negative number (0 - exit): ";
unsigned int n;
if (not (std::cin >> n) or n == 0) break;
std::cout << '\n';
for ( unsigned int i = 0; i < n; i++ )
{
std::cout << std::setw( n - i + 1 );
for (unsigned int j = 0; j < i + 1; j++)
{
std::cout << asterisk << (j == i ? '\n' : ' ');
}
}
std::cout << std::endl;
}
return 0;
}
The program output might look like
Enter a non-negative number (0 - exit): 6
*
* *
* * *
* * * *
* * * * *
* * * * * *
Enter a non-negative number (0 - exit): 5
*
* *
* * *
* * * *
* * * * *
Enter a non-negative number (0 - exit): 4
*
* *
* * *
* * * *
Enter a non-negative number (0 - exit): 3
*
* *
* * *
Enter a non-negative number (0 - exit): 2
*
* *
Enter a non-negative number (0 - exit): 1
*
Enter a non-negative number (0 - exit): 0

"return" isn't returning the correct value from an array function

I am trying to set the price of each array value by row.
The price of each row has to be entered every time the program starts.
the program complies but the output is incorrect, here is what is displayed:
Please enter price for row 0 = 66
please enter row number 0
please enter seat number 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
---------------------------------------------
# * * * * * * * * * * * * * * |0
* * * * * * * * * * * * * * * |1
* * * * * * * * * * * * * * * |2
* * * * * * * * * * * * * * * |3
* * * * * * * * * * * * * * * |4
* * * * * * * * * * * * * * * |5
* * * * * * * * * * * * * * * |6
* * * * * * * * * * * * * * * |7
* * * * * * * * * * * * * * * |8
* * * * * * * * * * * * * * * |9
-858993460
Press any key to continue . . .
what is incorrect is "-858993460", it should display 66. Someone help me
my code looks like
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
void drawgrid(int ai[10][15], int ji, int ii); // draw grid proto
int numb(int num[10], int tps)
// this function returns back the array possition
// that matchest "tps" ' number
{
return num[0];
}
void numbee(int nu[10])
// this function prompts the user for the price of each row
{
for (int i = 0; i < 10; i++)
{
cout << " Please enter price for row " << i << endl;
cin >> nu[i];
}
return;
}
int printArray(int a[10][15],int tp,int pp)
// this function asks the user what seat to buy
{
int num[10];
cout << "please enter row number ";
cin >> tp;
cout << "please enter seat number";
cin >> pp;
a[tp][pp] = numb(num, tp);
return 0;
}
int main() // this is the main
// calls numbee, drawgrid, and printArray
// sets all to 0 using memset
{
int n[10];
numbee(n);
int love;
int a[10][15];
int i = 0, j = 0;
memset(a, 0, sizeof(a[10][15]) * 10 * 15); // set everything in gridto 0
drawgrid(a, i, j);
love = printArray(a, i, j);
numb(n, love);
drawgrid(a, i, j);
cout << a[0][0];
system("PAUSE");
return 0;
}
void drawgrid(int ai[10][15], int ji, int ii)
// this function draws the gridd
{
ii = 0; ji = 0;
cout << "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ";
std::cout << std::endl;
cout << "---------------------------------------------";
std::cout << std::endl;
for (int ii = 0; ii < 10; ii++) //This loops on the rows.
{
for (int ji = 0; ji < 15; ji++) //This loops on the columns
{
if (ai[ii][ji] == 0)
{
cout << "*" << " ";
}
else
{
cout << "#" << " ";
}
}
cout << "|" << ii;
cout << endl;
}
}
In this function:
int printArray(int a[10][15],int tp,int pp)
// this function asks the user what seat to buy
{
int num[10];
cout << "please enter row number ";
cin >> tp;
cout << "please enter seat number";
cin >> pp;
a[tp][pp] = numb(num, tp);
return 0;
}
you delcare num[10] and you left it uninitialized. Then in this line:
a[tp][pp] = numb(num, tp);
through a function:
int numb(int num[10], int tps)
// this function returns back the array possition
// that matchest "tps" ' number
{
return num[0];
}
you write uninitialized num[0] into a[0][0]. That's why you get garbage.
Your code is quite confusing because of the naming - "numb" and "numbee" are nonsense, and the function that asks for a seat is called "printArray", but doesn't ever print an array. You should probably work on your naming a bit.
On to the problem:
printArray has a local array variable, num.
This array is uninitialized, so can contain any random data.
You then assign the value of the first element of this array (returned from numb(num, tp)) to a[tp][pp];, and this is where the strange data is coming from.
It's difficult to advise on a remedy because it's a bit unclear what you intend for numb and code such as
love = printArray(a, i, j);
numb(n, love);
to accomplish.