How would I write this function in C (arrays)?

This is actually the question:

Compose a operate int all_zero(int YOUR, int n) which in turn returns true in the event the elements A0 to be able to An-1 are all zero, and phony if any ones are non-zero.

Any help will be much treasured!

Thank you, James.

Since your boolean datatype seriously isn’t supported with C, we can not return true or false from the function inside C.So we can use integer datatype with values ONE and 0 for your purpose.ONE means true, 0 means false.Note that even inside your question, the operate return type in integer.The next function will conduct the needful, with an added printf inside the function

Logic is checking each element if its totally free or not.We’ll try a flag to do that.Flag is focused as JUST ONE.If quite a few element seriously isn’t zero after that flag might be 0 as well as break the particular loop.If flag is 1 right after executing the loop after that all components are 0.Appreciate!!!

int all_zero(int THE, int n)

int flag=1;
for(int i=0; i<n; i++)

if(Ai! =0)

flag=0;
crack;

if(flag==1)
go back 1;
else
go back 0;

piece code pertaining to printing
if(all_zero(arr, 10) )
printf(“All your elements from the array is 0”);

The plan statement is slightly self-contradicting.The operate declaration is shown to return int, nevertheless the text tackles returning true and bogus, which usually are values belonging to the C variety bool (remember in order to #include if you are planning to apply it)

I’ll show tips on how to use bool, considering you’ve witout a doubt gotten advice that use int

bool all_zero(const int THE, unsigned int n)

while(n–)
if(An)
go back false;
go back true;

and also, if you no longer like a number of return tips, there are lots of other methods of write the following loop, intended for example

bool all_zero(const int THE, unsigned int n)

bool result = true;
while(n–)
result &=! The; // and also if(An) result=false;
go back result;

analyze:
https://ideone.com/Upm05

Sure, what help are you wanting How far to get gotten What’s giving you trouble

Believe write it similar to this:

1) Cycle from 0 that will n-1.
2) For each value, find out if that element is no.If not really, return phony.
3) Should you finish your loop, go back true, almost all values will be zero.

int all_zero(int THE, int n)

for(int i=0; i<A.size; i++)

if(Ai ==0)
continue;
else
go back false;

go back true;

Anticipation this facilitates, thanks.

Leave a Reply