need some help...

B

bmlclemson08

I'm trying to figure out why this program is not printing out the
correct mode... suggestions?

int main(){
int qty, num, mode, a_1[count], a_2[MAX], sum = 0;
float mean;
int max_qty = 0;

printf ("Enter how many: ");
scanf ("%i", &qty);
if (qty > MAX)
printf ("Enter less than 50 numbers.\n");
else
for (num = 0; num < qty; num++){
printf ("Enter data %i:", num);
scanf ("%i", &a_2[num]);
sum += a_2[num];
}
for (num = 0; num < count; num++){
if (a_1[num] > max_qty){
max_qty = a_1[num];
mode = num;
}
}

mean = (float)sum / qty;
if (qty <= MAX)
printf ("The mean is %.2f.\n", mean);

printf ("The mode is %i.\n", mode);
return 0;
}


Thanks,
Brandon
 
F

Fred Kleinschmidt

bmlclemson08 said:
I'm trying to figure out why this program is not printing out the
correct mode... suggestions?

int main(){
int qty, num, mode, a_1[count], a_2[MAX], sum = 0;
float mean;
int max_qty = 0;

printf ("Enter how many: ");
scanf ("%i", &qty);
if (qty > MAX)
printf ("Enter less than 50 numbers.\n");
else
for (num = 0; num < qty; num++){
printf ("Enter data %i:", num);
scanf ("%i", &a_2[num]);
sum += a_2[num];
}
for (num = 0; num < count; num++){
if (a_1[num] > max_qty){
max_qty = a_1[num];
mode = num;
}
}

This is not a C question, it is an algorithm question.
You are not computing the mode here; you are computing the index of the
maximum value - and incorrectly at that (what if all values are negative)?
Mode is defined as the item that occurs most often.
 
D

Default User

bmlclemson08 said:
I'm trying to figure out why this program is not printing out the
correct mode... suggestions?

Yes, tell us what you expected to get, and what you actually got.



Brian
 
B

bmlclemson08

When doing a quantity of 5, I input 2, 2, 2, 3, and 3, expecting 2 and
getting 782.
 
B

Ben Bacarisse

I'm trying to figure out why this program is not printing out the correct
mode... suggestions?

int main(){
int qty, num, mode, a_1[count], a_2[MAX], sum = 0; float mean;
int max_qty = 0;

printf ("Enter how many: ");
scanf ("%i", &qty);
if (qty > MAX)
printf ("Enter less than 50 numbers.\n");
else
for (num = 0; num < qty; num++){
printf ("Enter data %i:", num);
scanf ("%i", &a_2[num]);
sum += a_2[num];
}
for (num = 0; num < count; num++){
if (a_1[num] > max_qty){
max_qty = a_1[num];

At this point a_1 has had nothing put into it so anything can happen here.
There are other things to say, but this is so huge a problem that you
can't go further until you fix it.
 
R

Robin Haigh

bmlclemson08 said:
I'm trying to figure out why this program is not printing out the
correct mode... suggestions?

int main(){
int qty, num, mode, a_1[count], a_2[MAX], sum = 0;
float mean;
int max_qty = 0;

printf ("Enter how many: ");
scanf ("%i", &qty);
if (qty > MAX)
printf ("Enter less than 50 numbers.\n");
else
for (num = 0; num < qty; num++){
printf ("Enter data %i:", num);
scanf ("%i", &a_2[num]);
sum += a_2[num];
}
for (num = 0; num < count; num++){
if (a_1[num] > max_qty){
max_qty = a_1[num];
mode = num;
}
}

mean = (float)sum / qty;
if (qty <= MAX)
printf ("The mean is %.2f.\n", mean);

printf ("The mode is %i.\n", mode);
return 0;
}


I don't think this is your actual code. As it stands, no values are put in
the a_1 array. I think you've missed out a line that looks like
a_1[a_2[num]]++, to accumulate the frequency count of each data item. But
before doing that, you have to initialise all elements of a_1 to 0, or your
counts are starting off with random values: automatic arrays aren't
initialised automatically.

Also, you need braces round the else block.

You also need to check that the input data items are within the range
0..count-1, otherwise you'll be writing to a_1 out of range.
 
O

osmium

bmlclemson08 said:
I'm trying to figure out why this program is not printing out the
correct mode... suggestions?

int main(){
int qty, num, mode, a_1[count], a_2[MAX], sum = 0;
float mean;
int max_qty = 0;

printf ("Enter how many: ");
scanf ("%i", &qty);
if (qty > MAX)
printf ("Enter less than 50 numbers.\n");
else
for (num = 0; num < qty; num++){
printf ("Enter data %i:", num);
scanf ("%i", &a_2[num]);
sum += a_2[num];
}
for (num = 0; num < count; num++){
if (a_1[num] > max_qty){
max_qty = a_1[num];
mode = num;

<snip>
Did you mean a_2[num]? a_1 has no data. How about using names like
"sample" and "frequency"? If you are going to use two arrays in your
solution, I think those would be pretty good names. But, as has been
pointed out by others, you are not computing frequency here. If the sample
data are 7,2,2,2 would not the single 7 trump all the 2s?

Another method would be to sort the data and then look for the longest
"run". You could write a bubble sort or use qsort() in the C libraries. It
might help a bit if you ignore the mean part of the assignment so you can
focus on one thing at a time. I suggest you start over.
 
B

Barry Schwarz

I'm trying to figure out why this program is not printing out the
correct mode... suggestions?

int main(){
int qty, num, mode, a_1[count], a_2[MAX], sum = 0;
float mean;
int max_qty = 0;

printf ("Enter how many: ");
scanf ("%i", &qty);
if (qty > MAX)
printf ("Enter less than 50 numbers.\n");
else
for (num = 0; num < qty; num++){
printf ("Enter data %i:", num);
scanf ("%i", &a_2[num]);
sum += a_2[num];
}
for (num = 0; num < count; num++){
if (a_1[num] > max_qty){

You have no data in a_1.
max_qty = a_1[num];
mode = num;
}
}

mean = (float)sum / qty;
if (qty <= MAX)
printf ("The mean is %.2f.\n", mean);

printf ("The mode is %i.\n", mode);
return 0;
}


Thanks,
Brandon


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top