Reading contents of an array horizontally?



Now, what i need help with is how to being able to insert the string into the array and be able to read it HORIZONTALLY instead of vertically. How do i output the contents of an array horizontally instead of vertically?

So if i insert 1234569 in an multidimensional array, instead of showing it like this:

1 4 7
2 5 8
3 6 9

i want it to show like this:

1 2 3
4 5 6
7 8 9

Please explain clearly as i'm still pretty novice. Thanks!
i want it to show like this:

1 2 3
4 5 6
7 8 9


The output is always horizontal. To make it this way, all you have to do is simply make a
new line after 3 outputs -

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
#include <iostream>

using namespace std;

int main(){

	int arr[3][3] = 
	{
		{1,2,3},
		{4,5,6},
		{7,8,9}

	};

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << " ";
		}
		cout << "\n";
	}

	
	return 0;
}
Last edited on
Could you show me an example of a code where you actually insert the numbers and convert them so you can see them horizontally?
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
#include <iostream>

using namespace std;

int main(){

	int arr[3][3];
	std::string numbers = "123456789";

	int stringLooper = 0;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			arr[i][j] = numbers[stringLooper] - '0'; // converting from char to integer
			stringLooper++;
		}
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << " ";
		}
		cout << "\n";
	}

	return 0;
}
Sorry for a bad example, but i didn't really mean integers. I was just using it as an example because when i explained my realt assignment, people were getting really confused.

My actual assignment is to insert a crypted message, and when it's read horizontally it would be decrypted. Could you pleaaasee be a friend and show that exact same example, but instead of numbers. show me abcdefghi horizontally in an array? I'm really lost :/
Do you mean simply filling an multidimensional string array with numbers 1-9 and then printing it out like that?
No, i mean filling a multidimensional array with a string, like in letters. So what i'm asking is if you could make a same example but instead of 123456789, you show me how to do abcdefghi.

It's just an example and nothing specific really, that's why i need a CIN in the beggining of the code :)

So you insert these letters and it will show as:

a b c
d e f
g h i
Something like this? -

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
#include <iostream>

using namespace std;

int main(){

	char arr[3][3];
	std::string numbers = "abcdefghi";

	int stringLooper = 0;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			arr[i][j] = numbers[stringLooper]; 
			stringLooper++;
		}
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << " ";
		}
		cout << "\n";
	}
	system("pause");
	return 0;
}


You only had to change the type of the array, and from 1-9 to a-i. Then simply removing the - '0'
But in that code you wrote it automatically, how do i make it that i have to type it in manually?

so in the beginning in the code it's gonna ask something like:

cout << "Type in the letters: ";
cin >> abcdefghi

in this case, abcdefghi is the string.
Last edited on
I think you can figure that one out yourself, you've got a good template which I provided for you =) Goodluck!
Alright, thanks for your help! You have been ever so helpful to me! :)
I'd be happy to help out if you get stuck on a specific bit, but you're gonna need to put in lots of effort first :p
I figured it out! But i'm still not as done, as this does not fit my assignment.
Now when you seem to really understand the point of my program, i will attempt to try to explain it as well as i can.

Here is a sketch my teacher made: https://gyazo.com/33d90496958ef231dec7866e39ce1951

My assignment is to insert a string, to be specific this string; "DA PAE RTTTARE TRYR" If this
string is placed horizontally in a string, it will show a message;


D E T T
A Ä R
K R Y
P T E R
A T

It means "THIS IS CRYPTED" in Swedish. Any tips? You have been veeery helpful so far!! :)

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
#include <iostream>

using namespace std;

int main(){

	string arr[3][4];
	char c;

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cin.get(c);
			arr[i][j] = c;

		}
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << " ";
		}
		cout << "\n";
	}

	return 0;
}
Last edited on
Sorry but I'm not following.

to be specific this string; "DA PAE RTTTARE TRYR"
-
it will show a message;


D E T T
A Ä R
K R Y
P T E R
A T

There is no "K" in the original string, so Im confused how K ended up in the message. You're probably gonna have to give it another go at explaining.

It means "THIS IS CRYPTED" in Swedish.

Jag vet ^_-
Last edited on
Hahahaha, awesome!! I'm going to continue using English for the benefit of other people to learn from this.

I think i misspelled the insert string (sorry for being so confusing >.<)

I will emphezise the blank spaces with a " _ ".

The string you are supposed to enter is: DA_PAE_KTTTÄRE_TRYR_

And it should look like this, when inserted into an array;

D E T T
A _ Ä R
_ K R Y
P T E R
A T _ _

Did i explain it well? :P
That makes more sense. The only question that remains is that. Do you want to insert it vertically or does it only have to be displayed vertically?
Hmm, it has to be printed out the way i wrote it on my last question, like it should look like that on the console. So basically just printing out the array.
Last edited on
The start is easy, simply filling the array with the string -

1
2
3
4
5
6
7
8
9
10
11
12
std::string words = "DA PAE KTTTÄRE TRYR ";
	char arr[5][4];

	int stringCounter = 0;
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			arr[i][j] = words[stringCounter];
			stringCounter++;
		}
	}


Now this is where you have to start thinking. Because you can't just display it like it is, it will print out

D A _ P
A E _ K
T T T _
...
...


So what you want to do is develop some kind of pattern/algorithm, which displays them the way you want it to. Basically the algorithm is -

Display the first Letter, which is D, then 5 characters to the right, which is E, then 5 characters to the right, which is T, then 5 characters to the right, which is T.

Now start from second character, 5 characters to the right which is _ ... and so on, until it's displayed like you want it to.

Work hard on this, I'd like to see the progress you make =)
I've been balling around this idea now and i know how it works, but i'm having a really hard time thinking of a simple and clear way of putting it into code. It could be that i'm just extremely tired, but i'll try my best, i'll update you in a PM, or just a reply, if i get something written that's even close to decent. Thanks again for everything and taking the time to help me. :P
Having a good idea on how it works is a great start, putting it to code is the challenge. Give it a day of trying and we'll see how you progress. If you feel you've done your best and is at a point where you're clueless, you can pm me and I'll help you in person via skype or something if you prefer it that way. Goodluck =)

P.S. You'll also need to active the ability to be PM'd.
Last edited on
Topic archived. No new replies allowed.