hi
i tried to do this q but i stuck in the middle of writing codes
Write a program that creates an array dynamically whose pointer must not change, initialize the array to random values between 0 and n (inclusive) using rand () where n is a value entered by the user, then pass the array to a function that takes an array of integers, its size and n and returns the value that was repeated more than other values. The function must use an array of n elements that is used to store the number of occurrences of a specific value, for example location 0 tells you how many times 0 appeared in the passed array, location 1 tells you how many times 1 appeared in the passed array and so forth.

#include <iostream>
#include<ctime>
using namespace std;
int fun( int a[], int size, int n) 
{
    int *p= new int[n+1];
    if (p==NULL)
        {
            cout<<"errer!"<<endl;
            exit(1);
        } 


    for (int i=0;i<size;i++)
    {
        p[i]=0;
    }
    for (int j=0;j<size;j++)
    {
        p[a[i]]++;
    }
}


void main()
{
    cout<<"please enter a number\n";
    int n;
    cin>>n;
 int *const p= new int [5];
 srand(time(0));
for(int x=0;x<5;x++)
{
    p[x]=rand()%(n+1);
}

}

Edited 3 Years Ago by Nick Evan: Fixed formatting

//Write a program that creates an array dynamically whose pointer must not change
const int* array = new int[size];

//initialize the array to random values between 0 and n (inclusive) using rand () 
for(int i=0; i<size; i++)
     array[i] = rand()%(n+1);

//then pass the array to a function that takes an array of integers, its size and n and returns the value that was repeated more than other values
int high_frequency(int array[], int& size, int& n)
{
     int highest = 0;
     int temp = 0;

     for(int i=0; i<size; i++)
     {
          temp = count(&array[0], &array[size], array[i]);
     
          if(temp > highest)
 
               highest = temp;
     }

it gave a compilation error: error C3861: 'count': identifier not found

I appologize for my indescretions, but i got distracted with other things, so here is a function for you, free of charge. No effort at learning required on your part:

/* pass the array to a function that takes an array of integers, its size and n and returns the value that was repeated more than other values. The function must use an array of n elements that is used to store the number of occurrences of a specific value, for example location 0 tells you how many times 0 appeared in the passed array, location 1 tells you how many times 1 appeared in the passed array and so forth. */

#include<algorithm>

int high_frequency(int array[], int& size, int& n)
{
     int highest = 0;
     int temp = 0;
     int index = 0;
     int* counter = new int[n];

     fill(&counter[0], &counter[n], 0);

     for(int i=0; i<size; i++)
     {
          temp = count(&array[0], &array[size], array[i]); 

          if(temp > highest)
          {
               highest = temp;    
               index = i;           
          }

          counter[array[i]] = temp;
     }

     return array[index];
}

Edited 7 Years Ago by Clinton Portis: no more pm's unless they are thanking me for your free code.

This article has been dead for over six months. Start a new discussion instead.