1) What is the purpose of the code below?  2) What is each function used for in the code? 3) What are the names of the parameters/ arguments and what are they responsible for?  4) Why did you use a while loop in you code?  5) How did you accomplish the sort() to sort numbers? Code:

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 8PE
icon
Related questions
Question

1) What is the purpose of the code below? 

2) What is each function used for in the code?

3) What are the names of the parameters/ arguments and what are they responsible for? 

4) Why did you use a while loop in you code? 

5) How did you accomplish the sort() to sort numbers?

Code:

#predefined modules
import random
import math

#function to sort the list in ascending order
def sort(x):
    #predefined function sort()
    x.sort()
    #print the sorted list
    print("\nSorted list is: ",str(x))

#function to find the sum of list elements
def sum_of_list(x):
    #predefined function sum()
    Sum=sum(x)
    #return the sum of list elements
    return Sum

#function to list the maximum from the list
def list_max(x):
    #predefined function max()
    maximum=max(x)
    #return maximum
    return maximum

#function to test the above three function
def main():
    #set a flag variable
    flag=True
    #create a list
    list1=list()
    #initialize the list element by using randrange() predefined function  of random module
    list1=[random.randrange(1, 50, 1) for i in range(0,7)]
    #print the original list
    print("\nThe list element is shown below:\n" + str(list1))
    #repeatative strurure while
    while(flag):
        # print the menu
        print("\n******** Menu ********\n1. Sort the element\n2. Find the maximum from the list\n3. Find the sum of list element\n4. To exit the program")
        # input user choice
        ch = int(input("\nEnter your choice: "))
        #decision struture
        #if ch is 1
        if(ch==1):
            # sort the list
            sort(list1)
        #otherwise, if ch is 2
        elif(ch==2):
            # print the maximum from list
            print("\nMaximum from the list element is: ", list_max(list1))
        #otherwise, if ch is 3
        elif(ch==3):
            #print the sum of list
            print("\nSum of list element is: ",sum_of_list(list1))
        #otherwise
        else:
            flag=False

#function call
main()

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning