Could you clarify that a little more I'm not sure I understand.

How do you traverse a 1-d array?

``````//set a constant called MAX_SIZE (you already have this, 2 of them actually)
//create an array with MAX_SIZE elements (you already have this as well)
//for an index variable with values [0, (MAX_SIZE-1)] {

/*...you may perform actions here...*/

//access the array element and set it to a value
array[index] = value;

/*...you may perform other actions here...*/

//end loop
}``````

Take that and see if you can extrapolate it to a 2-d array (your shape array)...

I'm really sorry but I just started a month ago so I don't understand you very well could you clarify the for me.

Think about it a little, it's not that tough... I'm not telling you to do anything you aren't already doing else where in your program.

``````//set a constant called MAX_SIZE (you already have this, 2 of them actually)
const int MAX_SIZE = 10;
int value = 0;
//create an array with MAX_SIZE elements (you already have this as well)
int anArray[MAX_INT] = {0};

//for an index variable with values [0, (MAX_SIZE-1)]
for (int index = 0; index < MAX_SIZE; ++index) {

/*...you may perform actions here, such as input...*/
printf("Enter an integer number: ");
scanf("%d", &value);

//access the array element and set it to a value
anArray[index] = value;

/*...you may perform other actions here...*/

} //end the for loop``````

This is the basic idea for traversing a 1-dimensional array. Using this as a starting point, you should be able to find a way to modify it to traverse both dimensions of a 2-d array (which your shape array is). Hint: it requires 2-loops...

ok

One quick question: what is scanf?

One quick question: what is scanf?

scanf() is the input equivalent of printf(), which is output. I think you've been using getchar(), but that doesn't work for my example.

okay thanks.

I made an attempt at modifying your example to work with a 2-D array instead of an 1-D array. However, I'm not really sure if I did it right.

``````char value;
char end = '.';
printf("Enter a shape\n");
for (int rindex = 0; rindex < MAX_SIZE; rindex++)
{
for (int cindex = 0; cindex < MAX_SIZE; cindex++)
{
scanf("%c", &value);
shape[rindex][cindex] = value;
if (value == end)
{
return;
}
}
}``````

Is that code right?

It looks like you have the correct idea, but you should be using your row and column max values instead of MAX_SIZE.

MAX_SIZE is something that I created for demonstration purposes.

well The row and column values are the interior point(where i should start filling) I don't know the length until after the getshape function prototype is done and the program goes into the getsize function prototype.

Well the program doesn't crash anymore however, the program is repeating the getshape function.

``````#include <stdio.h>
#include "simpio.h"

const int MAX_ROW = 1000;
const int MAX_COL = 1000;

void getshape(char shape[MAX_ROW][MAX_COL]);
void getsize(char shape[MAX_ROW][MAX_COL]);
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column);
int main()
{
char shape[MAX_ROW][MAX_COL];
int row=0, column=0;
printf("Enter the interior point where the program where start filling\n");
row=GetInteger();
column=GetInteger();
getshape(shape);
int r, c;
getsize(shape);
fillArray(shape, row, column);
getchar();
return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL])
{
char value;
char end = '.';
printf("Enter a shape\n");
for (int rindex = 0; rindex < MAX_ROW; rindex++)
{
for (int cindex = 0; cindex < MAX_COL; cindex++)
{
scanf("%c", &value);
shape[rindex][cindex] = value;
if (value == end)
{
break;
break;
return;
}
}
}
}
void getsize(char shape[MAX_ROW][MAX_COL])
{
int i, j;
int columns = 1000, rows = 1000;

for(i=0;i<rows;i++)
{
for(j = 0; j <columns; j++)
{
if(shape[i][j] == '\0' && shape[i-1][j] == '\0')
{
columns = j;
break;
}
if(shape[i][j] == '\0' && shape[i][j-1] == '\0'){
rows = i;
break;
}
}
if(rows != 1000 && columns != 1000) break;
}
}
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
if (shape[row][column] == '*' || shape[row][column] == '#') return;
else
{
shape[row][column] = '*';
fillArray(shape,row+1,column);//down
fillArray(shape,row-1,column);//up
fillArray(shape,row,column-1);//left
fillArray(shape,row,column+1);//right
}
}``````

To test getshape() will take for ever if you don't get it right
what you need to do is to test your function first for ideas that you can check by hand.

1000*1000 inputs will take a long time so
use smaller MAX values
if you need something this big you will have to load from file
for now I recommend that you have a test get_shape() function
that just gives you a square and a display function so that you can see what is happening then run your own get_shape for a small input and see what happens when you call display
so you want to add something a bit like:

``````const int max_row(5), max_col(5);
void dummy_get_shape(char shape[max_row][max_col]);
void display_shape(char shape[max_row][max_col]);

int main()
{
char my_shape[max_row][max_col];
dummy_get_shape(my_shape);
display_shape(my_shape);
return 0;
}

//gets a big empty square
void dummy_get_shape(char shape[max_row][max_col])
{
char edge('#');
char empty('\0');
for(int c = 0; c < max_col; ++c)
{
for(int r = 0; r < max_row; ++r)
{
char to_add = empty;
if(r == 0)
{
//left edge
}
else if(r == max_row - 1)
{
//right edge
}
else if(c == 0)
{
}
else if(c == max_col - 1)
{
}
}
}
}

void display_shape(char shape[max_row][max_col])
{

for(int c = 0; c < max_col; ++c)
{
for(int r = 0; r < max_row; ++r)
{
std::cout << shape[r][c];
}
std::cout << std::endl;
}
}``````

now you can run your getshape and see what the output looks like

commented: Rep for this monster thread +2

Okay your example program would work however, the program needs to work with all shapes. But the function prototype for getshape is very nice.

Is there a way to modify that function where it can get a different shape instead of just a box?

Is there a way to modify that function where it can get a different shape instead of just a box?

Yes, there are ways to modify it
and it is the general priniciple of putting things in functions is that it is easy to change

What you have to do to draw other shapes is define what you want to look like first

so you have a grid

``````0000#0000
000#0#000
00#000#00
0#00000#0
#########
000000000``````

You can draw a triangle like that
you will be able to find some examples on the web for this and
circle
I assume that you have some basic geometry:
A triangle is defined by three points

so you grid has an [row, column] for each cell
and you need to tell the computer when to draw a #

If you pick your three points as a coordinate
Then you can work out what the equation of the line would look
like for each side:

y = m*x + c

you have y1 & x1 for one corner and
y2 & x2 for the other this lets you find m & c via a
simultaneous equation

y1 = m*x1 + c (1)
y2 = m * x2 + c (2)

(1) - (2)
(y1 - y2) = m * (x1 - x2)
rearrange:
m = (y1 - y2) / (x1 - x2) (3)
then you can find c from (1) & (3)

c = y1 - x1 * (y1 - y2) / (x1 - x2) (4)
now you have the equation for one of the sides

if you use float instead of int for all you points
then you need to check for each coordinates to see `if(true == is_on_line(row, column)` Each coordinate has a left hand side and a right hand side
and top and bottom and if you can check if the line (1)
with the x & y values to see if the line is in the point

If this doesn't ring any bells I would suggest that you stick to squares and when you are ready to test more complicated code

read up on ifstream
this is how you open a file
then if you have a load function
you can use something like this in a function

``````//#include <fstream>
std::ifstream fin("C:\\folder\\test_file.txt");
if(fin.is_open() == true)
{
char line[max_row];
/*
you need to look up get_line()
read in a line for each row and then
set your shape with each row
*/
fin.close();
}
else
{
//failed to find folder
}``````

Now if you write a .txt file with notepad
and put in # and I would recommend 0 and convert to '\0'
you can set your shape in the file
and check that you have read it in using

display_shape(shape);

commented: it was good +1
commented: Mor rep for this thread that never ends :) +2

Okay is there a link on that?

Is there a more efficient way to get the shape(other then loading a .txt document)?

here is my program as of right now

``````#include <stdio.h>
#include "simpio.h"

const int MAX_ROW = 3; /* max no. of rows a shape can have */
const int MAX_COL = 3; /* max no. of rows a shape can have */

char get_shape(char shape[MAX_ROW][MAX_COL]);
void getsize(char shape[MAX_ROW][MAX_COL]);
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column);
void display_shape(char shape[MAX_ROW][MAX_COL]);
int main()
{
char shape[MAX_ROW][MAX_COL];
int row=0, column=0;
printf("Enter the interior point where the program will start filling\n");
printf("Row: ");
row=GetInteger();
printf("Column: ");
column=GetInteger();
get_shape(shape);
int rows, columns;
getsize(shape);
fillArray(shape, row, column);
display_shape(shape);
getchar();
return 0;
}
char get_shape(char shape[MAX_ROW][MAX_COL])
{
char value;
int r, c;
printf("Enter a shape\n");
for (int rindex = 0; rindex < MAX_ROW; rindex++)
{
r=rindex;
for (int cindex = 0; cindex < MAX_COL; cindex++)
{
scanf("%c", &value);
shape[rindex][cindex] = value;
c=cindex;
}
}
return shape[r][c];
}

void getsize(char shape[MAX_ROW][MAX_COL])
{
int i, j;
int column = 3, rows = 3;

for(i=0;i<rows;i++)
{
for(j = 0; j <column; j++)
{
if(shape[i][j] == '\0' && shape[i-1][j] == '\0')
{
column = j;
break;
}
if(shape[i][j] == '\0' && shape[i][j-1] == '\0'){
rows = i;
break;
}
}
if(rows != 50 && column != 50) break;
}
}
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
if (shape[row][column] == '*' || shape[row][column] == '#') return;
else
{
shape[row][column] = '*';

fillArray(shape,row+1,column);//down
fillArray(shape,row-1,column);//up
fillArray(shape,row,column-1);//left
fillArray(shape,row,column+1);//right
}
}
void display_shape(char shape[MAX_ROW][MAX_COL])
{

for(int r = 0; r < MAX_ROW; r++)
{
for(int c = 0; c < MAX_COL; c++)
{
printf("%c", shape[r][c]);
}
printf("\n");
}
}``````

