Create a function l_mode that finds the index of the value that occurs most often in an array. The input to your function is an array of integers A and the number of elements in the array 0 < n <= 10^3. Assuming v is the most frequent element in the array, your function will return the index i of the first (leftmost) occurrence of v in A. You can assume that -500 <= A[i] <= 500, for 0 <= i < n. Ties must be broken by choosing the smallest element.
this is the function name and parameters:
unsigned int l_mode(const int *A, unsigned int n);
It must be written in C++ and can only include
#include <iostream>
#include <cmath>
I only need the function but if you'd like to write a main to test the function out you can use some of the following test cases:
{1, 3, -500, -500, 0, 0, 0, -324, 500, 500, 500, 500} = 8
#include <iostream> #include <climits> using namespace std; unsigned int l_mode(const int *A, unsigned int n){ int value = INT_MAX , count = 0, c, index; for(int i = 0;i<n;i++){ c = 0; for(int j = 0;j<n;j++){ if(A[i] == A[j]){ c++; } } if(count <= c && A[i]<value){ value = A[i]; count = c; } } for(int i = 0;i<n;i++){ if(value == A[i]){ index = i; break; } } return index; } int main() { int A1[] = {1, 3, -500, -500, 0, 0, 0, -324, 500, 500, 500, 500}; int A2[] = {50, -500, -500, -500, 0, 1, 0, 3, -500, -500, 500, -500}; int A3[] = {5, 5, 5, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3} ; int A4[] = {1, 1, 1, 1 ,5, 5, 5, 5}; int A5[] = {5, 5, 5, 5, 1, 1, 1, 1} ; int A6[] = {-5, -1, -5, -1, -5, -1}; cout<<l_mode(A1,12)<<endl; cout<<l_mode(A2,12)<<endl; cout<<l_mode(A3,13)<<endl; cout<<l_mode(A4,8)<<endl; cout<<l_mode(A5,8)<<endl; cout<<l_mode(A6,6)<<endl; return 0; }
Create a function l_mode that finds the index of the value that occurs most often in...
In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
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 function in python, index(arr, value) to find indices of elements equal to some value in a Numpy array. The input arr is a 1-d numpy array, and input value is the value to search for. The function should return the index of the value. If the value occurs for multiple times, then all the indices should be returned as a 1-d numpy array. For example, if arr=[1 0 2 0 3 0 4 0 5 0 6 7...
PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...
#include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...
Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code for 8-bit signed array, 16-bit unsigned array, 16-bit signed array, 32-bit signed array and 32-bit signed array. 2) Fill in all the blanks in Table 1 using your completed code, following the hints provided within the table. 3) Fill in all the blanks in Table 2 using your completed code, following the hints provided within the table. C++ Program #include <stdio.h> #include <iostream> int...
Arrays in Functions 2. Arrays in Functions You can use both the array index variables and the entire array itself as arguments to functions. The following program updates the elements of the array grades, one-by-one. Thus, we have used a call-by-reference-ish mechanism to update the values (although no & was used). Note that there is a major difference between the grade in the function call and the one in the function definition. In the statement get_grade (grades[i]); "grades" is the...
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...