turn string 90 degrees

Pages: 12
how to turn the rows 90 degrees using a matrix ? help please !!!


Input
c plusplus
Developer

Output
cD
 e 
pv
le
ul
so
pp
le
ur
s
Last edited on
The operation is called transpose.

Assuming your matrix is 1-d mask for 2-d,

for(k = 0; k < rows; k++)
{
for(j = 0; j < cols; j++)
{
dest[j*rows + k] = src[k*cols +j];
}
}

where rows and cols are the dimensions of the source.

The 2-d algorithm is the same, just doubled index instead of computed, and you can't transpose in place (though I didn't do that here, and you need a temp/swap approach to do it in place ).

Last edited on
jonnin can I write a PM to you ?
sure. But if you post here everyone can benefit. Its usually preferred to share the info?
how to turn the rows 90 degrees using a matrix

what does it mean to turn rows 90 degrees? is it just printing vertically keeping the underlying container unchanged or you'd like to turn a container of 1 row and n elements into n containers with 1 element each?
if you look at his example, its just transpose (rows become columns). Though what it means to the data structure is going to matter.

Last edited on
if you look at his example, its just transpose (rows become columns)

not necessarily:
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>

int main()
{
    std::string inputOne{}, inputTwo{};
    getline(std::cin, inputOne);
    getline(std::cin, inputTwo);
    std::vector<std::string> inputOneTwo{};
    for (size_t i = 0; i < std::max(inputOne.size(), inputTwo.size()); ++i)
    {
        inputOneTwo.push_back(std::move(std::string{inputOne[i]} + std::string{inputTwo[i]}));
    }
    for (const auto& elem : inputOneTwo)
    {
        std::cout << elem << "\n";
    }
}
/*
c plusplus
Developer
cD
 e
pv
le
ul
so
pp
le
ur
s
*/

and I bet there are myriad other ways of achieving the same output from the same inputs using one or more containers
You could regard a vector<string> as a matrix of chars, so push_back your successive lines into a container like that.

Then just print out:
- all the 0 elements of the strings, followed by newline;
- all the 1 elements of the strings, followed by newline
...
until you reach the largest length of string that you have entered.

Different strings have different lengths, so you must watch for this and print out a blank at that point, rather than trying to take data from beyond bounds.
in the task given to me here this part of code

1
2
3
4
5
6
7
8
9
10
int n;
std::cin >> n;
std::string restOfFirstLine;
std::getline(std::cin, restOfFirstLine);
for(int i = 0; i < n; ++i)
{
    std::string currentLine;
    std::getline(std::cin, currentLine);
    ….
}


I don't know where to start

gunnerfunner I can't use
#include <utility>
and
std::move
because not taught this

I can't use
#include <utility>
and
std::move
because not taught this

OK, the program can survive without these, line 15 then becomes just:
 
inputOneTwo.push_back(std::string{inputOne[i]} + std::string{inputTwo[i]});

edit: also suggest using std::string::size_type for the type of the loop variable
Last edited on
gunnerfunner ok, thanks ... I'll try to do something ))
and taking lastchance's suggestion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
    std::string inputOne{}, inputTwo{};
    getline(std::cin, inputOne);
    getline(std::cin, inputTwo);
    for (std::string::size_type i = 0; i < std::max(inputOne.size(), inputTwo.size()); ++i)
    {
        std::cout << inputOne[i] << inputTwo[i];
       std::cout << "\n";
    }
}

edit: both this and above post can be inserted into the code outline you've been supplied, just be careful about using int n, ideally it should be std::string::size_type
Last edited on
well, but I also can't use std::string::size_type because they also did not say anything about it :D
for two lines I understood but I can't understand how to make this for different quantities

write something like this ?

 
for (std::string::size_type i = 0; i < std::max(currentLine.size(), restOfFirstLine.size()); ++i)
write something like this ?

yes with int as type for variable i as per your restriction
see once I have it all in a loop to n

1
2
3
4
5
6
for(int i = 0; i < n; ++i)
{
    std::string currentLine;
    std::getline(std::cin, currentLine);
    ….
}


it turns out that this part also needs to be in it

1
2
3
4
5
for (int j = 0; j < max(currentLine.size(), restOfFirstLine.size()); ++j)
		{
			std::cout << currentLine[j] << restOfFirstLine[j];
			std::cout << "\n";
		}


but the program gives an error string subscript out of range
send your full program, start to finish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

int main()
{
	int n;
	std::cin >> n;
	std::string restOfFirstLine;
	std::getline(std::cin, restOfFirstLine);
	for(int i = 0; i < n; ++i)
	{
		std::string currentLine;
		std::getline(std::cin, currentLine);
		for (int j = 0; j < std::max(currentLine.size(), restOfFirstLine.size()); ++j)
		{
			std::cout << currentLine[j] << restOfFirstLine[j];
			std::cout << "\n";
		}
	}
}
on cpp.sh this result


2
c plusplus
c
 
p
l
u
s
p
l
u
s
Developer
D
e
v
e
l
o
p
e
r


but in Visual Studio gives an error
Last edited on
you probably cannot use std::max() as well and so I have taken that out:
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
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>

int main()
{
	int n{};
	std::cout << "Enter n \n";
	std::cin >> n;
	std::cin.ignore();
	std::string restOfFirstLine{};
	std::cout << "Enter rest of first line \n";
	std::getline(std::cin, restOfFirstLine);
	for(int i = 0; i < n; ++i)
	{
		std::string currentLine{};
		std::cout << "Enter current line \n";
		std::getline(std::cin, currentLine);
		int J;
		J = (currentLine.size() > restOfFirstLine.size())? currentLine.size() : restOfFirstLine.size();
		for (int j = 0; j < J; ++j)
		{
			if (j < currentLine.size())std::cout << currentLine[j];
			if (j < restOfFirstLine.size())std::cout << restOfFirstLine[j];
			std::cout << "\n";
		}
	}
}
/*
Enter n
3
Enter rest of first line
c plusplus
Enter current line
developer
dc
e
vp
el
lu
os
pp
el
ru
s
Enter current line
tomorrow
tc
o
mp
ol
ru
rs
op
wl
u
s
Enter current line
donald trump!!!
dc
o
np
al
lu
ds
 p
tl
ru
us
m
p
!
!
!
*/
Last edited on
Input

3
Hello World
abcd
a b c d


Output


Haa
eb
lcb
ld
o c
W d
o
r
l
d



but in code output is like this

Enter n 
3
Enter rest of first line 
Hello World
Enter current line 
abcd
aH
be
cl
dl
o
 
W
o
r
l
d
Enter current line 
a b c d
aH
 e
bl
 l
co
  
dW
o
r
l
d
Pages: 12