Thread: arrays, sums and averages

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15

    Lightbulb arrays, sums and averages

    Hey, Can i get help too? I have a problem same with this guy, now i got far in coding and learned alot while doing so but i have a slight problem, in those 3 displays (ODD,EVEN,PRIME)
    if i input (1,2,3,4,5)
    it should display
    EVEN:2,4
    ODD;1,3,5
    PRIME;1,2,3,5

    but instead it displays something like this
    even;0,2,0,4,0
    odd;1,0,3,0,5
    prime:1,2,3,0,5

    help please!

    Code:
    #include<stdio.h>
    #include<conio.h>
    int sum (int);
    float average (float,float);
    void even (int,int);
    void odd (int,int);
    void prime (int,int);
    void factorial (int,int);
    int ehold[20],ohold[20],phold[20],fhold[20],facthold[20],ucnt=0;
    
    main()
    {
        int N,Ncnt,tsum=0,num;
        clrscr();
        printf("Enter N numbers: ");
        scanf("%d",&N);
        for (Ncnt=1;Ncnt<=N;Ncnt++)
        {
            printf("Value of Number %d - ",Ncnt);
            scanf("%d",&num);
            tsum=sum(num)+tsum;
            even(num,Ncnt);
            odd(num,Ncnt);
            if ((num%Ncnt)==0)
            {
                prime(num,Ncnt);
            }
            factorial(num,Ncnt);
        }
        printf("\nSum: %d",tsum);
        printf("\nAverage: %.1f",average(tsum,N));
        printf("\n\nEVEN numbers: ");
        for (ucnt=1;ucnt<Ncnt-1;ucnt++)
        {
            printf("%d,",ehold[ucnt]);
        }
        printf("\b ");
        printf("\n\nODD numbers: ");
        for (ucnt=1;ucnt<Ncnt-2;ucnt++)
        {
            printf("%d,",ohold[ucnt]);
        }
        printf("\b ");
        printf("\n\nPRIME numbers: ");
        for (ucnt=1;ucnt<=Ncnt-1;ucnt++)
        {
            printf("%d,",phold[ucnt]);
        }
        printf("\b ");
        printf("\n\nFactorial:");
        for (ucnt=1;ucnt<=Ncnt-1;ucnt++)
        {
            printf("\n\tValue %d - %d = %d",ucnt,fhold[ucnt],facthold[ucnt]);
        }
        getch();
        return 0;
    }
    
    int sum (int a)
    {
        int sum=0;
        return sum=a+sum;
    }
    
    float average (float s,float c)
    {
        float ave;
        ave=s/c;
        return (ave);
    }
    
    void even (int e,int o)
    {
        if ((e%2)==0)
        ehold[o]=e;
    }
    
    void odd (int o,int d)
    {
        if ((o%2)>0)
        ohold[d]=o;
    }
    
    void prime (int p,int r)
    {
        int test,cnt,prc=0;
        for (cnt=1;cnt<=p;cnt++)
        {
            if ((p%cnt)==0)
                prc++;
        }
        if (prc<=2)
        {
            phold[r]=p;
        }
    }
    
    void factorial (int f,int a)
    {
        int fact=1,cnt;
        for (cnt=1;cnt<=f;cnt++)
        {
            fact=fact*cnt;
        }
        fhold[a]=f;
        facthold[a]=fact;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you start your own thread instead of jumping on the end of a similar thread with "me too".

    even;0,2,0,4,0
    odd;1,0,3,0,5
    Notice that wherever there is a zero, there is a valid number in the corresponding position in the other array.

    A number is either even or odd - it cannot be both.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15
    Thanks for moving it sir.

    so what do i do with it,
    I'll merge the 2 functions in 1?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Either
    - each array has a separate count of the number of valid entries (ie, not the same global Ncnt
    - inside each printing loop, you skip any entry which is zero.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15
    how do i skip the zero entry?
    i tried
    if (hold[ucnt]==0)
    ucnt++;

    what do i put under the if?? i thinks i have it wrong,,

    and how about the prime?? i thinks its easier if i just used a skip?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    As the first thing in each loop, do something like

    if ( ehold[ucnt] == 0 ) continue;
    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.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15
    It worked like a charm! thanks sir! you're really good. I'll be back soon thanks again sir, you just saved me hours and a whole semester.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and sums of contents.
    By Xpl0ReRChR in forum C Programming
    Replies: 3
    Last Post: 11-24-2011, 02:22 PM
  2. Help with Arrays and Moving Averages
    By mju4t in forum C Programming
    Replies: 3
    Last Post: 04-25-2007, 08:44 PM
  3. Arrays & Sums
    By mrlooneytoon in forum C Programming
    Replies: 9
    Last Post: 04-05-2007, 06:35 PM
  4. moving averages
    By planet_abhi in forum Tech Board
    Replies: 0
    Last Post: 04-24-2003, 08:22 AM
  5. Structures, Arrays, Sums...
    By Inept Pig in forum C Programming
    Replies: 19
    Last Post: 04-19-2002, 04:07 PM