Question

In: Computer Science

Write a program calls each of the three methods described below and prints out the result...

Write a program calls each of the three methods described below and prints out the result of the two methods which return value

reverse- a void method that reverses the elements of the array. Print the array before you call the method and after call the method.
getMin- an int method that returns the smallest value in the array

Sample output
array: 22,34,21,35,12,4,2,3,99,81
array in reverse: 81,99,3,2,4,12,35,21,34,22
the smallest number is 2

Solutions

Expert Solution

PROBLEM :

To reverse the array and find the minimum element in the array.

----------------------------------------------------------------------------------------------------------------

EXPLANATION / ALGORITHM :

  1. Reverse the array-
  • We will take 2 variables - 'left' and 'right'.
  • We will swap array[left] with array[right] and increment the 'left' and decrement the 'right' till left is less than right.

       2. Find the smallest element-

  • Assume that first element of the array as the current smallest element.
  • Compare the current smallest element with other elements
  • If the current element is smaller than the current smallest element, update the current smallest element.

Code is written in C++.

-----------------------------------------------------------------------------------------------

CODE:

#include <bits/stdc++.h>
using namespace std;

void reverse(int array[], int left, int right)
{
    while (left < right) //run while loop till lef is less than right
    {
        int var = array[left];   // Next 3 lines swaps the elements
        array[left] = array[right];
        array[right] = var;
        left++; //increment the left
        right--; //decrement the right
    }
}    

void displayElements(int array[], int size) //function to print the elements of the array
{
   for (int i = 0; i < size; i++)
   {
       cout << array[i] <<" ";
   }
   cout << endl;
}

int getMin(int array[],int size)
{
    int smallest = array[0]; //initialise first element as the smallest element
    for (int i = 1; i < size; i++)
   {
       if(array[i] < smallest) //if current element is less the smallest update the smallest
            smallest = array[i];
   }
   return smallest;
}

int main()
{
    int array[] = {22,34,21,35,12,4,2,3,99,81};
    int size = sizeof(array) / sizeof(array[0]);
    cout<<"array:";
    displayElements(array, size); //print the array
    cout<<"array in reverse:";
    reverse(array, 0, size-1); // reverses the elements of the array
    displayElements(array, size); //After reverse print
    cout<<endl;
    cout<<"The smallest number is : "<<getMin(array,size-1); //prints the smallest number
    return 0;
}

OUTPUT:


Related Solutions

Write a short program that asks the user for a sentence and prints the result of...
Write a short program that asks the user for a sentence and prints the result of removing all of the spaces. Do NOT use a built-in method to remove the spaces. You should programmatically loop through the sentence (one letter at a time) to remove the spaces.
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
In Python write a program that calculates and prints out bills of the city water company....
In Python write a program that calculates and prints out bills of the city water company. The water rates vary, depending on whether the bill is for home use, commercial use, or industrial use. A code of r means residential use, a code of c means commercial use, and a code of i means industrial use. Any other code should be treated as an error. The water rates are computed as follows:Three types of customers and their billing rates: Code...
Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a program that generates and prints out in a neat format all possible truth tables...
Write a program that generates and prints out in a neat format all possible truth tables with three propositional variables p, q, and r. Your program should also number generated truth tables and output the numbers so it is clear how many total tables were generated. Your program output may look like the following (Note: only the beginning of the output is shown): Truth table 1: p q r proposition ----------------- F F F F F F T F F...
Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
Python # Write a program that examines three variables—x, y, and z # and prints the...
Python # Write a program that examines three variables—x, y, and z # and prints the largest odd number among them. # If none of them are odd, it should print a message to that effect. n = input('Enter the 1st Integer x: ') x = int(n) n = input('Enter the 2nd Integer y: ') y = int(n) n = input('Enter the 3rd Integer z: ') z = int(n) if x % 2 == 0 and y % 2 ==...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT