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);
displayIntegers(testIntegers,numberIntegers);
//printf("The average is: %d\n",modeIntegers(testIntegers,numberIntegers));
free (testIntegers);
system("pause");
return 0;
}
int *getIntegers(int *sizep)
{
int i;
int *myArray;
int *iptr;
printf("How many Integers do you want? ");
fflush(stdout);
scanf(" %d",sizep);
myArray = calloc(*sizep , sizeof(int));
/* using array notation*/
for (i = 0; i < *sizep; i++)
{
printf("\tIntegers %d: ",i+1);
fflush(stdout);
scanf(" %d",&myArray[i]);
}
return myArray;
}
void displayIntegers(int *integers, int size)
{
/* array */
int i;
for (i = 0; i < size; i++)
printf("%d\n",integers[i]);
fflush(stdout);
}
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;
}
}
}
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);
displayIntegers(testIntegers,numberIntegers);
//printf("The average is: %d\n",modeIntegers(testIntegers,numberIntegers));
free (testIntegers);
system("pause");
return 0;
}
int *getIntegers(int *sizep)
{
int i;
int *myArray;
int *iptr;
printf("How many Integers do you want? ");
fflush(stdout);
scanf(" %d",sizep);
myArray = calloc(*sizep , sizeof(int));
/* using array notation*/
for (i = 0; i < *sizep; i++)
{
printf("\tIntegers %d: ",i+1);
fflush(stdout);
scanf(" %d",&myArray[i]);
}
return myArray;
}
void displayIntegers(int *integers, int size)
{
/* array */
int i;
for (i = 0; i < size; i++)
printf("%d\n",integers[i]);
fflush(stdout);
}
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;
}
}
}