(C++) Input specific pattern accoring to user input - c++

I have a line of code that when inputted 3, the result will print out a series of dashes and asterisks to form a diamond:
Expected Input:
3
Expected Output:
--*--
-***-
*****
-***-
--*--
what i have so far is the triangle but I can' seem to get rid of the middle line to make it a full diamond shape. Also "-" is not printing on the right side of the bottom half
this is the code I have made
int n;
cin >> n;
for (int left_stars = 0; left_stars < n; left_stars++) {
for (int column = 0; column < 2 * n - 1; column++) {
int first_star = n - 1 - left_stars;
int last_star = n - 1 + left_stars;
if (column < first_star || column > last_star) {
cout << "-";
} else {
cout << "*";
}
}
cout << endl;
}
for(int i = n; i >= 1; --i) {
for(int space = 0; space < n-i; ++space) {
cout << "-";
}
for(int j = i; j <= 2*i-1; ++j) {
cout << "*";
}
for(int j = 0; j < i-1; ++j) {
cout << "*";
}
cout << endl;
}
return 0;

For removing the double full star line use following code line
for (int i = n-1; i >= 1; --i) {
instead of
for(int i = n; i >= 1; --i) {
(side note: maybe you want to check as suggested by yaodav if you could not write the second part like the first part).

Related

How do this program but in reverse, pattern

so i want output like this
1
123
12345
123
1
i already make the program but it only output these, and im confused how to output the bottom triangle
1
123
12345
here's my program
#include <iostream>
using namespace std;
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
#Mojtaba's answer is a perffect extension to your approach.
However, I wanted to provide another method that is generally used in creating such strings that are formatted in a particular manner. It is common to create the entire pattern line by line and then print to the console all at once.
I have appropriately commented the code for your reference and it should be easy to understand:
#include <iostream>
#include <vector>
void pattern(int n) {
std::vector<std::string> lines; // store the first n lines to print later
int length = 2*n - 1; // length of each line
for(int i = 0; i < n; i++) {
std::string str = std::string(length, ' ');
for(int j = 1; j <= 2*i + 1; j++) {
str[n - i + j - 2] = j + '0';
// indexing can be figured by observing the pattern
}
lines.emplace_back(str);
}
for(int i = 0; i < n; i++) {
std::cout << lines[i] << std::endl;
}
for(int i = n-2; i >= 0; i--) {
std::cout << lines[i] << std::endl;
}
return;
}
int main() {
int n;
std::cin >> n;
pattern(n);
}
I added another for loop exactly like yours with different order from n-1. I modified your code to this:
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
for (i = n - 1; i >= 1; i--) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
Now it returns:
1
123
12345
123
1

print two diamonds side by side

I wanna print two diamonds side by side, but my code prints 1 diamond.
I spent lots of time on it and really do not know what else to do.
Any help would be appreciated.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;
middle = (n - 1) / 2;
for ( i = 0; i < n; i++)
{
spaceCount = abs(middle - i);
starCount = n - 2 * abs(middle - i);
for ( j = 0; j < spaceCount; j++)
cout << " ";
for (j = 0; j < starCount; j++)
cout << "*";
for (j = 0; j < spaceCount; j++)
cout << " ";
cout << endl;
}
}
input = odd numbers
desired output =
* *
*** ***
**********
*** ***
* *
You forgot to print you second diamond shape. Each iteration, you should print first a few spaces, then the stars, then the double of the spaces (to finish the first diamond and set spaces for the second diamond) and then you should draw you stars for your second diamond.
This is an example of code that prints two diamonds:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;
middle = (n - 1) / 2;
for ( i = 0; i < n; i++)
{
spaceCount = abs(middle - i);
starCount = n - 2 * abs(middle - i);
// print a row of the first diamonds
// spaces at the beginning
for ( j = 0; j < spaceCount; j++)
cout << " ";
// the stars itself
for (j = 0; j < starCount; j++)
cout << "*";
// finish the rectangle of the first diamond
for (j = 0; j < spaceCount; j++)
cout << " ";
// print a row of the second diamond
// spaces at the beginning
for (j = 0; j < spaceCount; j++)
cout << " ";
// the stars itself
for (j = 0; j < starCount; j++)
cout << "*";
// spaces at the end are not necessarily required for the last diamond
cout << endl;
}
}
It would be even better to create a function to print one diamond (row) and call this function two times (this prevents duplicate code).

image made from dots and letters c++