When you say is there a more efficient way to get the shape than a txt file the answer is probably no. But it is always important to know what it is you are wanting the computer to do before you start programming.

Looking at you function get_shape it is not obvious what you are trying to with it.

It looks like that you are trying to load a char is this supposed to be used for the fill function? is this supposed to identify the shape?
Or is it supposed to load a shape into the memory?

If it is the first one then it is an easy fix.
I imagine identifying the shape is well beyond the scope of the problem. But you might have a choice of shapes say, square, rectangle, hexagon, triangle, circle.

Prompt the user for information to get them to choose the shape ie

``````cout << "Please choose a shape 1-5:" << endl;
cout <<  "1: square" << endl;
cout << "2: rectangle" << endl;
//other shapes
int shape_id;
cin >> shape_id;``````

Then you would use shape id to select a draw method such as
the earlier method did with squares. I would recommend that you
stick to squares and rectangles until you get everything else working.

Then getsize()
are you trying to get the user to tell you how big the shape should be? If so you want to get a rectangle to bound the shape as if
you were saving an image. Then you only need to get values width and height. You would benefit from moving your functions into a class if this is what getsize is supposed to do.

The load method is useful as you can find the size of your shape
before you have to store it in memory
you could store the shape in .txt file

``````10
8
0000000###
000000#00#
00###00#0#
00#0##0#0#
0##00###0#
##0000000#
#00000000#
##########``````

so the first number is the width
second the height then it is the rows one by one
This allows for any shape to be entered in
you might want another two rows to indicate the start point

The only other thing that I think getsize is doing is to get the area
in which case you can adjust your fill method to add one to the return rather than change the shape. It wants a return value

The first thing you need to do is be sure what it is that you want to do. Then decide how you can break this down into smaller steps.

The getShape function is supposed to get the shape from the user. The algorithm that firstPerson gave me is perfect so I don't need to have the user enter a number that corresponds with a certain shape. Also I was told that using an input file was a bad idea by my teacher. I also realized that I could just have the user enter a maximum number of rows and columns and making a getsize function prototype is not needed. And lastly is there a reason that my program is shutting down after the user inputs the shape.

scratch that now the program works however, the input isn't quite right. When I entered a square as the input the output was a mess of asterisks for some reason.

``````#include <stdio.h>
#include "simpio.h"

const int max=10;

char get_shape(char shape[max][max], int maxR, int maxC);
void fillArray(char shape[max][max], int row, int column);
void display_shape(char shape[max][max], int maxR, int maxC);
int main()
{
char shape[max][max];
int row=0, column=0;
printf("max no. of rows: ");
int maxR=GetInteger();
printf("max no. of columns: ");
int maxC=GetInteger();
printf("Enter the interior point where the program will start filling\n");
printf("Row: ");
row=GetInteger();
printf("Column: ");
column=GetInteger();
get_shape(shape, maxR, maxC);
fillArray(shape, row, column);
display_shape(shape, maxR, maxC);
system("pause");
}
char get_shape(char shape[max][max], int maxR, int maxC)
{
char value;
int r, c;
printf("Enter a shape\n");
for (int rindex = 0; rindex < maxR; rindex++)
{
r=rindex;
for (int cindex = 0; cindex < maxC; cindex++)
{
scanf("%c", &value);
shape[rindex][cindex] = value;
c=cindex;
}
}
return shape[r][c];
}
void fillArray(char shape[max][max], int row, int column)
{
if (shape[row][column] == '*' || shape[row][column] == '#') return;
else
{
shape[row][column] = '*';

fillArray(shape,row+1,column);//down
fillArray(shape,row-1,column);//up
fillArray(shape,row,column-1);//left
fillArray(shape,row,column+1);//right
}
}
void display_shape(char shape[max][max], int maxR, int maxC)
{

for(int r = 0; r < maxR; r++)
{
for(int c = 0; c < maxC; c++)
{
printf("%c", shape[r][c]);
}
printf("\n");
}
}``````

actually never mind I finished the program but thanks for help guys!

The get shape function currently requires the user to enter
max_row *max_column chars
which is 1000*1000 = 1 000 000 times
If you wish to test your function for big images
inputting the code will be arduous and time consuming.

There is not any point to having a very complex shape entered
if it is going to have to be entered everytime.

If this was a serious implementation you would allow input
from file but as this is only an assignment different rules apply.

I am assuming that your teacher is against file as you haven't covered it yet, but it allows you to input from either file or by hand
and even save the current shapes.

If you are not using file then you should allow a user the choice
of entering a big shape simply.
the easy shapes are square, rectangle and diamond.

``std::cout << "0: manually enter shape" << std::endl;``