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...
Write a program that asks the user for an integer and then prints out all its...
Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop.in c++
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 prints out all numbers, less than or equal to 10,000,000, that are...
Write a program that prints out all numbers, less than or equal to 10,000,000, that are evenly divisible by all numbers between 5 and 15 (inclusive). Do not use any libraries besides stdio.h. Need it in 10 minutes, please.
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT