Question

Be sure to create the algorithms (pseudocode or flowcharts) for all 4 functions plus the main...

  • Be sure to create the algorithms (pseudocode or flowcharts) for all 4 functions plus the main program.
  • Create a very simple larger( ) function that takes 2 positive numbers as parameters and returns the larger value of the two numbers.   
  • Use your larger function to help create the following functions -- reuse your function code.
  • Create a simple larger3( ) function that takes 3 positive numbers as parameters and returns the largest value of the three numbers.  
  • Create a simple larger4( ) function that takes 4 positive numbers as parameters and returns the largest value of the four numbers.   
  • Create a  largest( ) function that takes an array of positive numbers and the array size as parameters and returns the largest value from the array.   
  • Create a main( ) program that calls and tests all four of the above functions to insure that they work.
  • Create 1 program source file that include the main and the other 4 functions

To get credit for the lab, submit the source file,  5 algorithms and a screen shot of the running program.

Homework Answers

Answer #1

C++ PROGRAM

#include<iostream>
using namespace std;

// implement larger() function with two integer parameters
// return integer value
int larger(int x,int y)
{
   if(x>y) // check x>y then
   return x; // return x value
   else
       return y; // else, return y value
}

// implement larger3() function with three integer parameters
// return integer value
int larger3(int x,int y,int z)
{
   int big=larger(x,y); // calling larger() function and receive biggest value in 'big'
  
   if(big>z) // check big>z then,
       return big; // return big value
   else
       return z; // else, return z value
}

// implement larger4() function with four integer parameters
// return integer value
int larger4(int x,int y,int z, int n)
{
   int big1=larger(x,y); // calling larger() function and receive biggest value in 'big1'
   int big2=larger(z,n); // calling larger() function and receive biggest value in 'big2'
  
   if(big1>big2) // check big1>big2 then,
       return big1; // return big1 value
   else
       return big2; // return big2 value
}

// implement largest() function with two parameters
// first parameter is integer array and second paramter integer argument
// return integer value

int largest(int a[],int size)
{
   int big=larger(a[0],a[1]); // calling larger() function and receive biggest value in 'big'
   for(int i=0;i<size;i++) // create for loop until size
   {
       if(a[i]>big) // check array element a>big
       big=a[i]; // then, assign biggest value into 'big'
   }
   return big; // finally return value
}

int main()
{
   int x,y,z,n; // declare 4 integer variable
   int a[100],size; // declare integer array a and integer variable size
  
   cout<<"Enter Positive integer x,y,z and n values: ";
   cin>>x>>y>>z>>n; // Read 4 integer variables
   cout<<"Enter Array Size: ";
   cin>>size; // Read size of an array
   cout<<"\nEnter Positive "<<size<<" Elements in Integer array :";
   // input array elements
   for(int i=0;i<size;i++) // create for loop until size of an array
       cin>>a[i]; // read array elements
  
   // Display original values  
   cout<<"\n\nX = "<<x<<", Y = "<<y<<", Z = "<<z<<", N = "<<n<<endl;
   cout<<"Original Array Elements\n";
   for(int i=0;i<size;i++)
       cout<<a[i]<<" ";
  
   int b1=larger(x,y); // callng larger() function and receive biggest number into b1
   int b2=larger3(x,y,z); // calling larger3() function and receive biggest number into b2
   int b3=larger4(x,y,z,n); // calling larger4() function and receive biggest number into b3
   int b4=largest(a,size); // calling largest() function and recieve biggest number into b4
  
   // dipslay all biggest values
   cout<<"\n\nLarger Value: "<<b1<<endl;
   cout<<"Larger 3 Value: "<<b2<<endl;
   cout<<"Larger 4 Value: "<<b3<<endl;
   cout<<"Array Largest Value: "<<b4<<endl;
  
  
  
   return 0;
}

OUTPUT

Enter Positive integer x,y,z and n values: 25 689 5 66
Enter Array Size: 5

Enter Positive 5 Elements in Integer array :58 11 489 6 78


X = 25, Y = 689, Z = 5, N = 66
Original Array Elements
58 11 489 6 78

Larger Value: 689
Larger 3 Value: 689
Larger 4 Value: 689
Array Largest Value: 489

ALGORITHMS

Algorithm for larger() function with two integer arguments are x and y

Step 1: Start
Step 2: Read two Integer values x and y
Step 3: if x>y
   Display x is the Biggest Number
   Else
   Display y is the Biggest Number
Step 4: Stop

Algorithm for larger3() function with three integer arugments are x,y and z

Step 1: Start
Step 2: Read three Integer values x,y and z
Step 3: calling larger() function and store into integer variable big i.e.,
       big=larger(x,y)
Step 4: if big>z
       Display big is the Biggest number
   Else
       Display z is the Biggest number
Step 5: Stop

Algorithm for larger4() function with four Integer arguments area x,y,z and n

Step 1: Start
Step 2: Read 4 Integer values x,y,z and n
Step 3: calling larger() function and store into Integer variables big1 and big2
   i.e.,
   big1=larger(x,y)
   big2=larger(z,n)
Step 4: if big1 > big2
       Display big1 is the Biggest number
   Else
       Display big2 is the Biggest number
Step 5: Stop

Algorithm for largest() function with two arguments
First argument is Integer Array a[] and Second Argument is integer variable size

Step 1: Start
Step 2: Read Size of and Array size
Step 3: Read Integer Array Elements a
Step 4: calling larger() function like
   big=larger(a[0],a[1]) first and second elements in the array
Step 5: Set a for loop
Step 6: if a[i]>big then,
big=a[i]
Step 7: Display Biggest number is big
Step 8: Stop


Algorithm for main() function

Step 1: Start
Step 2: Read 4 Integer variables x,y,z,n and size and Integer Array a
Step 3: Read Array size
Step 4: Set a for loop
Step 5: Read Array elements a
Step 6: calling larger() function and receive return value into b1
b1=larger(x,y)
Step 7: calling larger3() function and receive return value into b2
   b2=larger3(x,y,z)
Step 8: calling larger4() function and receive return value into b3
   b3=larger4(x,y,z,n)
Step 9: calling largest() function and receive return value into b4
   b4=largest(a,size)
Step 10: Display b1,b2,b3,b4
Step 11: Stop

Know the answer?
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
Similar Questions
Q: Design a program that lets the user enter the total rainfall for each of 12...
Q: Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Create parallel arrays for the month names and rainfall amounts for each month. Use month names (i.e. January, February, March, etc.) when printing out the months with the highest and lowest amounts. Include a modular...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Module 4 Assignment 1: Pseudocode & Python with Variable Scope Overview Module 4 Assignment 1 features...
Module 4 Assignment 1: Pseudocode & Python with Variable Scope Overview Module 4 Assignment 1 features the design of a calculator program using pseudocode and a Python program that uses a list and functions with variable scope to add, subtract, multiply, and divide. Open the M4Lab1ii.py program in IDLE and save it as M4Lab1ii.py with your initials instead of ii. Each lab asks you to write pseudocode that plans the program’s logic before you write the program in Python and...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following:...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following: 1. The main program calls the function enterNum 3 times. The first time enterNum is called, the main program stores the returned output in the variable xx. The second time, the returned output is stored in the variable yy and the third time in zz. 2. enterNum asks the user to enter a floating point number. This function has no input arguments and returns...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
Step 1: Select any four sorting algorithm and two searching algorithms Step 2: Understand the logic...
Step 1: Select any four sorting algorithm and two searching algorithms Step 2: Understand the logic of all the algorithms Step 3: Create java program and use your sorting/searching source codes and integrate it into your main java project. Step 4: Create a separate java class for each algorithm Step 5: Create a random function that generates at least 100000 random integer numbers from 1 to 1 million(No need to print out or store the numbers) Step 6: Insert start...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
Objectives: In this lab, you need to modify your functions (createArray, getArraySize, and freeArray) based your...
Objectives: In this lab, you need to modify your functions (createArray, getArraySize, and freeArray) based your pre-lab. In createArray function, an integer array needs to be created with its size and the maximum value in this array stored in front as two integers. After creating the array, your array should look like this: max size Array[0] Array[1] ... Array[n-1] You also need to modify the other two functions accordingly. Main program steps: 1.Create an array like mentioned above with 10...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT