Thread: Pleas Help Me!!!!

  1. #1
    Unregistered
    Guest

    Question 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 read(int);
    int occurs(int);
    int after_pass(int);
    int i,j,p,q,w,x,y,z,a[SIZE];

    int main()
    {
    read(z);
    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[0], a[1],
    a[2], a[3], a[4], a[5], a[6], a[7]);

    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;
    }
    int read(int z)
    {
    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]);
    }
    }

    Thank you in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    More homework. Joy.

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

    ...fill array...
    /** your code here **/
    ...count instances...
    for(x=0;x<size;x++)count[array[x]%possiblechoices]++;

    It doesn't get much easier than that.

    Quzah.

  3. #3
    Unregistered
    Guest
    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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > read(z);
    > 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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or Java ? Pleas help meee ...
    By bchaib in forum C Programming
    Replies: 60
    Last Post: 05-19-2008, 06:54 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM