Problems of printing pattern

When I try to use 2d array to print a greeting card, the output of the columns are not I want(it shifted).
Can someone teach me how to write the correct code to have the correct output?
Thank you so much.

Here is the 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
void main{
      string pattern[70][30];
      string r;
  	for (int i = 0; i < 16; i++){
	 for (int j = 0; j < 71; j++){
	   //col
	   if ((i>0 && i < 15) && (j == 0 || j == 69)){
	       pattern[i][j] = "x";
	       cout << pattern[i][j];
		}
	   //row
	   if ((i == 0 || i == 15) && j < 70){
	       pattern[i][j] = "x";
	       cout << pattern[i][j];
		}
	   else
		pattern[i][j] = " ";
	        cout << pattern[i][j];

	   if (j == 5)
		{
		  if (i == 1)
		    pattern[i][j] = r;
	            cout << pattern[i][j];}
}
I want to keep it simple by using for-loop and conditional statement only, coz I am just a beginner and want to practice more the basic technique.
closed account (48T7M4Gy)
So what is the correct output? At the moment your program a row of x's.
I have modified the codes as below. I don't know why there will be a extra x inside the box. I want the box fixed when I type something inside the box. But now the last column of x will be shifted if something in the box.

#include <iostream>
#include<string>
using namespace std;

void main(){
string pattern[70][30];
for (int i = 0; i < 16; i++){
for (int j = 0; j < 71; j++){
//col
if ((i>0 && i < 15) && (j == 0 || j == 69)){
pattern[i][j] = "x";
cout << pattern[i][j];
}
//row
if ((i == 0 || i == 15) && j < 70){
pattern[i][j] = "x";
cout << pattern[i][j];
}

else
cout << " ";

if (j == 5)
{
if (i == 1)
pattern[i][j] = "Hello";
cout << pattern[i][j];
}
}cout << endl;
}
}
closed account (48T7M4Gy)
OK so what you have to do is modify the lines in which you have characters other than blank spaces.

You currently have one pattern for a normal row and a second pattern for a normal column.

So you need a new pattern for a row with text made up of
x + a spaces + text + b spaces + x

This means you will have a small calculation. You know the overall width of the line. The reast follows.


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
x      Hello                                                               x  
x      x                                                               x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
x                                                                     x  
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 


So I cannot just use loop to fixed the position of the columns?

Also how can I change these if statement to a nested for-loop and show the pattern in the box?

if ((i == 5 && j == 5)||(i==9&&j==11)){
pattern[i][j] = "*";
cout << pattern[i][j];
}
if ((i ==5||i==9) && (j>5 && j < 11)){
pattern[i][j] = "=";
cout << pattern[i][j];
}
if ( (j==5||j==15) && (i>5 && i < 9)){
pattern[i][j] = "|";
cout << pattern[i][j];
}
closed account (48T7M4Gy)
Yes you could. There is no single way to do it. But you have to realise that the text is pushing your line out.

Why don't you use what you have and adjust the number of spaces as I suggested. Unfortunately that is what you will have to do. There is no magic. :)
You've got an interesting idea with the pattern, but you're not taking advantage of it at all.

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
// http://ideone.com/Gm8TcZ
#include <iostream>
#include<string>

int main() {
    const std::size_t rows = 30;
    const std::size_t cols = 70;

    std::string pattern[rows];

    // initialize strings in pattern to be of length "cols" and all spaces:
    for (std::size_t i = 0; i < rows; ++i)
        pattern[i] = std::string(cols, ' ');

    // top/bottom:
    for (std::size_t i = 0; i < cols; ++i)
        pattern[0][i] = pattern[rows - 1][i] = 'x';

    // left/right:
    for (std::size_t i = 1; i < rows - 1; ++i)
        pattern[i][0] = pattern[i][cols - 1] = 'x';

    // print the pattern out, taking advantage of the fact that each row is a string and
    // cout knows how to print the whole row:
    for (std::size_t row = 0; row < rows; ++row)
        std::cout << pattern[row] << '\n';

    std::cout << '\n';
    std::cin.ignore(100, '\n');

    // We want to insert "Hello" into the box without distorting the boundary:
    {
        std::size_t insertion_row = 5;
        std::size_t insertion_col = 5;
        std::string insert_me = "Hello";

        for (std::size_t i = 0; i < insert_me.length(); ++i)
            pattern[insertion_row][insertion_col + i] = insert_me[i];
    }

    // See our handiwork
    for (std::size_t row = 0; row < rows; ++row)
        std::cout << pattern[row] << '\n';

    std::cout << '\n';
    std::cin.ignore(100, '\n');

    // An alternate way to insert text:
    {
        std::size_t insertion_row = 10;
        std::size_t insertion_col = 5;
        std::string insert_me = "Goodbye";

        pattern[insertion_row].replace(insertion_col, insert_me.length(), insert_me);
    }

    // See our handiwork
    for (std::size_t row = 0; row < rows; ++row)
        std::cout << pattern[row] << '\n';

    std::cout << '\n';
}
Wow. Thank you so much. You guys are amazing :D
Sorry for one more question. How to convert this loop to function and return the value to pattern array?

for (std::size_t i = 0; i < insert_me.length(); ++i)
pattern[insertion_row][insertion_col + i] = insert_me[i];
closed account (48T7M4Gy)
.
Last edited on
When I try to modify the provided code, it said "error C2109: subscript requires array or pointer type" and I add these pointers. But it still not work and keep showing the same error message. How can I modify it :( ?

int *pointerR;
int *pointerC;

void drawPattern(char[X][Y], int r, int c, char){
pointerR = &r;
pointerC = &c;
}

....

void drawPattern(char aPattern,int , int , char aChar)
{
int r = *pointerR;
int c = *pointerC;
aPattern[r][c] = aChar;
}
Last edited on
closed account (48T7M4Gy)
When I try to modify the provided code

Which code is the provided code?
The drawLine function provided by you:

void drawLine(char[limitX][limitY], int, int, int, int, char);


void drawLine(char aPattern[limitX][limitY], int r0, int c0, int r1, int c1, char aChar)
{
int col = 0;
double m = (c1 - c0) / (r1 - r0);

for (int row = r0; row < r1; row++)
{
col = (int)((c0 - m * r0) + m * row);
aPattern[row][col] = aChar;
}
}
closed account (48T7M4Gy)
char** passes a 2d array.
So if I want to use the function to store the value of a symbol in the 2d array, how can I modify it?
Topic archived. No new replies allowed.