0

how to translate from python to c++

hi guys, I'm a beginner proger, I wrote python code, but how to convert it into c++ code n,m = map(int, input().split()) for j in range(n): print(''.join(['{:>4}'.format(i + 1 + m * j)for i in range(m)][::pow(-1, j)]))

3rd Mar 2023, 4:47 PM
uncon un
uncon un - avatar
20 Respuestas
+ 8
uncon un , > the first step for you would be in reworking the python code to a *very* basic version like: n = input() n = int(n) m = input() ... also the last line with the list comprehension should be greatly simplified. > then you can start *converting* the code to c++
4th Mar 2023, 11:55 AM
Lothar
Lothar - avatar
+ 2
3x5..its very easy include <iostream> using namespace std; int main() { int n{3},m{5}; for(int j{};j<n;++j){ if((j&1)==0){ for(int i{1};i<=m;++i){cout<<i+m*j<<" ";} }else{for(int u=m;u>0;--u){cout<<u+m*j<<" ";} } cout<<"\n"; } } It's simple way, if you need more, so put some logics also
4th Mar 2023, 10:26 PM
Smith Welder
Smith Welder - avatar
+ 2
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int n, m; cin >> n >> m; for (int j = 0; j < n; j++) { for (int i = 0; i < m; i++) { int num = i + 1 + m * j; if (j % 2 == 1) { num = (j + 1) * m - i; } cout << setw(4) << num; } cout << endl; } return 0; } In this C++ version, the map() function from Python is replaced with the cin function to read input from the user. The loop over the range of n and the inner list comprehension are replaced with nested for loops. The join() function is replaced with the setw() function from the iomanip library to format the output. The pow() function is replaced with the modulo operator % and the conditional statement if (j % 2 == 1) to alternate the direction of the sequence on odd rows. Note that this C++ code assumes that the iostream and cmath libraries have been included at the beginning of the code.
4th Mar 2023, 11:06 PM
Hasan Kuluk
+ 1
why not simply learn C++? if you managed to learn Python, you can learn C++. Even if you can feed the code to ChatGPT and get a C++ equivalent, you'd still have to know C++ to be able to debug it.
5th Mar 2023, 5:20 AM
Bob_Li
Bob_Li - avatar
0
I dont think you can… why do you need to convert it into C++?
4th Mar 2023, 10:05 AM
Stonedev
Stonedev - avatar
0
a study assignment, we were allowed only in c++, and I only know pyton
4th Mar 2023, 10:08 AM
uncon un
uncon un - avatar
0
So you need to find someone who can translate it for you?
4th Mar 2023, 10:10 AM
Stonedev
Stonedev - avatar
0
Yes...
4th Mar 2023, 10:11 AM
uncon un
uncon un - avatar
0
The first one is more successful, but the second one COULD be as sucessful and faster, but its a small risk…
4th Mar 2023, 10:14 AM
Stonedev
Stonedev - avatar
0
Oh shoot i deleted the other post
4th Mar 2023, 10:15 AM
Stonedev
Stonedev - avatar
0
It said the first option was to try to dm a mentor, second explain the code to me and i translate it
4th Mar 2023, 10:16 AM
Stonedev
Stonedev - avatar
0
According to the given numbers n and m, fill a two-dimensional array of size n×m with numbers from 1 to n×m with a “snake”, as shown in the example. Input data format Two numbers n and m are entered, each of which does not exceed 20. Output data format Output the resulting array, allocating exactly 4 to the output of each element the symbol. that is, it should go from one to some number from left to right, and then from right to left
4th Mar 2023, 10:20 AM
uncon un
uncon un - avatar
0
nxm as in n*m? What is snake?
4th Mar 2023, 10:21 AM
Stonedev
Stonedev - avatar
0
let's say we introduce 3×5 1 2 3 4 5 10 9 8 7 6 11 12 13 14 15
4th Mar 2023, 10:24 AM
uncon un
uncon un - avatar
0
why all the numbers?
4th Mar 2023, 10:25 AM
Stonedev
Stonedev - avatar
0
Well, I'm trying to explain what a "snake" is
4th Mar 2023, 10:26 AM
uncon un
uncon un - avatar
0
yes- but why all numbers? i know what 3x5 is…
4th Mar 2023, 10:28 AM
Stonedev
Stonedev - avatar
0
as an example
4th Mar 2023, 10:29 AM
uncon un
uncon un - avatar
0
ah- pls continue
4th Mar 2023, 10:29 AM
Stonedev
Stonedev - avatar
0
bro took my spotlight lmao
4th Mar 2023, 4:06 PM
Stonedev
Stonedev - avatar