如何搜索值 [英] How do I search for values

查看:97
本文介绍了如何搜索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我在C中创建了一个动态数组,并在其上存储了一些值。现在我想检查是否有任何值为0或负值。





问题解决了



  int  zero =  0  ; 

for (i = 0 ; i< count; i ++)
{
if (my_array [i] == 0
zero = 0 + 1 ;
}

解决方案

find_index(my_array, my_array + count - 看起来很可疑。



ele!= my_array [i]) - 就像那样。


我的建议是在我对这个问题的评论中。所以,你的问题是:1)你声明了int函数,但你没有返回任何东西; 2)你甚至没有尝试检查元素< = 0,只与 ele 比较,它是0; 3)你不应该在执行计算的函数中打印任何东西。



GeorgeGkas写道:



如果函数找到负数或数字0,那么将在主函数中返回错误消息,否则它将继续执行程序。我清楚了吗?

更好。



正如我所说,不要在计算函数中处理字符串(错误消息)。只需在第一次出现的违规元素(< = 0)中返回一个索引,然后-1可以表示没有问题。您可以发出消息和调用函数,从而使您的计算功能更可重用。此消息可以显示此索引和/或坏值。



在该函数中,首先将-1分配给某个变量。只有一个循环从< code> 0到 1 - length 。在该循环中,检查当前元素是否为< = 0并且如果是,则立即返回索引。您可以将索引分配给变量并从循环中断。返回变量。如果没有找到,它将保持-1。你不需要任何if。



-SA


用于查找零和小于零的值你还需要实现以下



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

int main
{
int i,my_array [count],flag = 0 ;
printf( \ n输入my_array值:);
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 在[%d],i);
}
if (my_array [i]< 0)
{
flag = 1 ;
printf( \ n比[%d]处的零值更多,i);
}
}
if(flag == 0)
printf(
\\\
No zero 小于找到零。);


getch();
return 0 ;
}
希望帮助。< /conio.h>< /stdio.h>


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

int zero = 0;
	
	for(i=0;i<count;i++)
	{
  if(my_array[i]==0)
   zero = zero + 1;
	}

解决方案

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

ele != my_array[i]) -- as does that.


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


For finding zero and less than zero values also you need implement the below

#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>


这篇关于如何搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