Question

Trying to do this in C++

Question 1: 1. Implement the function: int minInArray (int arr[], int arrSize) This function is given arr, an array of integers, and its logical size, arrSize. When called, t returns the minimum value in arr. Write a program that reads from the user a sequence of 20 integers (unnecessarily different from one another) into an array, and outputs the minimum value, and all the indices it appears in the array. 2. Your program should interact with the user exactly as it shows in the following example: Please enter 20 integers separated by a space 14 5 12 5 6 14 5 12 14 12 14 687 5 136 9 2189 10 6 The minimum value is 5, and it is located in the following indices: 1 3 6 14

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

1.CODE IN C:

#include<iostream>
#include<string>
using namespace std;
int minInArray(int arr[],int n);
int main(){
   //declaring variables
   int n,min;
   //taking the size of the array as input from the user
   cout << "Enter the size of the array:";
   cin>>n;
   int arr[n],i;
   //taking elements of the array as input from the user
   cout <<"Enter the elements of the array:";
   for(i=0;i<n;i++){
       cin>>arr[i];
   }
   cout <<"Your array is: ";
   for(i=0;i<n;i++)
       cout<<arr[i]<<" ";
   //calling the minInArray() function
   min=minInArray(arr,n);
   //displaying the result
   cout <<"\nThe minimum element in your array is:"<<min;
}
//this is minInArray function find the minimum element in the array
int minInArray(int arr[],int n){
   int i,min=99999999;
   for(i=0;i<n;i++){
       if(arr[i]<min)
           min=arr[i];
   }
   return min;
}

OUTPUT:

2.CODE IN C:

#include<iostream>
#include<string>
using namespace std;
int minInArray(int arr[],int n);
int main(){
   //declaring variables
   int n=20,min;
   int arr[n],i;
   //taking elements of the array as input from the user
   cout <<"Please enter 20 integers seperated by a space:";
   for(i=0;i<n;i++){
       cin>>arr[i];
   }
   cout <<"Your array is: ";
   for(i=0;i<n;i++)
       cout<<arr[i]<<" ";
   //calling the minInArray() function
   min=minInArray(arr,n);
   //displaying the result
   cout <<"\nThe minimum value is "<<min<<", and it is located in the following indices:";
   for(i=0;i<n;i++){
       if(arr[i]==min)
           cout<<" "<<i;
   }
}
//this is minInArray function find the minimum element in the array
int minInArray(int arr[],int n){
   int i,min=99999999;
   for(i=0;i<n;i++){
       if(arr[i]<min)
           min=arr[i];
   }
   return min;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Trying to do this in C++ Question 1: 1. Implement the function: int minInArray (int arr[],...
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
  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

  • 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...

  • Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int...

    Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax (int [ ] arr) determines and returns the maximum value within an array int arrayMin (int [ ] arr) determines and returns the minimum value within an array void arraySquared (int [] arr) changes every value within the array to value2 void arrayReverse (int [ ] arr) reverses the array (for example: 7 8 12 becomes 2 18 7) Test your methods by...

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting...

    Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting at index 0 void exchange(int x, int y) { int temp:= x; x:= y; y:= temp; } main(){ for (j = 0; j < 5; j++) arr[j]:= j; i:= 1; exchange(i, arr[i+1]); output(i, arr[2]); //print i and arr[2] } What is the output of the code if both parameters in function swapping are passed by: a- value? b- reference? c- value-result?

  • using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an...

    using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an array manipulation program that allows the user is to pre much whatever meant to ananas Wananching the program the user will passat are the contains a set of value and that w 2 Check to see the could be opened at the program was not opened the continue 3. Create an array and with the values from Presente en de h and powermany reded...

  • a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string>...

    a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string> such that the values in the map are the string values in the array of strings, and the keys in the map are the corresponding array indices of the string values. You may assume all necessary libraries have been included in your program and your solution must be syntactically correct in order to receive full credit. map<int, string> arrayToMap(string arr[], int arrSize) { b....

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

  • How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace...

    How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace std; void read(int arr[]) {    string line;    ifstream myfile("input.txt");    int i = 0;    if (myfile.is_open())    {        while (getline(myfile, line))        {            arr[i];            i++;        }        myfile.close();    } } void output(int arr[]) {    ofstream myfile("output.txt");    if (myfile.is_open())    {        int i = 0;...

  • Please do in Java as simply as possible :) Exercise #3: Design and implement a program...

    Please do in Java as simply as possible :) Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7   8   9 becomes 9   8   7 ) The program main...

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
ADVERTISEMENT