I want to display an inverted half-star pyramid pattern next to each other.Here's the desired output
And here's my code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter Number of Rows: ";
cin >> n;
for (x = n; x >= 1; x--)
{
for (y = 1; y <= x; y++)
{
if (y <= x)
cout << "*";
else
cout << " ";
}
for (y = n; y >= 1; y--)
{
if (y <= x)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Here's the output I got after running the code.
The number of rows desired is 10.
After running my code, the output isn't like what I expected. Please tell me how to make it right.
Thank you.
I saw some symmetries in the problem
for n rows, we're printing 2*n+1 characters
for the yth row, we're printing an asterisk if x is less than n-y or more than n+y
So I coded a single double loop with the more complex if statement. I had to adjusted the if statement until it worked.
#include <iostream>
using namespace std;
int main()
{
int n, x, y;
cout << "Enter Number of Rows: ";
cin >> n;
for (y = 0; y < n; y++)
{
for (x = 2*n+1; x > 0; x--)
{
if ((x > n+y+1) || (x < n-y+1))
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Well, you need to change your logic a little bit rest all is fine.
Here is the code:-
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter the number of Rows: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(y = n; y >= 1; y--)
{
if(y <= x)
cout << " ";
else
cout << "*";
}
for(y = 1; y <= n; y++)
{
if(y <= x)
cout << " ";
else
cout << "*";
}
cout << "\n";
}
return 0;
}
Explanation:- I am starting from row 1 and going till row "n". Now I need to print two different inverted flags so I used two for loops for that. In one loop I am starting from column "n" and going till row >1 and in the other loop, I am doing the just opposite of that so that both flags will be opposite to each other. Just try to understand this code by taking X as row and Y as column.
Related
I previously asked for help with a school assignment after getting help from several of the good people of stack overflow I've hit another snag. For some reason my program wont print an odd number of * to the console so I get a miss shaped diamond. Would anyone have any ideas on how I can fix that issue?
Here is my code:
using namespace std;
int x;
int y;
int rowNum;
int main (int argc, char *argv[]){
cout << " Enter the Numbers of rows you would like between 3 and 15: " << endl;
cin >> rowNum;
if((3 <= rowNum)&&( rowNum<= 15))
{
for (x = 1; x <= rowNum; x++) //outer loop
{
for (y = 1; y <= x; y++) {
if (y <= (rowNum - x))
cout << " ";
else
cout << "*";
}
cout << endl;
}
for (x = rowNum; x >= 1; x--) //outer loop
{
for (y = 1; y <= x; y++)
{
if (y <= (rowNum - x))
cout << " ";
else
cout << "*";
}
cout << endl;
}
}
else{
cout << "invalid number try again";
}
return 0;
}
I was trying to write a code that takes two numbers as a input and change the numbers into words in certain rules.
Below is the code I wrote at first, but whatever input I put in, the loop starts from x=0.
#include <iostream>
int main() {
string nums[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int x,y;
cin >> x;
cin >> y;
for (int x = x; x <= y; x++){
if (x <= 9){
cout << nums[x] << "\n";
}
else if (x % 2 == 0){
cout << "even" << "\n";
}
else {
cout << "odd" <<"\n";
}
}
return 0;
}
Below is the second code I wrote and it worked as I wanted to.
#include <iostream>
int main() {
string nums[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int x,y;
cin >> x;
cin >> y;
for (int i = x; i <= y; i++){
if (i <= 9){
cout << nums[i] << "\n";
}
else if (i % 2 == 0){
cout << "even" << "\n";
}
else {
cout << "odd" <<"\n";
}
}
return 0;
}
I found out that if I add a variable, it works as what I wanted too. I found the solution but I don't know why I have to add a variable and why the first one always starts from x=0.
In first sample that you provided you defined a local variable(x) as same name in outer block:
for (int x = x; x <= y; x++)
In fact shadowing of variable take place here and c++ hide the declaration of variable of outer block with same name in nested block.
Can't figure out how to generate coordinates(x,y) entered by the user and find the closest genareted point to the one that the user has entered.
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>//for generating a random number
using namespace std;
int main()
{
int x,k,y,quad,x1,y1;// the variables that we are going to use
cout << "Enter the value of x : "<<endl;
cin >>x;
cout << "Enter the value of y : "<<endl;
cin >>y;
cout <<"Enter the amount of numbers you want to be generated : "<<endl;
cin >>k;//how many coordinates to be generated
srand(time(0));//to make sure that the numbers are random
for(int i = 0; i < k; i++){
int x = rand() % k;//generate x
int y = rand() % k;//generate y
x1 = x;
y1 = y;
cout << "(" << x << "," << y <<")"<< "\n\n";
}
if (((x1*x1)+(y1*y1))<(x*x)+(y*y)){
cout <<"("<<x1<<","<<y1<<") is the nearest point to ("<<x<<","<<y<<")"<<endl;
}
if (x<0 && y<0){//these are to show in which quadrant are the coordinates
quad = 3;
}
else if(x<0 &&y>0){
quad = 2;
}
else if(x>0 && y>0){
quad = 1;
}
else if(x>0 && y<0){
quad = 4;
}
else if(x<0 && y==0){
quad = 5;
}
else if(y<0 && x==0){
quad = 6;
}
else if(x>0 && y==0){
quad = 7;
}
else if(y>0 && x==0){
quad = 8;
}
switch(quad){
case 1: cout <<"("<<x<<","<<y<<")"<<"are in the 1st quadrant"<<endl;break;
case 2: cout <<"("<<x<<","<<y<<")"<<"are in the 2nd quadrant"<<endl;break;
case 3: cout <<"("<<x<<","<<y<<")"<<"are in the 3rd quadrant"<<endl;break;
case 4: cout <<"("<<x<<","<<y<<")"<<"are in the 4th quadrant"<<endl;break;
case 5: cout <<"("<<x<<","<<y<<")"<<"are between the 2nd and 3rd"<<endl;break;
case 6: cout <<"("<<x<<","<<y<<")"<<"are between the 3rd and 4th"<<endl;break;
case 7: cout <<"("<<x<<","<<y<<")"<<"are between the 1st and 4th"<<endl;break;
case 8: cout <<"("<<x<<","<<y<<")"<<"are between the 1st and 2nd"<<endl;break;
default : cout <<"("<<x<<","<<y<<")"<<"are in the (0,0)"<<endl;break;
}
}
I know that the solution is probably simple, but i just couldn't figure it out. I'm freshman in college and i feel pretty retarded
for asking questions like this, but this is my last hope.
Any help will be appreciated.^^
Please check this code I didn't use quadrants as you wanted to find closest point to the given point.
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>
using namespace std;
float dist(int x1, int y1, int x2, int y2) {
return sqrt(pow((x1-x2),2) + pow((y1-y2),2));
}
int main()
{
int x,y,k;
cout << "Enter the value of x : "<<endl;
cin >>x;
cout << "Enter the value of y : "<<endl;
cin >>y;
cout <<"Enter the amount of numbers you want to be generated : "<<endl;
cin >>k;//how many coordinates to be generated
//Define arrays for points,
int *X = new int[k];
int *Y = new int[k];
srand(time(0));//to make sure that the numbers are random
for(int i = 0; i < k; i++){
X[i] = rand() % k;
Y[i] = rand() % k;
cout << "(" << X[i] << "," << Y[i] <<")"<< "\n\n";
}
float minDist = dist(x,y,X[0],Y[0]);
int number = 0;
for( int i = 1; i < k; i ++ ) {
if( dist(x,y,X[i],Y[i]) < minDist ) {
minDist = dist(x,y,X[i],Y[i]);
number = i;
}
}
cout << "Nearest point (" << X[number] << "," << Y[number] << ")";
cout << "Distance is " << minDist;
return 0;
}
I'm kind of new to C++ so last night I thought of something. I want to print out numbers from 1-100 but with 10 numbers per line. I'm aware my code is below is wrong as it just prints 1-100 vertically. If anyone can shed some light to my question, it would be greatly appreciated. Thanks for reading :)
#include <iostream>
using namespace std;
int main() {
for(int x = 1; x <= 100; x++) {
cout << x << endl;
}
}
So you want to print 10 numbers, then a carriage return, and then 10 numbers, then a carriage return, and so on, correct?
If so, how about something like:
for(int x = 1; x <= 100; x++) {
cout << x << " ";
if ((x%10)==0) cout << endl;
}
Use the modulo operator % to determine if a number is a multiple of another:
for(int x = 1; x <= 100; x++) {
if( x % 10 == 0 ) cout << endl;
cout << x << " ";
}
How about
int main() {
for(int x = 1; x <= 100; x++) {
cout << x << " " ; //Add a space
if ( x % 10 == 0 ) {
cout << endl //Put out a new line after every 10th entry?
}
}
}
Print new line when it can be device by 10.
for(int x = 1; x <= 100; x++) {
cout << x << ",";
if ((x % 10) == 0) {
cout << endl;
}
}
for(int i=1; i<=100; i++) {
i%10==0 ? cout << i<<endl : cout<<i<<" ";
}
I am working on a program that emulates conways game of life, and it works perfectly with the preset dimensions. However, once i try to use the dynamic dimensions as seen in option e, i start having problems. The main problem is in the "life" function which iterates throughout the array and decides if it should bring to life a cell. I have been debugging for a while and it i enter the dimensions as 50*40, it iterates until 61, 1. This should technically work but it just breaks everytime. Keep in mind that I add 12 to each dimension to account for the buffer zone I put around the edges. Technically it should work then right? If you have any suggestions I would really appreciate it!
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <new> // i havent used this one yet
#include <cmath>
using namespace std;
// REMEMBER: The outside of the array is 6 more than what we show so that nothing interferes
//also that it goes y,x and that x is going to be bigger so that we get a rectange
//we use the copy function to copy an array from eachother, either the current one to the temp one or
//vise versa. This is so that we can alter the cells one step at a time without affecting everything else.
void copy(int **array1, int **array2, int o, int p)
{
for(int j = 0; j < o; j++)
{
for(int i = 0; i < p; i++)
array2[j][i] = array1[j][i];
}
} // the second array sent is assigned the first array sent!
//this array will initialize our arrays so that we can use them later
int** init(int n, int m)
{
int **array;
array = new int*[m]; // x
array = new int*[n]; // y
for (int q=0; q < n; q++)
{
array[q] = new int[n];
for (int w=0; w < m; w++)
{
array[w] = new int[m];
}
}
return array;
}
void populate(int o, int p, int** board){ // THIS FUNCTION HASN'T BEEN USED YET
for(int i=0; i < p; i++)
{
for(int j=0; j < o; j++) // It was in a in-class demo but i dont think i need it
{
board[i][j] = pow(i, j);
}
}
}
//The life function looks at the pieces around the cell and figures out what happens next.
// Probably the most important in the entire program, feast your eyes!
void life(int **array, int o, int p)
{
//Copies the main array to a temp array so changes can be made without affecting anyone else
int **temp;
temp = init(o, p);
copy(array, temp, o, p);
for(int j = 1; j < o; j++)
{
for(int i = 1; i < p; i++)
{
// checks all 8 cells surrounding it
int count = 0;
cout << " j is " << j << " and i is " << i << endl;
// cout << array[j][i]; // DEBUGGING
count =
array[j-1][i] + array[j-1][i-1] +
array[j][i-1] + array[j+1][i-1] +
array[j+1][i] + array[j+1][i+1] +
array[j][i+1] + array[j-1][i+1];
//cell dies.
if(count < 2 || count > 3)
{
temp[j][i] = 0;
}
//nothing happens.
if(count == 2)
{
temp[j][i] = array[j][i];
}
//now the cell will be born, or if it already is alive then it stays that way.
if(count == 3)
{
temp[j][i] = 1;
}
}
}
//Copies the temp array back to the main array.
copy(temp, array, o, p);
}
//This function prints the 40 x 50 part of the array, a 1 means that there will be a cell there,
//otherwise it will just be an empty space.
void print(int **array, int o, int p)
{
// WE ONLY CHECK WHAT WE SEE, WHICH IS 6 LESS THAN THE ARRAY!!!
for(int j = 6; (j < (o-6)); j++)
{
for(int i = 6; (i < (p-6)); i++)
{
if(array[j][i] == 1 )
cout << '*';
else
cout << '.';
}
cout << endl;
}
}
//I read somewhere it would be a good idea to make sure to end the program early if it somehow
//became stable by itself early. so this compares the old array with the new one to check if they
//are the same. This commonly occurs if a glider runs off the screen for example.
bool compare(int **array1, int **array2,int o,int p)
{
int counter = 0;
for(int j = 0; j < o; j++)
{
for(int i = 0; i < p; i++)
{
if(array1[j][i]==array2[j][i])
counter++;
}
}
if(counter == o*p)
return true;
else
return false;
}
int main()
{
int o= 52, p=62;
int **firstgen;
int **next;
int **backup;
// 40 + 12, 50 + 12
int x, y;
char starty;
char again;
char cont;
bool comparison;
//Here is where we initialize our arrays
firstgen = init(o,p);
next = init(o,p);
backup = init(o,p);
cout << endl << "Welcome to John Conway's Game of Life." << endl;
//This loop is for if we are still simulating, don't get confused!
do
{
//this loop checks for inputs.
do
{
menu: //this is a goto we use for if we change dimensions
x = 0, y = 0;
//now we get the menu
cout << endl << "--- Choose an option Below ---" << endl;
cout << "(a) Glider" << endl;
cout << "(b) Gosper Gilder gun" << endl;
cout << "(c) R Pentomino Pattern" << endl;
cout << "(d) Oscillator" << endl;
cout << "(e) Change the dimensions (it defaults to (50*40)" << endl;
cin >> starty;
}while(starty != 'a' && starty != 'b' && starty != 'c' && starty != 'd' && starty != 'e');
int i = 0;
//we need to assign firstgen in this area
//choose a glider position
if (starty == 'a'){
x= 0, y= 0;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > p || y < 0 || y > o){
cout << endl << "you entered invalid dimensions" << endl;
goto menu;
}
x = x+6; //we add 6 because there are six spots to the left that aren't shown we need to account for
y = y+6;
//creates the glider
firstgen[y][x] = 1;
firstgen[y][x+1] = 1;
firstgen[y][x+2] = 1;
firstgen[y-1][x] = 1;
firstgen[y-2][x+1] = 1;
}
else if (starty == 'b'){
x= 0, y= 0;
cout << "Your dimensions are based on the farthest left point" << endl;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > p || y < 0 || y > o){
cout << endl << "you entered invalid dimensions" << endl;
goto menu;
}
//this is because we have the buffer zone of 6
x = x+6;
y = y+6;
//Gosper gun
//box on left
firstgen[y][x] = 1;
firstgen[y][x+1] = 1;
firstgen[y+1][x] = 1;
firstgen[y+1][x+1] = 1;
//left circle starting in top of the left curve (flat part)
firstgen[y][x+10] = 1;
firstgen[y-1][x+11] = 1;
firstgen[y-2][x+12] = 1;
firstgen[y-2][x+13] = 1;
firstgen[y+1][x+10] = 1;
firstgen[y+2][x+10] = 1;
firstgen[y+3][x+11] = 1;
firstgen[y+4][x+12] = 1;
firstgen[y+4][x+13] = 1;
//dot in middle
firstgen[y+1][x+14] = 1;
//arrow thing on the right
firstgen[y-1][x+15] = 1;
firstgen[y][x+16] = 1;
firstgen[y+1][x+16] = 1;
firstgen[y+1][x+17] = 1;
firstgen[y+2][x+16] = 1;
firstgen[y+3][x+15] = 1;
//boomerang bit on the far right section
firstgen[y][x+20] = 1;
firstgen[y][x+21] = 1;
firstgen[y-1][x+20] = 1;
firstgen[y-1][x+21] = 1;
firstgen[y-2][x+20] = 1;
firstgen[y-2][x+21] = 1;
firstgen[y-3][x+22] = 1;
firstgen[y-3][x+24] = 1;
firstgen[y-4][x+24] = 1;
firstgen[y+1][x+22] = 1;
firstgen[y+1][x+24] = 1;
firstgen[y+2][x+24] = 1;
//tiny box on farthest right, almost done!
firstgen[y-1][x+34] = 1;
firstgen[y-1][x+35] = 1;
firstgen[y-2][x+34] = 1;
firstgen[y-2][x+35] = 1;
}
else if (starty == 'c')
{
x= 0, y= 0;
cout << "Your dimensions are based on the farthest left point" << endl;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > p || y < 0 || y > o){
cout << endl << "you entered invalid dimensions" << endl;
goto menu;
}
x = x+6;
y = y+6;
//creates R Pentamino pattern
firstgen[y][x] = 1;
firstgen[y][x+1] = 1;
firstgen[y+1][x+1] = 1;
firstgen[y-1][x+1] = 1;
firstgen[y-1][x+2] = 1;
}
// creates the simple oscillator
else if (starty == 'd')
{
x= 0, y= 0;
cout << "Your dimensions are based on the top of the oscillator" << endl;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > p || y < 0 || y > o){
cout << endl << "you entered invalid dimensions" << endl;
goto menu;
}
x = x+6;
y = y+6;
firstgen[y][x] = 1;
firstgen[y+1][x] = 1;
firstgen[y+2][x] = 1;
}
// allows you to choose your dimensions
else if (starty == 'e')
{
o= 0, p= 0;
x= 0, y= 0;
cout << "choose the height and width of your field, between 0 and 100" << endl;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > 100 || y < 0 || y > 100){
cout << endl << "Please keep dimensions between 0 and 100" << endl;
goto menu;
}
// the problem is that it is adding my x dimension and my placement choice together and then
// starts to run the program, which threadbreaks. I need to find out why these two values are
// adding together and fix it
x = x+12;
y = y+12; // plus twelve so that we have 6 around all sides
p = x;
o = y;
firstgen = init(o,p);
next = init(o,p);
backup = init(o,p);
// is this part below necessary?
//firstgen[o][p];
// next[o][p];
// backup[o][p];
// idk
// cout << "y value is: " << o << " and the x value is " << p << endl; // debugging
goto menu;
}
//Loop that does the simulation.
do
{
//Prints the generation. If i == 0, the firstgen array is copied to the
//next array, and is printed before any functions act upon it.
cout << endl << "Generation " << i << ":" << endl << endl;
//Initializes the arrays by copying the firstgen array to the next array.
if(i == 0)
copy(firstgen, next, o, p);
//this stuff below happens in every cycle
cout << "the x/p value is" << p << "and the y/o value is " << o << endl;
copy(next, backup, o, p);
print(next, o, p);
life(next, o, p);
i++;
//Pauses the system .2 seconds so that it doesn't flash past you super fast and you
// can't appreciate its beauty
system("sleep .2");
//Checks whether the generation is a multiple of 100 to ask
//the user if they want to continue
if(i % 100 == 1 && i != 1)
{
cout << endl;
//Loop to check for proper inputs.
do
{
cout << "Continue? (y or n): ";
cin >> cont;
}while(cont != 'y' && cont != 'n');
if(cont == 'n')
break;
}
//Compares the current generation with a backup generation.
//The idea is that if it is the same with the backup generation then
//something boring is going on or smething went wrong. It will end if that
//is the case.
comparison = compare(next, backup, o, p);
if(comparison == false)
// system("clear");
//cout << string( 10, '\n' );
if(comparison == true)
cout << endl;
}while(comparison == false);
//Loop to check if we want to keep going.
do
{
cout << "Run another Simulation? (y or n): ";
cin >> again;
}
while(again != 'y' && again != 'n');
//this is where we clean out all our firstgen values
//i used to have this at the top but didn't really need it
for(int y = 0; y < o; y++)
{
for(int x = 0; x < p; x++)
{
firstgen[y][x] = 0;
}
}
}
while(again == 'y');
return 0;
}
I figured it out!
The thing to take away from this is to make sure that your initiation function creates the array with the same size as the one you will be accessing. I was trying to get values from array[52][1] which didn't exist because in my init function i only had the for loop running while n < o, which means it didn't create the 52nd row. what a relief!