Question

convert this python code to c++ code def sort(a,b): if a > b: return b,a else:...

convert this python code to c++ code

def sort(a,b):
if a > b:
return b,a
else:
return a,b

def main():
set1 = input('Enter the first pair of integers: ')
set2 = input('Enter the second pair of integers: ')
set3 = input('Enter the third pair of integers: ')

set1 = set1.split(',')
set1_0 = int(set1[0])
set1_1 = int(set1[1])

set2 = set2.split(',')
set2_0 = int(set2[0])
set2_1 = int(set2[1])

set3 = set3.split(',')
set3_0 = int(set3[0])
set3_1 = int(set3[1])
  
print(sort(set1_0,set1_1))
print(sort(set2_0,set2_1))
print(sort(set3_0,set3_1))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question.


Below is the code you will be needing Let me know if you have any doubts or if you need anything to change.


Thank You !


===========================================================================

#include<iostream>
using namespace std;

// function accepts the numbers as reference
void sort(int&a, int&b){
  
   if(a>b){
       int temp = a;
       a=b;
       b=temp;
   }
}

int main(){
  
   int a,b;
   int x,y;
   int m,n;
  
   cout<<"Enter the first pair of integers: ";
   cin>>a>>b;
  
   cout<<"Enter the second pair of integers: ";
   cin>>x>>y;
  
   cout<<"Enter the third pair of integers: ";
   cin>>m>>n;
  
   sort(a,b);
   sort(x,y);
   sort(m,n);
  
   cout<<endl;
   cout<<a<<" "<<b<<endl;
   cout<<x<<" "<<y<<endl;
   cout<<m<<" "<<n<<endl;
  
  
  
}

let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer.

Thanks!


===========================================================================

Add a comment
Know the answer?
Add Answer to:
convert this python code to c++ code def sort(a,b): if a > b: return b,a else:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [...

    convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [ ] if len(nums) == 1: return nums [0] # first find freq freq dict d = {} for num in nums: if num in d: d[num] -= 1 # reverse the sign on the freq for the heap's sake else: d[num] = -1 h = [] from heapq import heappush, heappop for key in di heappush(h, (d[key], key)) res = [] count = 0...

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

  • THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as...

    THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as an integer       a = int(input("Enter value for a: "))       #reading b as an interger       b = int(input("Enter value for b: "))       #reading n as an integer       n = int(input("Enter value for n: "))       # displaying if a and b are congruent modulo n, this is True if a and b       #both returns same remainder when divided by n       if a % n == b...

  • AND logic gate simulation in Python: Please fix the code below so that it efficiently performs...

    AND logic gate simulation in Python: Please fix the code below so that it efficiently performs the operation of the AND logic gate in Python. (Leave comments in the edited code about what you changed) #if both values are 1, return true, other wise return false print ("Logic Gate Simulation: AND gate") def AND(x, y):     if x == True and y == True:         return True        else:         return False    def main():     x = bool(input("Please...

  • Convert Python to Java def read_fsm(filename): fh = open('fsm.txt','r') contents = fh.readlines() sigma...

    Convert Python to Java def read_fsm(filename): fh = open('fsm.txt','r') contents = fh.readlines() sigma = list(contents[0].rstrip().split(' ')) table = {} n = int(contents[1]) for i in range(n): table[i] = {} final = list(map(int,contents[2].rstrip().split(' ')))    for line in contents[3:]: fro,ip,to = line.split(' ') fro = int(fro) to = int(to) table[fro][ip] = to print(table) fh.close() return table,final    def runString(table,final,string): current = 0 for i in string: current = table[current][i] if current in final: print(string,'--> ACCEPT') else: print(string,'--> REJECT')    def readInput(table,final): fh = open('Strings.txt','r')...

  • In Python, revise this code. define convert(s): """ Takes a hex string as input. Returns decimal...

    In Python, revise this code. define convert(s): """ Takes a hex string as input. Returns decimal equivalent. """ total = 0 for c in s total = total * 16 ascii = ord(c if ord('0) <= ascii <= ord('9'): #It's a decimal number, and return it as decimal: total = total+ascii - ord('0') elif ord('A") <= ascii <= ord('F'): #It's a hex number between 10 and 15, convert and return: total = total + ascii - ord('A') + 10 else...

  • I'm trying to run the code in Python below and keep getting invalid syntax. What's wrong?...

    I'm trying to run the code in Python below and keep getting invalid syntax. What's wrong? Thanks for any help! def even(n): if n%2==0 #Enter a Number def main(): n=int(input("Enter a number: ")) #If even print "Number is even" if(even(n)): print("The number is even") #If odd print "Number is odd" else: print("The number is odd") main() #Call the main function

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • I am writing python code. I submitted an assignment but the professor said it was a...

    I am writing python code. I submitted an assignment but the professor said it was a modularization. Can you help me see what part of the code is modularization? Pseudocode: 1. Decalre MainMethod () function: # A. Parameters: Three numbers # B. Put the three numbers in reverse # 2. Main Program # A. Initialize Varibles # B. Accepts three values from the user # C. Invoke MainMethod () function, passing the three numbers as arguments in reverse # D....

  • (python) [30] Implement exception handling with ValueError here in the following code: There are three blanks...

    (python) [30] Implement exception handling with ValueError here in the following code: There are three blanks you need to fill in. Write the answers in the space given for you. class Area: def __init__(self): self.x = 0 def setX(self n): if n<=0: else: self.x = n #main code a = Areal) inputValue = int(input("Enter a Value that is greater than 0")) a.setX(inputValue) print("Invalid Input Value Entered")

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT