recursive fill function

Pages: 123
actually I don't like a function that call another function that call another function again.

you must return a value from a function to main() and then the main() function will do another function call (easier to understand).

like for example:
1
2
3
4
5
6
int main(){
    do{
        val = funcCall1(val2);
        val2 = funcCall2(val);
    }while(...);
}

instead of
1
2
3
4
5
6
7
8
9
10
11

funcCall2(){
    funcCall1();//I don't recommend it at all.
}
funcCall1(){
    funcCall2();//it will do recursion but is ugly since it calls another function to do the recursion.
}

int main(){
    funcCall1();
}


Your problem is you must return 2 variables right? (for row and column)
well there is a code in returning to variables.
but you must understand it first.
Here's the link of the code if you want.
http://www.cplusplus.com/forum/beginner/18489/#msg94658
Last edited on
Okay so when i read that thread I got the impression that when returning 2 values you should use two functions to return them. Although, I do seem to misunderstand concepts.
when returning 2 values you should use two functions to return them

no. I only posted that to show a way in returning to values but try reading the post made by Duoas
here's his code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// this demonstrates returning two values from a function
//

#include <iostream>
#include <map>
#include <string>

using namespace std;

// Our template functor
template <typename T1, typename T2>
struct t_unpair
  {
  T1& a1;
  T2& a2;
  explicit t_unpair( T1& a1, T2& a2 ): a1(a1), a2(a2) { }
  t_unpair<T1,T2>& operator = (const pair<T1,T2>& p)
    {
    a1 = p.first;
    a2 = p.second;
    return *this;
    }
  };

// Our functor helper (creates it)
template <typename T1, typename T2>
t_unpair<T1,T2> unpair( T1& a1, T2& a2 )
  {
  return t_unpair<T1,T2>( a1, a2 );
  }

// Our function that returns a pair
pair<int,float> dosomething( char c )
  {
  return make_pair<int,float>( c*10, c*2.9 );
  }

// And how we test the pair.
int main()
  {
  int   a;
  float b;
  unpair( a, b ) = dosomething( 'A' );
  cout << a << ", " << b << endl;

  return 0;
  }

unpair( a, b ) = dosomething( 'A' );

this function now accepts two return values.. a and b. :)
I'm not really sure I understand the code above. I'm still trying to learn it. If you could clarify that more it would be great.
Hey I saw you love c++ do you think you could help me? I am just getting started with it a little more and while I understand the concept I am having trouble putting it into program
@kg245407
I already posted a reply in your thread
http://www.cplusplus.com/forum/general/19857/page2.html#msg103250

Next time don't post in another thread asking for help ok?

@ coolProgramer5
I'll post the explanation maybe tomorrow. I'm working right now.
The program isn't crashing anymore however, its repeating the getshape function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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
  }
}
Is there a more efficient way to get the shape(other then loading a .txt document)?
What is your output?
I'm sorry for not replying. I'm very busy right now.
well right now the program is looping at the getshape function because the max row and column are very high but even if i lower them to 50 its will still be very long.
here is my program as of right now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#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");
	}
}
Topic archived. No new replies allowed.
Pages: 123