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 ).
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?
#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 (constauto& 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.
#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
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)
#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
!
!
!
*/