question from a newbie:
can somebody help me in converting this c++ code to python... -
bool flag;
-
do{
-
flag=0;
-
for(int i=0;i<n;i++){
-
double s1=xold[i];
-
double s2=xnew[i];
-
e[i]= s1-s2;
-
if (e[i]<0) {e[i]=0-e[i];}
-
if(e[i]>prec){flag=1;}
-
-
for(int i=0;i<n;i++){
-
xold[i]=xnew[i];
-
}
-
-
}
-
}while (flag);
-
basically the bool thing is a prob... also if i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this: -
-
def display(parameter1, parameter2):
-
-
{code of display}
-
-
solve(parameter3, parameter4):
-
-
{code of solve and it gives me output its output in the form of a number}
-
-
#now i want to return control to display
-
-
{remaining code of display continues...}
-
3 1688
question from a newbie:
can somebody help me in converting this c++ code to python... -
bool flag;
-
do{
-
flag=0;
-
for(int i=0;i<n;i++){
-
double s1=xold[i];
-
double s2=xnew[i];
-
e[i]= s1-s2;
-
if (e[i]<0) {e[i]=0-e[i];}
-
if(e[i]>prec){flag=1;}
-
-
for(int i=0;i<n;i++){
-
xold[i]=xnew[i];
-
}
-
-
}
-
}while (flag);
-
<snip>
I'm not so hot with C, but it looks like the while will never execute due to flag=0, but (or maybe it executes once because while() is evaluated last) - I'll show the latter: - n = 4 # counter for for()
-
flag = 0
-
while True: # Note that python is indentation based
-
for i in range(n): # use xrange() for huge numbers
-
s1 = xold[i]
-
s2 = xnew[i]
-
e[i] = s1 - s2
-
if e[i] < 0: # colon starts a block, next de-dent ends it
-
e[i] = 0 - e[i]
-
if e[i] > prec:
-
flag=1
-
# comments can go on any indent level - etc...
-
if not flag:
-
break
That is, if I'm reading the C part correctly. Close, anyway
I'm not so hot with C, but it looks like the while will never execute due to flag=0, but (or maybe it executes once because while() is evaluated last) - I'll show the latter: - n = 4 # counter for for()
-
flag = 0
-
while True: # Note that python is indentation based
-
for i in range(n): # use xrange() for huge numbers
-
s1 = xold[i]
-
s2 = xnew[i]
-
e[i] = s1 - s2
-
if e[i] < 0: # colon starts a block, next de-dent ends it
-
e[i] = 0 - e[i]
-
if e[i] > prec:
-
flag=1
-
# comments can go on any indent level - etc...
-
if not flag:
-
break
That is, if I'm reading the C part correctly. Close, anyway
Anything that has a non-zero value will evaluate as True.
bool is a subclass of int so you can do many things with python bools that other languages wouldn't dream of doing (including list indexing): - timeToStop = True
-
print ['still going', 'stopping'][timeToStop]
Kind of nifty, huh?
question from a newbie:
can somebody help me in converting this c++ code to python... -
bool flag;
-
do{
-
flag=0;
-
for(int i=0;i<n;i++){
-
double s1=xold[i];
-
double s2=xnew[i];
-
e[i]= s1-s2;
-
if (e[i]<0) {e[i]=0-e[i];}
-
if(e[i]>prec){flag=1;}
-
-
for(int i=0;i<n;i++){
-
xold[i]=xnew[i];
-
}
-
-
}
-
}while (flag);
-
basically the bool thing is a prob... also if i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this: -
-
def display(parameter1, parameter2):
-
-
{code of display}
-
-
solve(parameter3, parameter4):
-
-
{code of solve and it gives me output its output in the form of a number}
-
-
#now i want to return control to display
-
-
{remaining code of display continues...}
-
First of, Only becouse C++ lets you make your code 100% unreadable does not mean you shuld do it. -
bool flag;
-
do{
-
flag = 0;
-
for(int i = 0; i < n; i++)
-
{
-
double s1 = xold[i];
-
double s2 = xnew[i];
-
-
e[i] = s1 - s2;
-
-
if (e[i] < 0)
-
e[i] = 0 - e[i];
-
-
if(e[i] > prec)
-
flag = 1;
-
-
for(int i = 0; i < n; i++)
-
xold[i] = xnew[i];
-
}
-
}while(flag);
And as for your second Q, -
def solver(arg1, arg2):
-
return arg1 ** arg2 # Ofcource you will replace this with whatever you want it to do
-
-
def display(no1, no2):
-
# Code
-
solvednum = solver(no2, no1)
-
# more code
-
Simply create multiple functions.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Emile van Sebille |
last post by:
QOTW: "If we get 2.3.3c1 out in early December, we could release 2.3.3 final
before the end of the year, and start 2004 with a 100% bug-free codebase
<wink>." -- Tim Peters
"cjOr proWe vbCould...
|
by: Joe Rattz |
last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to
intialize them to false.
bool bits = new bool;
foreach(bool bit in bits)
{
bit = false;
}
The compiler complains...
|
by: Randeh |
last post by:
Short story: in a beginning C++ class in college, was in a car accident that caused central spinal stenosis, some new Schmorl's nodes, disc problems and an incredible amount of pain. So far I've...
|
by: fgh.vbn.rty |
last post by:
I know that std::vector<boolis specialized to store individual bools
as single bits. I have a question about the comparison operation on a
pair of vector<bool>s.
Is the comparison done bit by...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |