Question

Write a method/function to find the smallest number in an array of type int. Declare the...

Write a method/function to find the smallest number in an array of type int. Declare the array size to be 7 and fill the array from the keyboard. The method should take the array as a parameter and return the smallest number as the return value.

using c++

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

Code

#include <iostream>
using namespace std;
int findSmallestElement(int arr[],int n){
   int temp = arr[0];
   for(int i=0; i<n; i++) {
      if(temp>arr[i]) {
         temp=arr[i];
      }
   }
   return temp;
}
int main() {
   int n=7;
   int arr[n-1];
   cout<<"Enter array elements: ";
   for(int i=0; i<n; i++){
      cin>>arr[i];
   }
   int smallest = findSmallestElement(arr, n);
   cout<<"Smallest Element is: "<<smallest;
   return 0;
}

output

Add a comment
Know the answer?
Add Answer to:
Write a method/function to find the smallest number in an array of type int. Declare the...
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
  • a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a...

    a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a statement that declares an array namedstreetAddress that contains exactly eighty elements of typechar. 2. In a single statement: declare, create and initialize an arraynamed a of ten elements of type int with the values of the elements (starting with the first) set to 10, 20,..., 100 respectively. 3. Declare an array reference variable, week, and initialize it to an array containing the strings "mon",...

  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • (Find the smallest element) use pointers to write a function that finds the smallest element in...

    (Find the smallest element) use pointers to write a function that finds the smallest element in an array of integers. Use {1,2,4,5,10,100,2,-22} to test the function. using the following header. int minindex(double* list, int size) example output: array size: 8 number 1: 1 number 2: 2 number 3: 4 number 4: 5 number 5: 10 number 6: 100 number 7: 2 number 8: -22 smallest element is: -22 C++ only.

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Write a templated function sumList that will take in an array of any type and the...

    Write a templated function sumList that will take in an array of any type and the length of the array. It should add all the elements in the array and return the total. Hint: you can declare a variable sum of type T and initialize it like this: T sum {}; The {} are the best way to make sure that numbers get set to 0, a string gets created as empty etc... #include <iostream> using namespace std; //Do not...

  • C PROGRAMMING ONLY 22 2 points Write a function for the following specs: • type: int...

    C PROGRAMMING ONLY 22 2 points Write a function for the following specs: • type: int • parameter: character array • Behavior: o Open the file using the parameter for the file name in read mode. o Return O if the file opened successfully, 1 if unable to open the file. В І о A- A Ex x, EE 12 23 2 points Write a function for the following specs: type: void • parameters: FILE", int[], int size Behavior: o...

  • c++ Write two versions of a function that returns a dynamic array consisting of all prime...

    c++ Write two versions of a function that returns a dynamic array consisting of all prime numbers less than or equal to the int parameter n. You may declare the dynamic array to have length n, even though this is a bit inefficient. In one version of the function, return the number of primes via a separate reference variable. In the other, use 0 as a sentinel value to signify the end of the prime list. See below for the...

  • C++ Write a function called reverseArray(char[] , int) that accepts in an array of chars and...

    C++ Write a function called reverseArray(char[] , int) that accepts in an array of chars and the size of the array.as parameters. The function should then create a new array that is the same size as the original array. The function should copy the elements from the first array into the second array in reverse order. So if the initial array is {'a' , 'b', 'c'} then the new array will be {'c', 'b', 'a'} Then in your main(), create...

  • TestScores . SIZE: int //size of the array scores: int [ 1 estScores findMax ( )...

    TestScores . SIZE: int //size of the array scores: int [ 1 estScores findMax ( ) : int findMin ) int findScore (value: int): bool res(): int Write the constructor method. It fills the array with random number from 0-100. Find below the code to get your started 1. public TestScores () Random rand -new Random); for (int i-0 i<SIZE i+) socresi] -rand.nextInt (101); Finding the maximum value in an array. Write the method findMax. It returns the maximum value...

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