1 Replies - 569 Views - Last Post: 02 December 2012 - 12:36 PM Rate Topic: -----

#1 tyronv   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-November 12

Dynamically allocate an array and find the mode.

Posted 02 December 2012 - 12:34 PM

This is the Question?
In statistics, the mode of a set of values is the value that occurs most often or with the greatest frequency. Write a function that accepts two arguments:
An array of integers
An integer that contains the size of the array
This function will determine the mode of the array. That is, it should determine which value in the array occurs most often. The mode is the value of the function should return. If the array has no mode (none of the values occur more than once), the function should return -1. (Assume the array will always contain nonnegative integers).
Demonstrate your function using a driver program that will accept from the user the number an integer containing the number of items, dynamicaly allocate an array of integers the size the user specified, prompt the user for each item in the array, calculate the mode() and then display the mode to the user.

I am able to arraing the array but don't know how to get started with finding the mode.

This is my code so far.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int * getIntegers(int *);
void displayIntegers(int *,int);
void sortIntegers(int *integers, int size);
int modeIntegers(int *integers, int size);
int main()
int i;
int *testIntegers = NULL;
int numberIntegers = 0;
testIntegers = getIntegers(&numberIntegers);
sortIntegers(testIntegers, numberIntegers);
free (testIntegers);
return 0;
int *getIntegers(int *sizep)
int i;
int *myArray;
int *iptr;
printf("How many Integers do you want? ");
scanf(" %d",sizep);
myArray = calloc(*sizep , sizeof(int));
/* using array notation*/
for (i = 0; i < *sizep; i++)
printf("\tIntegers %d: ",i+1);
scanf(" %d",&myArray[i]);
return myArray;
void displayIntegers(int *integers, int size)
/* array */
int i;
for (i = 0; i < size; i++)
void sortIntegers(int *integers, int size)
/* Selection sort as an array */
int inner, outer, hold;
for (outer = 0; outer < size -1; outer++)
for (inner = outer + 1; inner < size; inner++)
if (integers[outer] > integers[inner])
{ // swap them
hold = integers[outer];
integers[outer] = integers[inner];
integers[inner] = hold;

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically allocate an array and find the mode.

#2 modi123_1   User is online

  • Suitor #2
  • member icon

Reputation: 14088
  • View blog
  • Posts: 56,440
  • Joined: 12-June 08

Re: Dynamically allocate an array and find the mode.

Posted 02 December 2012 - 12:36 PM

Closing duplicate topic.. please avoid creating multiple topics on the same issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1