Populating 2D array using functions

Hi All,

New to the board, a fellow classmate suggested the site. I have a program for intro to C++ class that is as follows.

Your program will declare a 2-D array of dimensions 20 x 20. The array will be of type "character".
Functions:
You will create several functions for this program.
1) One function will take the address of the array and initialize all cells to ' ' (a blank space).
If the 2-D array is odd, then you will create functions that make:
2) a triangle using '*', OR
3) a diamond using '*', OR
4) a bulls-eye using '*'. <=== this one is worth BONUS POINTS!! and is therefore optional.
If the 2-D array is even, then you will create functions that make:
5) checkerboard #1 using '*', OR
6) checkerboard #2 using '*', OR
7) a GIANT checkerboard using '*'. <=== this one is worth BONUS POINTS!! and therefore is optional.

These functions will be called from main, and return nothing. Your program will keep asking the user if they want a different kind of 2-D array and different shape. If not, then quit.

Here is what I have so far. I have only been able to get the triangle work. I have not attempted the bullseye or giant checkerboard as they are for extra credit and I can't get the main program to work.

Any help is greatly appreciated!!

Thanks,

Mike

#include <cstdlib>
#include <iostream>
void clear(char pattern[20][20]);
void triangle(char pattern[20][20], int size);
void diamond(char pattern[20][20], int size);
// void bullseye (char pattern[20][20], int size);
void checker1(char pattern[20][20], int size);
void checker2(char pattern[20][20], int size);
// void bigchecker(char pattern[20][20], int size);

using namespace std;

int main(int argc, char *argv[])
{
char pattern[20][20];
int i, j, size, shape;
char answer;
do
{
clear(pattern);
size = 0;
shape = 0;
answer = 0;

cout << "Please enter a number between 1 and 20: ";
cin >> size;

if(size%2) //we have an odd number
{
cout << "Please select the shape that you would like to fill your ";
cout << size << " by " << size << " array: " << endl << endl;

cout << "1 to make a triangle " << endl;
cout << "2 to make a diamond " << endl<< endl ;
// cout << "3 to make a bullseye " << endl ;
cin >> shape;

switch(shape)
{case 1:
triangle(pattern,size);
break;
case 2:
diamond(pattern,size);
break;
/* case 3:
bullseye(pattern,size);
break; */
}
cout << endl;
}

else
{
cout << "Please select the shape that you would like to fill your " <<
size << " by " << size << " array: " << endl << endl;

cout << "1 to make a Checker Board 1 " << endl;
cout << "2 to make a Checker Board 2 " << endl << endl;
// cout << "3 to make a Giant Checker Board " << endl ;
cin >> shape;

switch(shape)
{case 1:
checker1(pattern,size);
break;
case 2:
checker2(pattern,size);
break;
/* case 3:
bigchecker(pattern,size);
break;*/
}
cout << endl;
}


for(i=0; i<20; i++)
{
for(j=0; j<20; j++)
cout << pattern[i][j];
cout << endl;
}
cout << "Do you want to use this program again? Type y or n" << endl;
cin >> answer;

}
while(answer = "y" || "Y");


cout << "Thank you for using this program! Have a nice day! " << endl;

system("PAUSE");
return EXIT_SUCCESS;
}



void clear(char pattern[20][20])
{int i , j;

for(i=0; i<20; i++) // Keeps track of the rows
{
for(j=0; j<20; j++) // Keeps track of columns
pattern [i][j] = ' ';
}
}

void triangle(char pattern[20][20], int size)
{// cout << "triangle" << endl;
int j, i = 0; //starts at the first row
int stars = size;

while (stars >= 0) //this loop will execute "stars" number of times
{
for (j = 0; j <= stars; j++)
pattern[i][j] = '*';
stars--; //decrement stars by 1
i++; //go down to the next row
}
}
void diamond(char pattern[20][20], int size)
{int stars = size;
int i, j, k;

for (i = 0; i <= stars; i++)
{for(j = (stars - i); j >= 1;j--)
pattern[i][j] = ' ';

for(k = 2 * i - 1; k >= 1; k--)
pattern[i][j] = '*';
}
}
void bullseye(char pattern[20][20], int size)
{cout << "Bullseye" << endl; }


void checker1(char pattern[20][20], int size)
{int i, j, k;
int stars = size;

for (i = 0; i <= stars; i++)
if (i % 2)
{ for (j = 0; j <= stars/2; j++)
pattern[i][j] = ' ';
for(k = stars / 2; k >= 1; k--)
pattern[i][j] = '*';
}
else
{
for (j = 0; j <= stars/2; j++)
pattern[i][j] = '*';
for(k = stars/2; k >=1; k--)
pattern[i][j] = ' ';
}
}

void checker2 (char pattern[20][20], int size)
{int i, j, k;
int stars = size;

for (i = 0; i <= stars; i++)
if (i % 2)
{ for (j = 0; j <= stars/2; j++)
pattern[i][j] = '*';
for(k = stars / 2; k >= 1; k--)
pattern[i][j] = ' ';
}
else
{
for (j = 0; j <= stars/2; j++)
pattern[i][j] = ' ';
for(k = stars/2; k >=1; k--)
pattern[i][j] = '*';
}
}
/* void bigchecker (char pattern[20][20], int size)
{cout << "bigchecker" << endl;}*/
Please use code tags.
Hi Austin J,

As I am new to the programming world I am not sure what code tags are?? Would that be commenting what I am expecting the code to do after the code?

I came up with a simpler way that someone could possibly help me.
I have pasted the code for my function that is supposed to be populating the array with a diamond shape. It is very frustration for me because I could easily write the code to print out a diamond to the screen. But converting this to a function that will populate the array is MUCH different.

Here is what I have for that part of the program. This is ONLY for odd number input from a user.


void diamond(char pattern[20][20], int size)
{int stars = size; // size is the user defined size of the array (limited to 20 x 20)
int j, i = 0;

while (i < (size/2 + 1)) // This should make the top of the diamond.
{
for(j = (stars/size + i); j >= 1; j--)
pattern[i][j] = '*';
stars = stars + 2;
i++;
}
while (i > (size / 2 + 1))
{for (j = (stars/2 - i); j >= 1; j--)
pattern[i][j] = '*';
stars = stars - 2;
i--;
}
}

Any help would be greatly appreciated. I also have to write functions to populate the 2D array for even number input with a checker board 1, checker board 2, giant checker board and a bulls eye (for odd number input).

* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *

8 x 8 checkerboard 1 . Checkerboard 2 would be simply reversing the * and spaces.


Thanks for any and all help!

Mike
Anyone have any ideas on this?

Topic archived. No new replies allowed.