Convert code from Python to C ++ | SoloLearn: Learn to code for FREE!

+1

Convert code from Python to C ++

n = input() n = int(n) max = 100 c = [[0 for x in range(max+1)] for x in range(max+1)] for i in range(101): c[i][0] = 1 c[i][i] = 1 for i in range(1, max+1): for j in range(1, i): c[i][j] = c[i-1][j] + c[i-1][j-1] sum=0 for i in range(1, n+1): prod = 1 for j in range(1, i+1): prod *= c[i][j] sum += prod print(sum) ......... My friends, I am learning several new languages โ€‹โ€‹and I am converting any code I write into other languages.๐ŸคŸ๐Ÿ™๐Ÿฅบ ๐Ÿ˜ƒ๐Ÿฅบ๐Ÿ™ Please if anyone can help convert this code from Python to C ++โคโค

12/31/2020 7:33:09 AM

Shadow

43 Answers

New Answer

+3

Shadow i have converted ur python code to c++, here it is: #include <iostream> using namespace std; int main() { int n, i, j; cin >> n; const int max = 100; long double c[max][max] = {0.0}; for(i = 0; i < max; i++) { c[i][0] = 1; c[i][i] = 1; } for(i = 1; i < max; i++) for(j = 1; j < i; j++) c[i][j] = c[i-1][j] + c[i-1][j-1]; long double sum = 0; //long double because sum value could be very long for(i = 1; i < n+1; i++) { long double prod = 1; for(j = 1; j < i+1; j++) prod *= c[i][j]; sum += prod; } cout << to_string(sum); //im using to_string() function so we can see actual number not in e notations. } //end of program I hope this helps u, please try to run this program and also try to learn c++. Happy sololearning!!โ˜บ๐Ÿ‘

+26

Shadow Everyday we see some people come here looking for other people who will do their assignments. We don't mind helping when they show a will to try, but many of them are just looking for a quick getaway. It's obvious that we're not doing them a favour in learning if we do the assignment, rather we cultivate laziness, that they end up not understanding, and into trouble when questioned by lecturers. Please don't take this personal, no one is accusing you of anything. This is just because we need to take care not to destroy one's opportunity to learn, while we're here to help, that's all. Hope you understand ๐Ÿ‘

+8

Shadow as I said, you have just started your C++ course on SoloLearn. Unless you know C++ from some other source, you should first advance in the C++ course. This isn't very hard to do if you know C++. Still, here are some steps you can follow: 1. All statements will end in semicolon 2. All variable names will be prefixed by their types when they are declared for the first time. For example, n is int, so you will declare n as `int n`. 3. You neither have list comprehensions in C++, nor do you need it in your code. Just replace the comprehension with the declaration of 2 dimensional int array with each inner array of size`max` 4. For loop can easily be changed as you are only iterating on a range 5. Converting input and output to the respective ways in C++ is also easy If you know C++, you will be able to follow the steps, else just learn more C++. Why else would you want to convert the code? Happy coding!

+6

Please show us your attempt first. Don't ask people to do work for you. Also, I recommend advancing furthur in the C++ course which you have just started.

+6

Thank you, but I learn so fast and I understand that I will find. If I have the code of this program in C + language, it is better for me to learn it. And I can compare it to Python, it gets easier. I started C ++ from scratch but it was hard for me to learn. Thanks, I think I have to start from scratch to get the answer, but I learned better that way.

+5

Shadow I do not at all think your question is bad. I just don't understand why would you want this converted to C++, when you don't know enough C++ to understand the converted code. That is why I want YOU to first learn, and then do it yourself. It will only benefit you and you'll get a stronger hold on C++

+5

I know, but I explained that this is not for me to escape and be lazy, but to learn faster, and it may be strange, but if I have programming and I would like to learn several languages together and do it for me. It is better and makes learning better. It's good that you are trying to do this so that others can answer for themselves, but my situation is different and why do you put these friends here and if you want to accept what you say and ask for help and guidance . Anyway, thank you, but I never intend to be lazy๐Ÿ˜Šโค๐Ÿ™

+5

Shadow Thanks for understanding, And I didn't say you intend to be lazy, just said some people. I will leave you to continue your study with C++ now. As your knowledge expands and you get a clearer idea how to do this, make a sketch of the code and post it here. I'll try to help as I can, and when I can (or others will) ๐Ÿ‘Œ

+3

And you have the right not to give me the code or I am doing something wrong but I am still trying to find the C ++ code of this program and convert them together and get to know C ++ better I think my learning will be better. thanks anyway

+2

there is no need to learn multiple languages at once( i already tried it and well now i know none of them ) and as a beginner to programming world you have to undrestand how to search your problem in google and stackOverFlow anyway for your answer there is something called Nuika on github or maybe you can convert it to c and then convert that to c++ again

+2

Flash Matin PH there is no need of mentioning these projects. All Shadow wanted was the mentioned program to be converted to C++ just for learning purposes so they could relate the code written in C++ to the mentioned Python code. That is why me and Ipang said that they should first learn C++ and then convert it themselves so they could learn better. Happy coding๐Ÿ‘!

+2

Shadow just google, but this may not be useful for you as xxx mentioned. personally I learned Python then C and C++ and my first language was Java. so, I think you could do that as well. one more thing, instead of just converting line by line, try to learn solving the problem in C++ way. it could be useful.

+1

And if you look at my page you will see that I started C ++ but I still can't convert the code I wrote to Python to C ++๐Ÿ˜Š

+1

True, this can be bypassed-but not unless you're a techie who knows what he's doing. I do not know why you think my question is so bad? I just wanted to do it the faster I learn. I was told here that friends can help you.๐Ÿฅบ๐Ÿคทโ€โ™€๏ธ๐Ÿคทโ€โ™€๏ธ

+1

Bhavya youโ€™re doing four SL courses yourself! maybe you know the answer. I donโ€™t see learning multiple languages is a sin if your brain can handle it.

+1

Bhavya What is Shiny Body Syndrome? I go to university and I have to learn languages that I do not need, for example, I know Python, but I have to solve C ++ for university exercises, and this has made it difficult for me, and I have to use other languages besides my main language. Learn too๐Ÿฅบ๐Ÿคฆ๐Ÿปโ€โ™€๏ธ

+1

Flash That you said is the site?

+1

Flash Thanks, but I wanted to do all my time for the language I like. But I have to learn other languages as well. Anyway, thank you for your guidance ๐Ÿ™โค

+1

Shadow My suggestion is learn one language first learn the concepts in it when you grab these concepts it will be easy to learn other language and do one language one time if you keep shifting on here to there you can't learn one language properly and your question will be but many people learn multiple languages this is because they have devote their time to single language and get stronger their concepts that's why they can get good hold on multiple languages.....

+1

I am New beginner programer thank you for all the support