473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Ues pointer to find the mode of a group of value

Here is the code I currently have. Now I need a function which is declared as "int mode(int *, int)" to find the mode of those value. The function requires:
1,create an array 'frequency' of 101 integers. 100 is arbitrarily chosen as the absolute maximum of movies a college student could see in a week. This array
will keep track of frequencies of weekly movie watching values.

2, Intialize array 'frequency' elements to zeros (use a loop)

3, Populate the 'frequency' array with the data (how many students see 1, 2, 3,etc. movies a week)

The following algorithm is a useful method to easily increment random values:

Run a loop on 'student_data' from 0 to 'num_students'.

The content of 'student_data' array becomes the index of the 'frequency' array and the content of that 'frequency' array
element gets incremented. Very important to understand this process.

The problem is I didn't understand the crucial part of this function which is : The content of 'student_data' array becomes the index of the 'frequency' array and the content of that 'frequency' array element gets incremented. Very important to understand this process.

Hope someone would help me on this. Thanks a lot.

#include <iostream>
#include <iomanip>
using namespace std;



int *create_array(int);
void enter_data(int *, int);
int mode(int *, int);
int avrg(int *, int);
void showArray(int *, int);

//Main functions:
int main()
{
int *dyn_array;
int mode, students;
float avrg;


do
{
cout << "How many students you will enter? ";
cin >> students;
}while (students <= 0);

dyn_array = create_array(students);
enter_data(dyn_array, students);

cout << "The array is: \n";
showArray(dyn_array, students);
cout << "The mode is: " << mode;
delete [] dyn_array;


return 0;

}

//Separated Functions:

//Create array.
int *create_array(int num_students)

{
int *student_data;

student_data = new int(num_students);

return student_data;


}

//Get date.
void enter_data(int *student_data, int num_students)
{

for (int count = 0; count < num_students; count ++)
{
cout << "How many did student " << (count + 1) << " watch? ";
cin >> student_data[count];

while ( student_data[count] < 0 || student_data[count] > 100)
{
cout << "Invalid Entry. Please enter a number between 0 and 100: ";
cin >> student_data[count];

}

}






}

//Display array
void showArray(int *student_data, int num_students)

{

for (int count = 0; count < num_students; count ++)
{
cout << "Student " << (count + 1) << " watched " << student_data[count] << " movies" << endl;
}

}
Nov 14 '11 #1
1 1776
weaknessforcats
9,208 Expert Mod 8TB
What is wanted is for you to create two arrays. One array is for data and the other is the frequency of ths data.

So if yur data array is cars:

[0] Ford
[1] Chevrolet
[2] Mercedes

then yur frequency array is:
[0] 36 ...the number of Ford
[1] 9 ...the number of Chevrolet
[2] 4 ...the number of Mercedes

This mapping technique is very common and is used to solve a variety of problems where counts of various things are needed
Nov 14 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Robin M via .NET 247 | last post by:
I want to check if the user who is running the program has administrative right or not. I am using following code to check all the property names but I couldn't find "memberof" property ( as...
8
by: Huibuh | last post by:
I start hating RETURN!!! list<happy> //#include <list>// is searched via iterator (ptr) so long until the argument "number" of the class "happy" is equal now. The function "findhappy" works...
5
by: cai_rongxi | last post by:
Anyone has the solution to the question "Given an array a with value 1...N, find a repeted value" in C code? Thanks a lot
2
by: ken | last post by:
how can I find out the value of an environment variable in my pythong script? Thank you.
10
by: alij | last post by:
Hi, I have just started learning C and am working on pointers but am having difficulty with the following: If i have a struct, that includes a pointer to char (in my case, i've named it...
1
by: Max | last post by:
Hello group, I use this function to find the value of a radio group: function valButton(btn) { var cnt = -1; for (var i=btn.length-1; i -1; i--) { if (btn.checked) {cnt = i; i = -1;} } if...
30
by: =?Utf-8?B?VGhvbWFzVDIy?= | last post by:
Hello, I have an array that holds 4 values ( they contain random numbers). I would like to find the lowest value (if there is a tie i would like to find one of the tie.) then remove that value....
2
choke
by: choke | last post by:
I need to write a function in devc++ that creates an array of n integers, each element is equal to n*n+i*i-n*i where i is from 0 to n-1 as the array index. Within the same function I need to find...
3
by: David C | last post by:
My brain is fried. How can I determine if the row is in edit mode when processing the RowDataBound event? Thanks. David
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.