printing chars with array.

I just got done writing an array program, and now I got stuck on the next one I had to write. The code is supposed to display. My code displays the proper characters on each line, but it does decrease. I do not know why each new programming problem seems impossible.
<<<<<<<<
>>>>>>>>
<<<<<<
>>>>>>
<<<<
>>>>
<<
>>
The code seems to write a single character infinite loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
	int i, j;
	char a = '<', b = '>';
	char print[2]{a, b};
	for (j = 0; j < 8; j++)
		{
			if (j % 2 == 0)
				print[j] = a;
			else
				 print[j]=b;
			for (i = 8; i > 2; i--)
			{
				cout << print[j]; 
			}
			cout << endl;
		}
		

	return 0;
}
D you know how to print those lines without array ? Only using loop ?
I believe so, but my assignment requires that I use an array. I have never done one with each line alternating, and with decreasing characters.
Last edited on
I think you can create that loop, but instead of print it out, you can assign it to 2D array
I have never attempted a 2d array before. I wrote this after a little internet research.
char print['<']['>'];
But I do not understand how to call the different elements of the array, or even if that is the right code.
You can think that 2D or more as a diagram,
It'll looks like
char x[][]
The first [] fill wth how many sentence that you'll create
And the second one is how many character for each sentence
So that means I will not include the characters < and > in the boxes? I just use the 2d for the placement of the characters?
Yup, '<' or '>' wll assigned to the array its looks like x[0][0]='<' now the first character of the first sentence is <
Okay after reading what you said here is my idea. I will create two arrays, one for each char. Then I will use each array in a separate loop to print their characters. I was thinking of using a modulus if else to determine which array is in play on which line. Does any of that sound correct?
Yeh ts possible, but the reason why I ask you for create loop that can print those lines is for easier method to give the array an correct value

Like
1
2
3
4
5
6
char x[2][9];
for(int i=2;I>0;i++){
  for(int i=8;i>0;i++)
    if(i%2==0)x[i][j]='x';
    else x[i][j]='y';
}

Now x[0] is xxxxxxxx
And. x[1] is yyyyyyyy
you wrote char X[2][9], what is the 9 for? Your code only displays 8 characters. Also is your inner loops supposed to use the variable j? If not how is j used in the code?
Fist c String we'll need extra space for null character at the end of strng
And second ups my mistake ^^)a
Okay, thanks a lot for taking the time to help me out. I am going to work on the code and post what I have in a few hours.
Couldn't make anything that works, so I will try some more in the morning.
Topic archived. No new replies allowed.