I’m an advanced beginner in C++ and I since I do a lot of competitive programming in high school I could really use some tips for faster and more efficient code.
The task is something like this:
As far as I’m aware, I need to create a file (even tho it doesn’t really say in the task) and put 5 strings into it, and I need to output those 5 strings but they need to be decrypted first.
They’re encrypted through a matrix.
So for example a string bcalkcajk has 9 letters so it can be put in a 3x3 matrix.
b c a
l k c
a j k
We decode the text by reading the string column by column , so column “bla” , “ckj” and “ack” , give blackjack.
If the string’s size is not a square of a number , it should read “GRESKA”.
The strings are:
test.in ::::::::::: output
saoreecessinntfi sesnaestocifreni
pmrrj ie2 :::::::: primjer 2
dobar tekst ::::: GRESKA
borj :::::::::::::: broj
Bjsr aoOm ::::::: Broj Osam
The dots are there just to line up the text, I don’t know how to handle these forum texts properly.
The code works, I just want to know if I can improve it in any way and if anybody could explain text files and some tricks, because I just blindly write and read these files, I don’t know how it actually works…
Here’s my code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ofstream write("test.in");
write << "saoreecessinntfi\npmrrj ie2\ndobar tekst\nborj\nBjsr aoOm";
write.close();
string line;
string x[5];
int i=0;
float root;
int size;
int j;
int demolition;
ifstream read("test.in");
if(read.is_open())
{
while(getline(read,line))
{
root=sqrt(line.size());
size=line.size();
if(root==floor(root))
{
demolition=1;
j=0;
for(int k=0;k<size;k++)
{
if(j>=size)
{
j=demolition;
demolition++;
}
cout << line[j];
j=j+root;
}
}
else
{
cout << "GRESKA";
}
cout << "\n";
}
read.close();
}
else
{
cout << "Ne mogu otvoriti taj fajl.";
}
return 0;
}