Printing alternating characters with array

Nov 21, 2014 at 10:17pm
I need to write an array code the prints this.
$=$=$=$=$=$
$=$=$=$=$=$
$=$=$=$=$=$
$=$=$=$=$=$
I really have no idea where to start. I was thinking that I would use a modulus operation, so even numbers with a reminder of zero would give = and a number with reminder would give $. I would use two for loops for the rows and columns. I have never used a array for a character and I do not know how to connect it to a modulus operation. Could someone send me some links to something that might get me on the right track, or just help explain some of the concepts.
Nov 21, 2014 at 10:24pm
The array has to be a char type.
Nov 21, 2014 at 10:44pm
I have been working a little bit on the code, and I was wondering if someone could tell me if I am anywhere near being on the right track.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
	int i, j;
	char a = '$', b = '=';
	char print[2]{a, b};
	for (i = 0; i < 5; i++)
	{
		for (j = 0; j < 11; j++)
		{
			if (j % 2 == 0)
				cout << print[b];
			else
				cout << print[a];

		}
		cout << endl;
	}

}
Last edited on Nov 21, 2014 at 10:53pm
Nov 21, 2014 at 11:12pm
Well I figured it out. I am posting my code in case it can help anyone else. I would welcome any feedback on ways in which I can improve the code, but I will be declaring the problem solved in a few hours. Actually feels really good to complete this on my own.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
	int i, j;
	char a = '$', b = '=';
	char print[2]{a, b};
	for (i = 0; i < 5; i++)
	{
		for (j = 1; j < 12; j++)
		{
			if (j % 2 == 0)
				cout << print[1];
			else
				cout << print[0];

		}
		cout << endl;
	}
	return 0;
}
Nov 21, 2014 at 11:55pm
You could have made the char array and filled it in the loop.
1
2
3
4
5
6
7
8
char print [11]
for (i = 0; i < 11; j++)
{
    if (i % 2 == 0)
        print[i] = '$';
    else
        print[i] = '=';
}

Then you could print it in the same way you had your nested for loop.
1
2
3
4
5
6
for (i = 0; i < 5; i++)
{
    for (j = 0; j < 11; j++)
        cout << print[i];
    cout<<endl;
}

Your way of doing it works just fine too. This way above just assigns the "$" and "=" at runtime instead.
Nov 22, 2014 at 12:06am
I like your way better. I think it will work better with the assignment I am working on right now.
Topic archived. No new replies allowed.