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);
}

What part to you not understand?

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

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.

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.

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.

Yea...i knew why it %20 because it increment is 2, instead of 1....Thx ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.