DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   display array element with passing value...no undesstand~help (http://www.daniweb.com/forums/thread105722.html)

bobei89 Jan 22nd, 2008 5:43 am
display array element with passing value...no undesstand~help
 
okay, here is the code my teacher gave...but i do not understand some part...

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void printfAll(int size, int ary[])
{
        int a;
       
        for(a=0; a< size; a++)
        {
                if(!(a%10)) //why a%10 and what not equal to it//
                        printf("\n");
                printf("%5d", ary[a]);
        }
        printf("\n");
}


void printOddIdx(int size, int ary[])
{
        int a;
        for(a=1; a<size; a+=2)
        {
                if(!((a-1)%20))
                        printf("\n");
                printf("%5d", ary[a]);
        }
                printf("\n");
       
}

void printfOdd(int size, int ary[])  // print the odd values
{
        int a, num=0, newline =0;
       
        for(a=1; a< size; a++)
        {
                if(!(num%10)&& !newline)
                {
                        printf("\n");
                        newline=1;
                }
                if(ary[a]%2)
                {
                        if(newline)
                                newline=0;
                        printf("%5d", ary[a]);
                        num++;
                }

        }
}

long sum (int size, int ary[])
{
        int a;
        long sum=0;

        for(a=0; a<size; a++)
                sum += ary[a];       

        return sum;
}

int largest (int size, int ary[])
{
        int a, large_index=0;
       
        for(a=0; a<size; a++)
                if(ary[a] > ary[large_index])
                        large_index=a;
                return large_index;
}

void getNegatives(int ary[])
{
        int a, b, negAry[100];
       
        for(b=0, a=0; a<100; a++)
        {
                if(ary[a]<0)
                {
                        negAry[b]=ary[a];
                        b++;
                }
        }
       

        printf("\n\n---random number is divisible by 3 or 7, is stored as negative number---\n");       
        printfAll(b, negAry);
}

       


main()
{
        int aryA[100], i;

        srand(time(NULL));
        for (i=0; i<100; i++)
        {
                aryA[i] = rand()%999+1;

                if((aryA[i]%3 ==0)|| (aryA[i]%7 ==0))
                        aryA[i]=-aryA[i];
        }

               
        printf("\n\n---Prac 2a.Print the array 10 values to a line---\n");
        printfAll(100, aryA);

        printf("\n\n---Prac 2b.Odd numberred index location, ten to a line---\n");       
        printOddIdx(100, aryA);

        printf("\n\nPrac 2c.Sum of array %ld\n",  sum(100, aryA));

        printf("\n\nPrac 2d.Largest index of array is =  %d\n",  largest(100, aryA));
       
        getNegatives(aryA);
}

WolfPack Jan 22nd, 2008 6:37 am
Re: display array element with passing value...no undesstand~help
 
What part to you not understand?

bobei89 Jan 22nd, 2008 6:59 am
Re: display array element with passing value...no undesstand~help
 
statement 11th and 38th...i think that for some sort to arrange the value printed. But no understand how it work. Thx for reading my post

WolfPack Jan 22nd, 2008 8:04 am
Re: display array element with passing value...no undesstand~help
 
Those statements are used to print a newline after 10 numbers are printed in a row.
% is the modulus operator.
a%10
is 0 for numbers like 10, 20, and so on. So after the 10th number, the rest of the array is printed in a newline. The same happens after printing the 20th number and so on.

Comment out those lines and see how the output changes. The values printed will be the same, but the output will be in a single line.

bobei89 Jan 22nd, 2008 10:10 am
Re: display array element with passing value...no undesstand~help
 
if(!(a%10))
but what is not (a%10)?

And statement 24th which is:
if(!((a-1)%20))
Why it %20 instead of 10. When i put replace it to 10 it just show 5 value in row then newline.

Also the statement 38th which is:
if(!(num%10)&& !newline)
I removed the newline and it come out that output is became messy. I do not know why.

Thx for helping me.

Aia Jan 22nd, 2008 8:28 pm
Re: display array element with passing value...no undesstand~help
 
Quote:

Originally Posted by bobei89 (Post 515503)
if(!(a%10))
but what is not (a%10)?

if( !( a % 10 ) ) What's the programmer trying to achieve here?
Is looking for when the result of the operation a % 10 would be 0, however 0 inside an if statement will never execute since in C, 0 is considerate FALSE. Hence the ! added inside the if() to force its execution.

With this information in hand, think about the other examples you're querying about.

bobei89 Jan 23rd, 2008 4:51 am
Re: display array element with passing value...no undesstand~help
 
Yea...i knew why it %20 because it increment is 2, instead of 1....Thx ^^


All times are GMT -4. The time now is 11:13 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC