finding repeated element value
Zen:
I don't get the loop for arr2;
arr1 would be 100 elements of random values
arr2 would be 30 elements that would increment as value of each element of arr1[n-99] equals 1-30; so say arr1[0] = 30; then in arr2 I suspect to increment arr2[n-29] //30 elements means the subscript of elements is 0-29; and arr2[29] is position for number 30 to be incremented.
-----------------------------------------
you say to do the following:
then you would loop through the first array incrementing the second array when a value is found -
for(int i=0;i<100;i++)
{
arr2[arr1[i]-1]++; // how does affect the 30 elements of arr2 ???
}
-------------------------------------------------------------------
Then the second array contains the first array sorted and counted. If you want to print the first array in it's sorted state you can use a nested for loop to print out the second array -
// we are only printing out the mode of the array arr1[100]
which is the number that comes up most often
// in this case arr2 would have 30 elements but the values in arr2 elements are incrementing as the number of times the numbers 1-30 come up in the random listing of elements in arr1[100]; how do you increment the element in arr2 and have those correspond to the value of element of arr1 ??? then how do you choose the elements with the highest value - could be just one element with high incremented value, could be more than one. How does program work to pull each element to
for(int j=0;j<30;j++)
{
for (int k=0; k<arr2[j] ;k++)
cout << j+1 << endl; //how is this sorting and
} // counting
// and the last line doesn't
// look like C function to me
only 2 arrays are asked for
Zen,
I will have to study your suggestion a bit.
But we were expressedly told to use only 2 arrays; one with the 100 elements randomly selected and one to sort and count the values...incrementing each element as often as it occurs and then sorting to get the number occurring most often.
Some instructors kinda give hints. This one does not.
Thanks anyways.
HERE"S CODE ; still have problem
I got the array created (100 random elements)
I got the frequency of values calculated (numbers 1-30)
However, I am needing the number that coincides with the highest frequency of occurring values:
eg. array1 elements = {1, 4, 5, 5, 6, 4, 3, 10, 5, 4}
1 occurs 1 time
4 occurs 3 times
5 occurs 3 times
3 occurs 1 time
10 occurs 1 time
6 occurs 1 time
SO: 4 and 5 each occur the most often at 3 times each;
these would be printed as the output titled MODE of array
Can I do this part somehow without a 2-dim array????
I don't get the answers I have been given thus far.
Sorry.
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/*****************************************/
/* definitions */
/*****************************************/
#define NUM_ELEM 100
#define RANGE 30
/***************************/
/* BEGIN OF MAIN */
/***************************/
int main()
{
int array1[NUM_ELEM]={0}; /* declare/initilize arrays */
int array2[RANGE]={0};
int i,j; /* declare int variables */
int temp, mode;
srand((unsigned)time(NULL)); /* seed random number */
/* generator */
printf("here is the array created\n");
/**********************/
for (i=0;i<NUM_ELEM;i++) /* create array of */
{ /* elements */
array1[i]=rand()%RANGE+1; /* and print array */
printf("%2d ",array1[i]); /**********************/
if ((i+1)%20==0)
printf("\n");
}
printf("\n\n");
/************************/
for(i=0;i<NUM_ELEM;i++) /* count frequency of */
{ /* number occurence */
array2[array1[i]-1]++; /************************/
}
for( i = 0; i < RANGE-1; i++ ) /***************/
{ for( j = RANGE-1; j > i; j-- ) /* sort array */
if( array2[j] > array2[j-1]) /*of occurences*/
{ /***************/
temp = array2[j-1];
array2[j-1] = array2[j];
array2[j] = temp;
}
}
return 0;
}