C++ School project drawing a diamond to console made of * characters - c++

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;
}

Related

How to print inverted half-star pyramid pattern next to each other?

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.

why do i have to add a new variable in this code

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.

How to fix addition/multiplication table errors?

I am having troubles with some of the inputs for my addition/multiplication table, and am hoping to find some help towards fixing this. I will begin by posting what I have for the program.
The code is as follows :
#include <iostream>
using namespace std;
void die() {
cout << "BAD INPUT!" << endl;
exit(1);
}
int main() {
const int ADD = 1;
const int MULTIPLY = 2;
const int MAX_SIZE = 20;
int choice = 0, min = 0, max = 0;
cout << "Choose:\n";
cout << "1. Addition Table\n";
cout << "2. Times Table\n";
cin >> choice;
if (!cin) die();
if (choice != ADD and choice != MULTIPLY) die();
cout << "Please enter the smallest number on the table:\n";
cin >> min;
if (!cin) die();
cout << "Please enter the largest number on the table:\n";
cin >> max;
if (!cin) die();
if (min > max) die();
if (max - min >= MAX_SIZE) die();
if (choice == ADD) {
for (int i = 0; i <= max; i++) {
if (i == 0)
cout << '+';
else
cout << i;
cout << '\t';
for (int j = min; j <= max; j++) {
cout << i + j << '\t';
}
cout << '\n';
}
}
if (choice == MULTIPLY) {
for (int i = min; i <= max; i++) {
if (i == min) {
cout << 'X';
else
cout << i;
cout << '\t';
for (int j = min; j <= max; j++) {
cout << i * j << '\t';
}
cout << '\n';
}
}
}
Now, here are the mistakes that I am getting from this code that I cannot seem to resolve. First, when doing the MUlTIPLY table with min = 1, max = 1, I am getting:
X 1
when I should be getting (I believe)
X 1
1 1
Secondly, while doing the MULTIPLY table with min = 1, max = 12, I am getting:
X 1 2 3 4 ... 12
2 2 4 6 8 ... 24
3 3 6 9 12 ... 36
when I should be getting
X 1 2 3 4 ... 12
1 1 2 3 4 ... 12
2 2 4 6 8 ... 24
3 3 6 9 12 ... 36
And finally, when using the ADD table with min = 21, max = 40, I cannot post all of the code since it is such a mess, but basically the columns/rows are as follows:
+ 21 22 23 24 25 ...
5
1
6
2
7
3
8
When obviously, the code should output the rows and columns to be 21 - 40 evenly. As you can see in the last example, my rows are outputting properly, but somehow my columns are a complete, garbled mess.
I have been sitting and staring at this code for awhile, and can't seem to fix these issues at hand. Can anyone help lead me in the right direction? I really appreciate any help and hints :)
Check this out. Might not be fully optimized, but works
if (choice == ADD) {
cout << '+';
for (int i = min; i <= max; i++) {
cout << '\t' << i;
}
for (int i = min; i <= max; i++) {
cout << '\n' << i << '\t';
for (int j = min; j <= max; j++) {
cout << i + j << '\t';
}
}
}
if (choice == MULTIPLY) {
cout << 'X';
for (int i = min; i <= max; i++) {
cout << '\t' << i;
}
for (int i = min; i <= max; i++) {
cout << '\n' << i << '\t';
for (int j = min; j <= max; j++) {
cout << i * j << '\t';
}
}
}
See output here.
#include <iostream>
#include <iomanip>
#include <cstdio>
void die()
{
std::cout << "BAD INPUT!" << "\n";
exit(1);
}
int main() {
const int ADD = 1;
const int MULTIPLY = 2;
const int MAX_SIZE = 20;
int choice = 0, min = 0, max = 0;
std::cout << "Choose:\n";
std::cout << "1. Addition Table\n";
std::cout << "2. Times Table\n";
std::cin >> choice;
if (!std::cin) die();
if (choice != ADD and choice != MULTIPLY) die();
std::cout << "Please enter the smallest number on the table:\n";
std::cin >> min;
if (!std::cin) die();
std::cout << "Please enter the largest number on the table:\n";
std::cin >> max;
if (!std::cin) die();
if (min > max) die();
if (max - min >= MAX_SIZE) die();
if (choice == ADD) {
for (int i = 0; i <= max; i++) {
if (i == 0)
printf(" +");
else
printf("%3d", i);
printf(" ");
for (int j = min; j <= max; j++) {
printf("%3d ", i + j);
}
printf("\n");
}
}
if (choice == MULTIPLY) {
/* for printing header of the multiplication table */
std::cout << "X\t";
for (int j = min; j <= max; j++) {
std::cout << min * j << "\t";
}
std::cout << "\n";
/* for printing rest of the table */
for (int i = min; i <= max; i++) {
std::cout << i << "\t";
for (int j = min; j <= max; j++) {
std::cout << i * j << '\t';
}
std::cout << '\n';
}
}
}
The crucial mistake in your code for multiplication was that, you were trying to print (max - min + 1) + 1 rows in total, the extra +1 for the header. While your code was printing the first row as header and then starting directly with the second row.
Your code for addition table was correct, but 21 to 40 with tab character in between was too taking too much space for a typical laptop screen, not to say the output won't be pretty.
On my system, the output of tput lines and tput cols was 38 and 144 resp.
which wasn't sufficient for your code.
you can format the output with printf using printf fixed width output.
Considering you are not much familiar with C++, I would like to state that
using the std namespace as default namespace will work for this program, but when you working with larger projects, you should always prefix it.
I haven't enough reputation to add comment
if I were, I was commenting these lines
if (i == min) {
cout << 'X';
else
cout << i;
cout << '\t';

Simple for loop I can't figure out

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<<" ";
}

Dynamic array error in Conways Game of Life

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!