|
-
April 22nd, 2003, 12:07 PM
#1
I need some help w/a programming project...
Okay...don't laugh...
I am a freshman C.S. major at my college and I am starting out learning c++ on a linux console machine. We are currently writing a program that reads in a list of numbers from a file and populates an array with those values. Then, it sorts the values and computes the mean, median, mode, and standard deviation. I am having one slight problem. The function I wrote for finding the mode only returns the highest value(ie, if there are two or three modes it only returns one of them - the highest value). I have included my humble little piece of code and if it isn't too much trouble with for you gurus I would greatly appreciate some assistance...here are the questions I need answered:
1. Is there a way to simply modify my existing code(i.e. changing the type to void and adding another loop over the top)
2. Do I need to use another array to determine the frequency of each value and if so, how do I implement it?
3. If I do need to use an array for frequency, how could I compare the two arrays to find all of the correct frequency indices?
If I am posting in the wrong forum, sorry...if you know where I could get help on this I would love to know...anyway...here is my function for the mode:
(I realize I will probably have to change its type to void since I need to find multiple modes...as it is, it returns the highest value mode regardless of how many there are)
Code:
int mode(int array[], int SIZE_OF_ARRAY)
{
using namespace std;
int counter = 0;
int max = 0;
int i;
int mode_index = 0;
for (int i = 0; i < SIZE_OF_ARRAY; i++)
{
if (array[i] == array[i + 1])
{
counter++;
}
else
{
if (counter > max)
{
max = counter;
}
counter = 0;
}
}
counter = 0;
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
if (array[i] == array[i + 1])
{
counter++;
}
else
{
if (counter == max)
{
mode_index = i;
}
counter = 0;
}
}
return array[mode_index];
}
Last edited by xsyntricity; April 22nd, 2003 at 01:06 PM.
-
April 22nd, 2003, 12:37 PM
#2
You have so many options here. But we need some info first. First, do you know any of the STL? Did you learn vector<> or list<> or any of that good stuff? Second, why are you returning a double when your array is an array of int? Third, when pasting code, try using the code tags given in the forum. There's a button that says "PHP" at the vB Code section of the reply. It helps if you put your code in there.
Welcome to the code guru forums! Though I know this specific topic isn't already in this forum (it's too specific), please be aware of the fact that there's a FAQ and that there is a search forum feature -- you can often find what you need there
-
April 22nd, 2003, 01:04 PM
#3
lol...don't know why my function is type double...i'll change that...doesn't hurt anything I guess but thanks for pointing that out...
I haven't learned much as this is an intro to C.S. class and mostly focuses on C++ programming basics. vector<> and list<> are unfamiliar to me. Also, I will edit my post above using the code tags. Anyway...I think my teacher wouldn't be too impressed if I used stuff that we haven't learned. I was hoping for a solution involving one of these options:
1. Maybe a void function that fills an array of the same size as the sorted array of values that has frequency numbers like...
value array frequency array
21___________3
21___________3
21___________3
35___________1
44___________2
44___________2
50___________3
50___________3
50___________3
...and then another void function that outputs the value array indices that have the max frequency value(in the above example it would output array[0] and array [6] or 21 and 50)
2. Adding another loop to the existing code that does iterations excluding the already found max until all of the same maxes are found(i.e. if there are two modes of frequency 4, it couts the first mode index then iterates without including those particular indices and eliminates them one at a time until the loop terminates)
Last edited by xsyntricity; April 22nd, 2003 at 01:08 PM.
-
April 22nd, 2003, 01:06 PM
#4
i'm definitively not a big fan of C++, but i can tell you that you're not programming in C++, that looks like C to me.
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
-
April 22nd, 2003, 01:10 PM
#5
My textbook says I am programming in C++...we are not learning anything fancy...like i said...this is an intro to C.S. class...most people in my class don't even type 20 wpm...just in case you were curious my textbook is Problem Solving with C++ The Object of Programming, 4th Edition by Walter Savitch.
Originally posted by Luis G
that looks like C to me.
notice that my counter is incremented with ++?
Last edited by xsyntricity; April 22nd, 2003 at 01:12 PM.
-
April 22nd, 2003, 01:10 PM
#6
You are using C, not C++... but...
I have modified your function:
void mode(int array[], int SIZE_OF_ARRAY, int* numMode)
{
//using namespace std;
int count = 0;
int max = 0;
int* data = (int*)malloc(SIZE_OF_ARRAY*sizeof(int));
int count2 = 0;
for(int i = 0; i < SIZE_OF_ARRAY; i++)
{
count = 0;
for(int j = i; j < SIZE_OF_ARRAY; j++)
{
if(array[i] == array[j])
{
count++;
}
}
if(count > max)
{
max = count;
count2 = 0;
*(data + count2) = array[i];
}
else if(count == max)
{
count2++;
*(data + count2) = array[i];
}
}
count2++;
for(i = 0; i < count2; i++)
{
array[i] = *(data + i);
}
*numMode = count2;
free(data);
}
When finishing, numMode returns the number of elements of the mode, and are placed in the first positions of the array.
i.e. int array[] = 9, 3, 7, 10, 12, 9, 3, 10;
int numElem;
mode(array, 8, &numElem);
numElem will be 3, and array[0-2] will be 9, 3, and 10
Caronte
Si tiene solución... ¿por qué te preocupas?
Si no tiene solución... ¿por qué te preocupas?
-
April 22nd, 2003, 01:15 PM
#7
I would do something along these lines then if you have to stay with basic types. I'm doing this on the fly so I might make a stupid syntax error or have a brain fart, but I hope you get the idea 
Code:
int mode(int array[], int SIZE_OF_ARRAY, int *pModeArray)
{
int ModeArrayCounter = 0;
... blah blah your code here
if (counter > max)
{
max = counter;
ModeArrayCounter = 1;
}
else if(counter == max)
{
ModeArrayCounter++;
}
... more code here
}
if(ModeArrayCounter > 0)
{
int Current = 0;
// create a new array using your pointer
// ModeArrayCounter in size, so it can hold
// all of the items that match that mode
pModeArray = new int[ModeArrayCounter];
... your for loop code here
if(counter == max)
{
*pModeArray[Current] = array[i];
Current++;
}
... more code
}
return ModeArrayCounter; // return the size of the new array pModeArray so you can loop through it later.
}
ok I hope I made some sense here, I typed in intervals cuz my boss kept walking by
-
April 22nd, 2003, 01:21 PM
#8
Originally posted by xsyntricity
My textbook says I am programming in C++...we are not learning anything fancy...like i said...this is an intro to C.S. class...most people in my class don't even type 20 wpm...just in case you were curious my textbook is Problem Solving with C++ The Object of Programming, 4th Edition by Walter Savitch.
notice that my counter is incremented with ++?
I should have been more clear, 'cause "that looks like C to me" is a little vague, I should have said, that is C code.
You'd be surprised by the ammount of books and teachers that confuse C with C++.
btw, increments a la ++ are part of the C language, and they are not distinctive of each language. C++ was named like that because it is "C increased", or at least that's what i read.
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
-
April 22nd, 2003, 01:22 PM
#9
Originally posted by Caronte
int* numMode)
int* data = (int*)malloc(SIZE_OF_ARRAY*sizeof(int));
*(data + count2) = array[i];
*(data + count2) = array[i];
*numMode = count2;
free(data);
I am not familiar with these operations...I am sorry if I am being a hassel but I should proably only use operations my class has learned thus far...is there a very beginner level, baisc way to do this? The way that was coded looked something similar to using call by ref params...is there a way to do it using by ref params instead of that '*' thing? Even if it involves a huge amount of code it would probably be better than doing something my teacher knows I am not familiar with...
-
April 22nd, 2003, 01:26 PM
#10
See if my code makes any more sense. My approach, using the new operator and such, is the c++ method, where malloc and free is the straight c method. If you say it's a c++ class then I'm sure new/delete is the way you want to go.
-
April 22nd, 2003, 01:26 PM
#11
Originally posted by xsyntricity
My textbook says I am programming in C++...we are not learning anything fancy...like i said...this is an intro to C.S. class...most people in my class don't even type 20 wpm
Believe it or not, you would have better off learning this the real C++ way, usage of vectors<> not arrays. Then you wouldn't have to lug that size argument to your mode function. I don't understand why these concepts are not introduced first. Beginning students pick up the vector<> syntax and have less trouble than when a beginning student gets arrays and pointers thrown at them. I just don't get it sometimes with the way this language is being taught to beginners
...just in case you were curious my textbook is Problem Solving with C++ The Object of Programming, 4th Edition by Walter Savitch.
Here is a review of your textbook:
http://www.accu.org/bookreviews/publ.../p/p001032.htm
You are already handicapped by a book that is not recommended by professional C++ programmers.
notice that my counter is incremented with ++?
What does that have to do with C or C++? Incrementing an integer using ++ exists in 'C' also.
Regards,
Paul McKenzie
-
April 22nd, 2003, 01:41 PM
#12
Originally posted by Luis G
You'd be surprised by the ammount of books and teachers that confuse C with C++.
The review of the book that I posted a link to states that the book is "C with classes". Oh-oh. I guess there is no mention of usage of the standard C++ library, or anything that C++ has had to offer for the past, say, 7 or so years.
The book used by xsyntricity is obsolete (published 1996) and doesn't reflect the way that proper, modern, C++ should be taught and programmed. I will boldly claim that if this book were up to snuff, the assignment would have been easier to do, and not as difficult as it has become. Too bad.
Regards,
Paul McKenzie
-
April 22nd, 2003, 01:41 PM
#13
Why does this keep happening? Why is c++ taught in c style to this day?? Why the misguidance for all these future job seekers?
xsyntricity, please, please tell your professor, in the nicest way possible so as not to trigger a territorial anger, that teaching c++ like this is not recommended by the c++ community, is not recommended by Bjarne Stroustrup himself, and really, truly causes problems for the students when they hit the workplace. I hope some of the posts in this thread have helped you solve your original problem, and I strongly urge you if you really have an interest in c++ to check out the ACCU site posted by Paul McKenzie and get a couple of the books that are recommended there.
None of this is to say that c is not a beautiful language in itself, and is used heavily in the marketplace to this day. But good c style is not good c++ style...
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
April 22nd, 2003, 01:45 PM
#14
Originally posted by Paul McKenzie
Believe it or not, you would have better off learning this the real C++ way, usage of vectors<> not arrays. Then you wouldn't have to lug that size argument to your mode function. I don't understand why these concepts are not introduced first. Beginning students pick up the vector<> syntax and have less trouble than when a beginning student gets arrays and pointers thrown at them. I just don't get it sometimes with the way this language is being taught to beginners.
You are already handicapped by a book that is not recommended by professional C++ programmers.
What does that have to do with C or C++? Incrementing an integer using ++ exists in 'C' also.
Regards,
Paul McKenzie
Well, first of all, I did not select our textbook and have thus been learning all of this the hard way I guess...I am not surprised to see a bad review of the text as it is more geared for a one-time experience with the 'C' programming language and doesn't seem suited for a C.S. major class - even an intro; however, I have no choice but to do this the way my instructor and textbook have taught it...so...I am stuck between a rock and a hard place...again...I am sorry for being such a hassel and now that you know what a hinderance my textbook has been I hope you understand my situation a little better...
Here is a quote from my text:
"Most of C is a subset of C++ , and so most C programs are also C++ programs."
This is why I was under the assumption I was programming in C++...according to my text, most C code is C++ code...although the opposite is not true...seeing as how the title to my text begins with C++, a beginner such as myself would naturally assume they were learning C++...sorry for the mistake...now that that is out of the way and we are both on the same page...is there any way to get this done without using vectors or '*'s? I have basic knowledge of functions, types, loops, nested loops, overloading function names, call by ref params, streams and basic file i/o, boolean expressions, arrays, and multi-dimensional arrays...I understand that there is a simple, easy way to do this...however, we have taken to learning the hard way first so I must do this the hard way...any other help you can give me would be much appreciated...
-
April 22nd, 2003, 01:51 PM
#15
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|