array cout

hello world!
if i have array let say map[5]={0,0,1,2,3]};
And I want to count with
0=@@
@!
1=##
##
2=$$
&*
3=**
**

Result as @@@@##$$**
@!@!##&***
Am I able to do it? Or only to create larger array to implement the idea? Thanks


Hello blacksjacks,

Yes. You could use a for loop and a switch pt display what you want.

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (size_t literal = 0; literal < MAXSIZE; literal++)
{	
	switch (intArray[lc])
	{
		case 0:
			std::cout << "@@";  // <--- No "\n" or "endl".
			break;
		case 1:
			std::cout << "##";  // <--- No "\n" or "endl".
			break;
		//default:  // <--- Not really needed here.
			//break;
	}
}
std::cout << std::endl;


Hope that helps,

Andy
Hello blacksjacks,

I do not know what you did with map[5]={0,0,1,2,3]};. The space between "map" and "[5]" and also between "[5]" and "=", I do not think it is just a space, but maybe a tab?

Also the "]" after the "3" should not be there.

From C++11 you can write the line as map[5]{ 0, 0, 1, 2, 3 };.

Again choose a better name than map that is more descriptive of what it is.

Hope that helps,

Andy
thanks Handy Andy,
and thanks for point out error,
what i want in array is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int arry[5]={0, 0, 1, 2, 3};
.
..
...
....
switch (arry[i][j]
case 0: cout<<"@@"
              "@!"
case 1: cout << "##"
                "##"
case 2: cout << "$$"
                "&*"
case 3: cout << "**"
                "**"

output should be
@@@@##$$**
@!@!##&*** 
switch (arry[i][j]

1) Why are you using two indices, when you've declared it as a 1-dimensional array? What are i and j supposed to represent?

2) You've missed the closing parenthesis from that statenent.

3) Every single one of your cout statements is missing a final semicolon.
thanks MikeyBoy for pointing out error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int arry[5]={0, 0, 1, 2, 3};
for (i=0;i<=5;i++)
{
switch (arry[i]
    {
case 0: cout<<"@@";
              "@!";
case 1: cout << "##";
                "##";
case 2: cout << "$$";
                "&*";
case 3: cout << "**";
                "**";
     }
}
output should be
@@@@##$$**
@!@!##&*** 
You can't write to more than one line at once.
Write the first line, then output a newline, and then the 2nd line.
This might require 2x loops (or an inner and outer loop, there's multiple ways to do this).

Also, your lines 7, 9, 11, and 13 aren't doing anything.

http://www.cplusplus.com/doc/tutorial/basic_io/
Last edited on
thanks Ganado for reply, i have no idea how to 1d array to output 2 rows, can give me hints? thanks
Hello blacksjacks,

I do not know of any way to print two different lines in one for loop. If you need two different lines than use two for loops with the case statements generating two different lines, but one line at a time.

Had I noticed the second line earlier I would have suggested two for loops.

Hope that helps,

Andy
When outputting to standard out, you can only do it line-by-line.

What you need to do here is construct your entire output in memory first, as a pair of strings, and then, one you've finished constructing them all, then print the lines out.

So for each value in your array, you modify - in memory - your first string, and then modify - again, in memory - the second string.

Then, once you've finished processing all the array elements, then you print out the first string, followed by the second string.

The important point is that you don't output anything, until after you've finished processing all the array elements, and have completely constructed both strings.
blacksjacks, what you mean is:
- you have a C-style array of ints
- where the only admitted digits are 0, 1, 2 and 3,
- and you want to apply a two steps conversion such as
- 0 becomes @@ and later @@ becomes @!
- 1 becomes ## and ## remains ##
- 2 becomes $$ and later $$ becomes &*
- 3 becomes ** and ** remains **

- but you want both transformations to take place in the same line?

For example:
int myarr { 0, 0, 1, 2, 3 };
0 @@ @!
0 @@ @!
1 ## ##
2 $$ $$
3 ** **


Have I understood or am I misinterpreting what you aim to do? And, if I am right, what’s the part you find it difficult to achieve?
Last edited on
@Enoizat No, I don't think so. The output is supposed to be two lines. For each given number in the array, a certain pair of characters is appended to the first line, and another pair of characters is appended to the second line.

So, for 0,
@@
is appended to the first line, and
@!
is appended to the second.

So, for the array {0} we would have:

@@
@!


for {0, 0}:

@@@@
@!@!


for {0, 0, 1}:

@@@@##
@!@!##


etc.

Last edited on
@MikeyBoy, thank you.
What can I say? «Oooooh!» :-)
I must admit I hadn't the foggiest notion what you were talking about :-/
Now I can see that, ok, it's an exercise that makes sense.
This in an example where data is more appropriate than code. Store the int-to-string mapping in an array. Since there are two different mappings, use two different arrays.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using std::cout;

int main()
{
    const char *line1[] = { "@@", "##", "$$", "**" };
    const char *line2[] = { "@!", "##", "&*", "**" };

    int indices[5] = {0, 0, 1, 2, 3 };

    for (int i : indices) cout << line1[i];
    cout << '\n';

    for (int i : indices) cout << line2[i];
    cout << '\n';
}

@@@@##$$**
@!@!##&***

Very clean, nice dhayden.
Topic archived. No new replies allowed.