Question

C programming for the below question.. In this program we are going to test the performance...

C programming for the below question..

In this program we are going to test the performance of two searching codes.
(a) Write a randomarray(n, max) function that returns a pointer to an array of size n integers,
filled with random values between 0 and max.
(b) Write a median(n, arr) function that returns the median of an integer array, without ordering
them. The median value means that half the numbers are smaller or equal and half are
bigger or equal. If the length n is even, choose the n/2 highest value. In this implementation
of the median, the return value must be a number within the sequence.
i. Starting from the first element determine if it is the median.
ii. If not advance to the next element and check, repeat the process until the median is
found.
iii. It is possible to skip elements to make it more efficient.

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

Thanks for the question.


Below is the code you will be needing Let me know if you have any doubts or if you need anything to change.


Thank You !!


===========================================================================

#include<stdio.h>
#include<stdlib.h>

int* randomarray(int n, int max);
int median(int n, int arr[]);

int* randomarray(int n, int max){
  
   int* pointer = (int*)malloc(n*sizeof(int));
  
   int i;
  
   for(i=0; i<n; i++){
      
       *(pointer+i) = rand()%max;
   }
  
   return pointer;
}

int median(int n, int arr[]){
  
   int i,j;
  
   // logic to find median when n is odd
   for(i=0; i<n; i++){
       int bigger_count=0;
       int smaller_count=0;
      
       for(j=0; j<n; j++){
           if(i==j)continue;
           if(arr[j]<=arr[i])bigger_count+=1;
           else if (arr[j]>=arr[i])smaller_count+=1;
       }
      
       if(bigger_count==smaller_count && n%2==1)return arr[i];
   }  
   // logic to find median when n is even
   int two_median[2]={-1,-1};
   int index=0;
  
   for(i=0; i<n; i++){
       int bigger_count=0;
       int smaller_count=0;
      
       for(j=0; j<n; j++){
           if(i==j)continue;
           if(arr[j]<=arr[i])bigger_count+=1;
           else if (arr[j]>=arr[i])smaller_count+=1;
       }
// when count of numbers bigger than the current number is same as the count of numbers smaller than the current

// number
       if(bigger_count-smaller_count==1 || bigger_count-smaller_count==-1){
           two_median[index]=arr[i];
           index=index+1;
       }
       if(index==2)break;
   }
   if(two_median[0]<two_median[1])   return two_median[1];
   else return two_median[0];
}


int main(){
   int n = 10;
   int max=100;
   int *p = randomarray(n,max);
   int median_ = median(n,p);
   printf("Median = %d\n\n",median_);
  
   int i=0;
   for(i=0; i<n; i++){
       printf("%d ",*(p+i));
   }
  
   free(p);
  
}

=====================================================================

rain.cpp median.cpp #include<stdio.h 1 #include<stdlib. h> int* randomarray (int n, int max) int median(int n, int arr [] );

Add a comment
Know the answer?
Add Answer to:
C programming for the below question.. In this program we are going to test the performance...
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
  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • i have a question need it to get solved in 20 minutes please in C++ programming...

    i have a question need it to get solved in 20 minutes please in C++ programming . this is the question: Write a function GetPtrMaxElem that takes as input the base address of an array of ints and the number of elements. The function should inspect the contents of the array and return a pointer to the last array element with the maximum value. (Hint: what should the return type be?)

  • C Programming For this task, you will have to write a program that will prompt the...

    C Programming For this task, you will have to write a program that will prompt the user for a number N. Then, you will have to prompt the user for N numbers, and print then print the sum of the numbers. For this task, you have to create and use a function with the following signature: int sum(int* arr, int n); Your code should look something like this (you can start with this as a template: int sum(int* arr, int...

  • MIPS Programming 1 In this project, you are going to write a MIPS program to read...

    MIPS Programming 1 In this project, you are going to write a MIPS program to read an integer number and convert it to 8- bit signed binary in a string. Your program should do the followings: Part II (50%, due date: March 1) for negative number . Write a non-leaf function to convert a negative integer number to 2's complement in string . Two parameters: integer number, string array or pointer -Get absolute value of the negative number o Call...

  • C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...

    C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...

  • "Controlling complexity is the essence of computer programming." Write a program that uses the C++ string...

    "Controlling complexity is the essence of computer programming." Write a program that uses the C++ string functionality to count the number of vowels in this phrase. Q7. Write a function bool xor(bool A, bool B) which returns the Exclusive OR of boolean input variables A and B. Test your function by printing out the full truth table of the function. Q8. Write a method swap(char_all, int i, int i) which swaps element i and element j of a given character...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • In C language Write a program that includes a function search() that finds the index of...

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

  • PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed...

    PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed with a list of nodes, and the first node pointer points to the front I a) (10 pts) Complete the following class declaration which is similar to Linklist class ecter class (Write only the prototypes and the private data field; definitions will follow the class declaration) template stypenane T> olass Linkedveoter t private olass Node publie T info Node "next typedet Node *nodePtr publie...

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