1. ## Pleas Help Me!!!!

Hey i really need help figuring out how to tally up the frequency of a value in an array, here are the instructions for the program:
http://www.cs.ucf.edu/~riqbal/assign5.html
I would really appreciate it if someone could tell me how to figure out how to count the number of times that the number appears:
#include <stdio.h>
#define SIZE 8

int sort(int);
int occurs(int);
int after_pass(int);
int i,j,p,q,w,x,y,z,a[SIZE];

int main()
{
sort(x);
occurs(w);

return 0;

}

int sort(int y)
{
for(i=0; i < SIZE; i++)
{
printf("After Pass %d: %d %d %d %d %d %d %d %d\n", i, a, a,
a, a, a, a, a, a);

for(j=i+1;j < SIZE; ++j)
{
if(a[i] > a[j])
swap(&a[i],&a[j]);
}
}
}

int swap(int *c, int *d)
{
int tmp;

tmp = *c;
*c = *d;
*d = tmp;
}
{
for(i=0; i < SIZE; i++)
{
printf("Enter value %d: \n", i);
scanf("%d", &a[i]);
}
}
int occurs (int w)
{
for(q=0; q < SIZE; q++)
{
for(p=q+1;p < SIZE; ++p)
{
if(a[q] < a[p])
swap(&a[q],&a[p]);
}
printf("%d occurs times.\n", a[q]);
}
} 2. More homework. Joy.

int array[size];
int count[possiblechoices]={0};
int x;

...fill array...
...count instances...
for(x=0;x<size;x++)count[array[x]%possiblechoices]++;

It doesn't get much easier than that.

Quzah. 3. Thanks for the help on the code but i am still confused as to why the count array doesn't show up the correct amount of numbers, here is the output i receive:
After Pass 0: 7 3 66 -5 -77 55 23 2
After Pass 1: -77 7 66 3 -5 55 23 2
After Pass 2: -77 -5 66 7 3 55 23 2
After Pass 3: -77 -5 2 66 7 55 23 3
After Pass 4: -77 -5 2 3 66 55 23 7
After Pass 5: -77 -5 2 3 7 66 55 23
After Pass 6: -77 -5 2 3 7 23 66 55
After Pass 7: -77 -5 2 3 7 23 55 66
66 occurs 1 times.
55 occurs 0 times.
23 occurs 1 times.
7 occurs 0 times.
3 occurs 0 times.
2 occurs 0 times.
-5 occurs 0 times.
-77 occurs 3 times.

and here is the new code that you helped me out with:

{
for(q=0; q < SIZE; q++)
{
for(p=q+1;p < SIZE; ++p)
{
if(a[q] < a[p])
swap(&a[q],&a[p]);
}
count[a[q]%8]++;
printf("%d occurs %d times.\n", a[q], count[q]);
}
}

I would really appreciate it if you could see the error and bring it to my attention, Thank you. 4. Um, what? What exactly are you trying to do? Once you've counted the number of occurances, why bother recounting? You're just reusing the same array anyway, no need to recount it. Additionally, if you're using negative numbers, you can't just use an array with the value as an index. You'll have to use some tricky (not really tricky) math to fudge the number so it fitx an index.

Please reexplain what you're trying to do.

Quzah. > sort(x);
> occurs(w);
What do those parameters do?
Nothing as far as I can make out.

So get rid of them, and all the other unused variables.

> int occurs (int w)
The array is already sorted, so there is no point calling swap any more is there.

> count[a[q]%8]++;
Do you know what the result of modulus is on a negative number? Popular pages Recent additions 