Write a C program to enter the elements of three one dimensional array A (size 5), B (size 10) and C (size 5) as an input from the keyboard. The program will find and print:

The maximum element of array A entered as an input.

The minimum element of array B entered as an input.

Total number of negative numbers divisible by 3 in array C and B entered as an input.

This is the question, i managed to do the first two parts, but not the last one.

I tried doing it but i think my code has a problem


The Program

#include <stdio.h>
#include <conio.h>
void main ()

{
  int max;
  int A[5];
  int i;
  int j;
  int B[10];
  int C[5];
  int min;
  int k;
  int count1=0;

 for(i=1;i<=5;i++)
  {
   printf("\nEnter Element A :	");
   scanf("%d",&A[i]);
  }

  max=A[0];
  for(i=1;i<=5;i++)
   {
    if(max<A[i])
     {
      max=A[i];
     }
   }
printf("\nMax in Array A is Equal to : %d",max);
printf("\n-----------------");

 for(j=1;j<=10;j++)
  {
   printf("\nEnter Element B :	");
   scanf("%d",&B[j]);
  }

  min=B[0];
  for(j=1;j<=10;j++)
   {
    if(min>B[j])
     {
      min=B[j];
     }
   }
printf("\nMin in Array B is Equal to : %d",min);
printf("\n-----------------");

 for(k=1;k<=5;k++)
  {
   printf("\nEnter Element C :	");
   scanf("%d",&C[k]);
  }
if(C[k] && B[j]<0 && C[k] && B[j]%3==0)
  {
  count1++ ;
  }
printf("\nTotal negative number in Array B and C : %d",count1);
getche();

}

Please consider the bold part.

Thanks

in your program (maximum value), your loop is starting with i=1 ,
but you've set the max value to A[0] ???
you didn't assign any value for A[0],
how is your program finding the max value ?

if you set the max value at A[0] , you must start your loop from i=0 and it should be less than 5 i.e i<5 similarly, for min value your loop should be (j=0;j<10;j++) because you've set first min value to B[0]

and for your third question,
use functions to call the array of

B

while printing the values of both B & C,
and use proper brackets otherwise your program will not work,
i.e you placed wrong brackets in the third part of your program ,
this should be like this,

for(k=0;k<5;k++)
{
printf("\nEnter Element C : ");
scanf("%d",&C[k]);
for (j=0;j<5;j++)
if(C[k]<0 && C[k]%3==0)
	{
	count1++ ;
	}
}
printf("\nTotal negative number in Array B and C : %d",count1);

this code will find the negative numbers divisible by 3 in onle array C

Thank you Xufyan for your helpful Reply, Appreciated.

and for the Third part, you have written only for C[k] Array, can you please find as the question says,Total number of negative numbers divisible by 3 in array C and B entered as an input.
as in one answer ?

Thanks Alot

count the values of both arrays separately,
here is the code for static program,

int main (void)
{
int B[5]={-3,-6,1,5,7};
int C[5]={-3,1,1,2,3};
int k,count1=0,j;
	{
	for(k=0;k<5;k++)
	if(C[k]<0 && C[k]%3==0)
		{
		count1++ ;
		}
	}
        {
	for (j=0;j<5;j++)
		if(B[j]<0 && B[j]%3==0)
		{
		count1++ ;
		}
	}
printf("\nTotal negative number that are divisible by 3 in Array B and C : %d",count1);
getche();
}

now use your own sense to find it in dynamic program.

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.