使用函数在给定数组中查找所有四位数的总和甚至否。 (以参考方式致电) [英] Find the sum of all four digit even no in the given array using function. (Call by reference )

查看:109
本文介绍了使用函数在给定数组中查找所有四位数的总和甚至否。 (以参考方式致电)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h> 
int b=0,c=0; 
void sum(int *p,int *m) 
{ 
int i,z=0;
for(i=0;i<*m;i++)
{ 
 if(*p>999&&*p<10000&&*p%2==0) 
z=z+*p; 
p=(p+1);
}

for(i=0;i<*m;i++)
{    
    if(*p<-999&&*p>-10000&&(*p%2)==0) 
    Z=z+*p; 
    p=(p+1);

}
printf("Sum is %d",z);

} 


         
int main() 
{ 
 int n,a[100],i; 
 
 printf("enter the number of elements"); 
 scanf("%d", &n); 
 printf("enter the elements "); 
 for(i=0;i<n;i++) 
  scanf ("%d",&a[i]); 
 sum(a,&n); 
 
 
 } 





我的尝试:



i也试过







What I have tried:

i also tried


#include<stdio.h> 
int b=0,c=0;
void sum(int p[100],int m) 
{ int i,j,l=0,l1=0,a=0,h=0,z=0,x;
for(i=0;i<*m;i++)
{ 
if(p[i]<0 && p[i]%2==0)
{    x=p[i];
    l=x;
    for(j=0;j<4;j++)
 {
    
    if(l>=1)
      {  a++;
    l=l/10;     }
    if(a==4)
    {
    z=z+*p;
    }
    
    }

    
p=(p+1);
a=0;
    
  }
    
else if (*p>0&&*p%2==0)
    l1=*p;
    for(j=0;j<4;j++)
 {    {if(l1>=1)
    h++;
    l1=l1/10;}    }

    if(h==4) 
    {z=z+*p;
     }
p=(p+1);
h=0;
    
}
printf("Sum is %d",z);
} 









任何时候输出都没有获得对数组的输入是四位数negetive





Output is not obtaining when any of the input to array are four digit negetive no

推荐答案

首先帮自己一个忙,然后更改所有变量名。单个字符名称可以快速输入,但它们使代码难以理解,这意味着它不可靠且难以修复。反映变量功能的名称意味着您的代码变得更容易阅读,并且至少部分地自我记录。

首先更改总和函数签名:

Start by doing yourself a favour, and changing all the variable names. single character names may be quick to type, but they make code difficult to understand, and that means it is unreliable and difficult to fix. Names that reflect what the function of the variable is mean that your code becomes easier to read, and at least partially self documenting.
So start by changing your sum function signature to:
int sum(int* data, int length)
   {
   ...
   }

(没有必要传递对元素数量的引用,这只是暗示函数将改变它,它绝对不应该)。使函数返回值意味着它可以被重用,结果打印在 main 方法中。

然后你可以从<$调用它c $ c> main 像这样:

(There is no need to pass a reference to the number of elements, that just implies that the function will change it, which it very definitely shouldn't). Making the function return a value means it can be reused, and the result printed in your main method.
You can then call it from main like this:

printf("Sum is %d\n", sum(a, m));

因为数组的名称是指向它的第一个元素的指针(你不需要取第一个元素的地址,数组的名称也适合你。)



当你这样做时,整理你的缩进所以它是一致的 - 再次,这使你的代码更易读,更容易理解。



然后你可以开始修复它。

这是你的功课,让它上班是这项任务的一部分。幸运的是,您有一个可用的工具,它可以帮助您了解正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!

because the name of an array is a pointer to it's first element (you don't need to take the address of that first element, the name of the array does that for you.)

While you are doing that, sort out your indentation so it is consistent - again, this makes your code more readable, and easier to understand.

Then you can start fixing it.
This is your homework, and getting it to work is part of the task. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


为了你的作业,你需要学习C ++ 。这是一个非常好的教程,所以花时间很好。



小贴士:通过引用打电话是不同的。本文介绍了通过引用正确调用
For your homework you need to Learn C++. It is a really good tutorial, so the time is well spend.

A tip: call by reference is something different. This article explains call by reference correct.


您的实际错误检查正数后,代码检查负数(指针 p 位于数组的末尾:不再有效)。

您应该在同一个循环中检查正数和负数(也是出于性能原因)。

尝试

The actual mistake in your code is checking negative numbers after having checked positive ones (the pointer p is at the end of the array: no more valid).
You should check in the same loop both for positive and negative numbers (also for performance reasons).
Try
#include <stdio.h>

#define MAXSIZE 100

int sum_for_digit_even (int array[], size_t size);

int main()
{
  int size;
  int  a[MAXSIZE];

  printf("please enter the number of elements\n");
  if ( scanf("%d", &size) != 1 || size > 100)
    return -1;
  
  printf("please enter the elements\n");
  
  int i = 0;
  
  while ( i < size )
  { 
    int x;
    if ( scanf ("%d",&x) == 1)
    {
      a[i] = x;
      ++i;
    }
  }
  
  printf("the sum of the four-digit, even numbers is %d\n", sum_for_digit_even( a, size));
  
  return 0;
}

int sum_for_digit_even (int array[], size_t size)
{
  size_t n;
  int sum = 0;

  for ( n = 0; n < size; ++n)
  {
    int x = array[n];

    if ( x < 0 ) x = -x;

    if ( x >= 1000 && x < 10000 && ( x % 2 == 0) )
      sum += array[n];
  }
  return sum;
}


这篇关于使用函数在给定数组中查找所有四位数的总和甚至否。 (以参考方式致电)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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