very basic Q as I just started with coding but I stuck at some point and have 0 ideas what to do.
I need to write code to get diamond shape made from dots and X letters, size based on a value (n) provided by the user, (3 ≤ n ≤ 80).
for example:
As I mentioned - I have almost 0 experience so all I could get is is this shape for n=6
height is ok, same as widht but unfortunately, the amount of X's and placement isn't correct :/
my code:
int h;
cerr << "Provide size of diamond: ";
cin >> h;
for (int i = 1; i <= h; i++)
{
for (int k = 1 ; k <= h-i ; k++)
{
cout << ".";
}
for (int j = 1; j <= i ; j++)
{
cout << "X";
}
cout << endl;
Thank you all good people who will help mi with this one :)
I help to draw points. I hope you, looking on my change, are able to update your code further to achive the required picture.
for (int i = 1; i <= h; i++)
{
for (int k = 1 ; k <= (h-i) / 2 ; k++)
{
cout << ".";
}
for (int j = 1; j <= i ; j++)
{
cout << "X";
}
for (int k = 1 ; k <= (h-i) / 2 ; k++)
{
cout << ".";
}
cout << endl;
}
In this kind of problems you can divide the problems in different parts. Such as for n=6 the image can be divided in 4 mirror images:
..X
.XX
XXX
then,
X..
XX.
XXX
and upside down mirror of them.
You said that you can draw the first one. I think if you give some more time you will be able to print the full image too.
But, if you have problems, here's the code for that
for (int i = 1; i <= h; i++) {
if((h-i)%2) continue;
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
for (int j = 1; j <= i ; j++) {
cout << "X";
}
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
cout << endl;
}
for (int i = (h/2)*2; i > 0; i--) {
if((h-i)%2) continue;
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
for (int j = 1; j <= i ; j++) {
cout << "X";
}
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
cout << endl;
}
Since this is tagged as a c++ question let's use std::string and three loops.
#include <iostream>
#include <string>
void print_diamond(int n)
{
int np = n / 2, nm = (n - 1) / 2;
int npl = np, nml = nm;
std::string str(n, '.');
for (int i = 0; i < nm; i++)
{
str[npl++] = 'X'; str[nml--] = 'X';
std::cout << str << std::endl;
}
for (int i = nm; i <= np; i++)
{
str[npl] = 'X'; str[nml] = 'X';
std::cout << str << std::endl;
}
for (int i = np; i < n - 1; i++)
{
str[npl--] = '.'; str[nml++] = '.';
std::cout << str << std::endl;
}
std::cout << std::endl;
}
Print all diamond for a shinier world...
int main()
{
for (int n = 3; n < 81; n++)
{
print_diamond(n);
}
}

How do i add all the values in my ascending array?

First i need to re-arrange all the values of my array into ascending order then add it afterwards. For example the user input 9 2 6, it will display in ascending order first ( 2 6 9 ) before it will add the sum 2 8 17.. The problem is my ascending order is not working, is there something wrong in my code?
#include <iostream>
#include<conio.h>
using namespace std;
int numberof_array, value[10], temp;
int i = 0, j;
void input()
{
cout << "Enter number of array:";
cin >> numberof_array;
for (i = 0; i < numberof_array; i++)
{
cout << "Enter value for array [" << i + 1 << "] - ";
cin >> value[i];
cout << endl;
}
}
void computation()
{
// this is where i'll put all the computation
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
for (i = 0; i <= numberof_array; i++)
{
for (j = 0; j <= numberof_array - i; j++)
{
if (value[j] > value[j + 1])
{
temp = value[j];
value[j] = value[j + 1];
value[j + 1] = temp;
}
}
}
}
void display()
{
// display all the computation i've got
cout << "\nData after sorting: ";
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
getch();
}
int main()
{
input();
computation();
display();
}
void computation(){
for (int j = 0; j < numberof_array; j++) cout << value[j]<<"\t";
for (int i = 0; i <= numberof_array; i++) {
temp = value[i];
int temp_idx = i;
for (int j = i; j < numberof_array; j++) {
if (value[j] < temp) {
temp = value[j];
temp_idx = j;
}
}
int temp_swap = value[i];
value[i] = value[temp_idx];
value[temp_idx] = temp_swap;
}
}
How about changing your second function to something like above.
I have to agree with other commentators that your coding style is not preferred but there might be more to the story than meets the eye.

why doesn't this replace the 'Xs' with space's

int mapSizeX = 30;
int mapSizeY = 10;
string map[10][30];
char playerMovement;
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
map[i][j]= "X";
cout << map[i][j];
}
cout << endl;
}
for (int i = 1; i < mapSizeY - 1; i++)
{
for (int j = 1; j < mapSizeX - 1; j++)
{
map[i][j] = " ";
cout << map[i][j];
}
cout << endl;
}
this for some reason doesn't replace the X's with spaces and adds it after the X's and doesn't make a "arena", this is for a snake game by the way
You're outputting to cout twice. Remove both cout << map[i][j]; from the loops and output only when you are done editing the string:
... // Previous code without printing
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
cout << map[i][j];
}
cout << endl;
}
Try adding the following to see your arena:
cout << endl << endl;
cout << "The arena: " << endl << endl;
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
cout << map[i][j];
}
cout << endl;
}
After you printed the entire arena with X's, you then print the entire arena with white spaces just one tile shorter on each side. So you're printing them after eachother. What you wanna do instead is to print them in the same nested for loop, like this:
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
if (i == 0 || j == 0 || i == mapSizeY - 1 || j == mapSizeX - 1) {
map[i][j] = "X";
cout << map[i][j];
}
else {
map[i][j] = " ";
cout << map[i][j];
}
}
cout << endl;
}
The if-clause checks if the current iteration is an edge (if its 0 on X or Y axis or if its the last element in the array on X or Y axis) and prints the X, if its not (its in the field) it prints a blank space.
Little tip I wanna give you: I would initialize the field straight away with the characters without printing it.
So initialize the field like this:
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
if (i == 0 || j == 0 || i == mapSizeY - 1 || j == mapSizeX - 1) {
map[i][j] = "X";
}
else {
map[i][j] = " ";
}
}
}
and then after you've done the equations on the snake, food and stuff and written that all into you ´map´ vairable, go and print the whole map like this:
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
cout << map[i][j];
}
cout << endl;
}
Hope I could help!