2
#include<iostream> #include<conio.h> #include<math.h> #include<vector> #include<iterator> #include<string> using namespace std; int main() { int k=0; string s; cout<<"string "; getline(cin,s); //taking in a string from the user float n=s.size(); //storing size of string int f=floor((sqrt(n))); //floor of square root of input string int c=ceil((sqrt(n))); //ceiling int m=f*c; //storing product of f and c vector< vector<string> > vec(n<=m?f:++f, vector<string>(c)); //makes a 2d vector //depending on user's //string length for(int i=0;n<=m?i<f:i<++f;i++) //looping acc to user's input and assigning { for(int j=0;j<c;j++) //string to a matrix { if(k<s.size()) { vec[i][j]=s[k]; k++; } } } for(int j=0;j<c;j++) //printing the vector { { for(int i=0;n<=m?i<f:i<++f;i++) cout<<vec[i][j]; }cout<<" "; } getch(); } 

It's not working for n>m as for a string of length 8 characters it makes a vector of 2*3 thus failing to enclose the whole string in the matrix and which is why I am using ternary so as to make a vector of bigger size when it encounters cases like these. .So what am I doing wrong?

I'll just write the whole question.

One classic method for composing secret messages is called a square code. The spaces are removed from the english text and the characters are written into a square (or rectangle). The width and height of the rectangle have the constraint, floor(sqrt(word)) <= width, height <= ceil(sqrt(word)) The coded message is obtained by reading down the columns going left to right. For example, the message above is coded as: imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau Sample Input: chillout Sample Output: clu hlt io 
14
  • 3
    Could you a) format the code nicely and b) explain the principle idea of the algorithm. It's time consuming to guess that out of the code (that even doesn't work correctly, yet) Commented Oct 10, 2013 at 15:16
  • 1
    This code is unreadablePh'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn Commented Oct 10, 2013 at 15:17
  • 1
    Big +1 for the Cthulu reference. ;) Commented Oct 10, 2013 at 15:33
  • Haha I am being ripped apart here.But please bear with me as I am new to programming as well as this site.I have edited my post and I hope the code is more readable now. Commented Oct 10, 2013 at 15:36
  • You should have been immediately able to see that this: vector< vector<string> > vec(n<=m?f:++f, vector<string>(c)); is just wrong, without having to even read it. Use of the comma operator in a ternary expression, one clause of the expression having type int, the other having type std::vector<string> - it's just obviously and intuitively wrong. Write something simpler, more obvious, and more expressive of what you're trying to achieve, particularly if you're "new to programming". Commented Oct 10, 2013 at 15:36

1 Answer 1

2

This won't fix your entire problem, but I still feel it is important. You seem to misunderstand how the ternary works. Let's observe one of its uses here:

for (int i = 0; n <= m ? i < f : i < ++f; i++) {} // ^^^^^^^^^^^^^^^^^^^^^^^^ <--- not the intended outcome 

This will not work because the returned side of the ternary does not "stick" itself in-place. In other words, neither i < f nor i < ++f will be put directly into the for-loop. Instead, it'll give you a value.

To see what it's really doing, you'll first need to understand that the ternary is just another way to do an if-else. The ternary above, put into if-else form, looks like this:

if (n <= m) i < f; // left side of the ":" else i < ++f; // right side of the ":" 

Let's break it down further:

i < f 

This is doing a less-than comparison of i and f. So, depending on the individual values, you'll receive either a 0 (false) or a 1 (true).

So, in your for-loop, this will occur:

for (int i = 0; 1; i++) {} // ^ <--- if comparison returns true for (int i = 0; 0; i++) {} // ^ <--- if comparison returns false 

So, for your example, you'll need to find the value of f before the loop. You can use a ternary for that part, but only if you understand it. Otherwise, use another method to find f (the intended numerical value). Once you find it, then you can put i < f into the for-loop.

Sign up to request clarification or add additional context in comments.

5 Comments

How do you grey out code in comments?My above comment is pretty difficult to comprehend right now.
@Ratul: Put a backtick (`) between the text.
I have stored the value in f before the loop I think.For example let the length of string be 8.So f will store (floor(sqrt(8))) ie 2 and c=3 and m=2*3 ie 6.So now to assigning the size to the vector.The ternary inside the vector will check if (8<=6) (row=2) else(row=++f ie 3) and then the for loop will work according to the vector created ie either i<2(if true) or i<3(if false)
This is already there in the code that I posted here.So in a way no would be the answer.
@Ratul: Okay. I was able to reproduce your issue, but I still can't quite find the solution. Just keep looking over the code to determine why the loop is going out of range.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.