Write, specify and prove the function negative that
finds whether there occurs a negative number in the array or not,
starting from the first cell of the array
(Note: Prototype is as below) void negative ( int const * arr ,
size_t len){
}
//C code
void negative ( int const * arr , size_t len){
int flag=0;
for(int i=0;i<len;i++)
{
if (arr[i]<0)
flag=1;
}
if (flag==1)
printf("Array contain negative number");
else
printf("No negative number");
}
Output:-
1.For arr={4,-7,5,9,2,6,45} and len=7
Array contain negative number
2.For arr={4,7,5,9,2,6,45} and len=7
No negative number
This function declare a flag variable 0 and check for each element of the array if any element of the array is negative then this flag will be equal to 1.
So, we can easily check if flag will be 1 then there is Array contain negative number. If flag is 0 then No negative number.
Get Answers For Free
Most questions answered within 1 hours.