Click here to Skip to main content
15,912,283 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello,

I made a dynamic array in C and I store some values on it. Now I want to check if there are any values into that are 0 or negative values.


Problem solved

C
int zero = 0;
	
	for(i=0;i<count;i++)
	{
  if(my_array[i]==0)
   zero = zero + 1;
	}
Posted
Updated 20-Nov-14 7:27am
v5
Comments
Sergey Alexandrovich Kryukov 20-Nov-14 11:59am    
You are not even trying to check for negative values. And it's not clear what find_index is supposed to do. First of all, create a clear function which returns value and not print anything. For example, it could be a function returning index of the first occurrence of the number less or equal zero. The code should be ways simple.
—SA
Sergey Alexandrovich Kryukov 20-Nov-14 12:13pm    
Print should be called in the calling function, not the calculating function. Isn't that obvious why?
—SA

find_index(my_array, my_array + count) -- that looks suspicious.

ele != my_array[i]) -- as does that.
 
Share this answer
 
Comments
PIEBALDconsult 20-Nov-14 12:06pm    
I mean take a look at those parts of your code, think about what they say, not what you think they say, and make changes so they say what you mean.

The code shown doesn't do what you describe as its purpose.
My advice is in my comment to the question. So, your problems are: 1) you declared int function, but you don't return anything; 2) you don't even try to check if the element <= 0, only compare with ele which is 0; 3) you should not print anything in the function doing a calculation.

GeorgeGkas wrote:

If the function find negative numbers or number 0 then will return an error message in the main function else if it doesn't then will just proceed the program. Am I clear?
Better.

As I say, don't deal with strings (error message) in a calculating function. Just return an index in the first occurrence of an offending element (<=0), then -1 could denote "no problems". You can issue a message an a calling function, thus making your calculating function more reusable. This message can show this index and/or "bad" value or not.

In that function, first assign -1 to some variable. Have only one loop from <code>0 to 1 - length. In that loop, check up if the current element is <= 0 and return the index immediately if it is. You can assign the index to the variable and break from the loop. Return the variable. If nothing was found, it will remain -1. You won't need any "if".

—SA
 
Share this answer
 
v4
Comments
[no name] 20-Nov-14 12:08pm    
code updated, nothing changed!
Sergey Alexandrovich Kryukov 20-Nov-14 12:12pm    
Good, now please tell us what that code is supposed to do. It should be much simple. The idea to return -1 is good (this is a customary thing for "not found"), but assign -1 to a variable at the very beginning, then update it if the index is found, then always return this variable. No "if"s, no ifs no buts.
—SA
Sergey Alexandrovich Kryukov 20-Nov-14 12:21pm    
No problem. Just define precisely and formally what behavior do you expect.
A simple thing: you return one number some index. An index of what. What to return if you have N 0s and M numbers less than 0?
—SA
Sergey Alexandrovich Kryukov 20-Nov-14 12:45pm    
You don't have to find the solution; I dictated every single detail of it. I must say, the problem is extremely simple. You need to learn how to solve such problems, to do any programming at all.
—SA
Sergey Alexandrovich Kryukov 20-Nov-14 13:12pm    
If you make the code as simple as possible, in such simple cases, it will make it as fast as possible.
Start with writing code which is the simplest and correct.
—SA
For finding zero and less than zero values also you need implement the below

C
#include<stdio.h>
#include<conio.h>
#define count 10

int main
{
int i, my_array[count],flag=0;
printf ("\n Enter my_array values : );
for (i=0;i< count;i++)
scanf ("%d", &a[i]);

for(i=0;i<count;i++)
 { 
if(my_array[i]==0)
{
flag=1;
 printf ("\nZero value at a[%d],i);
}
if (my_array[i]<0)
{
flag=1;
printf ("\nLess than zero value at a[%d],i);
}
}
if(flag==0)
printf ("\nNo zero or less than zero value found.);


getch();
return 0;
}
Hope this helps.</conio.h></stdio.h>
 
Share this answer
 
v5
You have to check Array value 0 or negative(less than 0).

Try this
C++
int zero = 0;
for(i=0;i<count;i++)>
{
   if(my_array[i]<1)  //checking value less than 1, means 0 or less
       zero = zero + 1; // or zero++;
}

'zero' will count number of array indexes which are 0 or negative.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900