简体   繁体   English

C 编程数组元素的平均值

[英]C program the average value of the elements of the array

If the array element is twice as large as its predecessor (for example 1,2,3,4,.. in this case element 2 is twice as large as element before 2, which is 1) then program should make average value of these two elements (in this case 1 + 2 = 3, 3/2 = 1.5) and after making average value, program should put that value between these two numbers and print new array.如果数组元素是其前一个元素的两倍(例如 1,2,3,4,.. 在这种情况下,元素 2 是 2 之前的元素的两倍,即 1),那么程序应该对这些元素取平均值两个元素(在本例中为 1 + 2 = 3, 3/2 = 1.5),在取平均值后,程序应将该值放在这两个数字之间并打印新数组。 (in this case new array would look like: 1, 1.5, 2, 3, 4, ...) Do you have any idea how to make this? (在这种情况下,新数组看起来像:1, 1.5, 2, 3, 4, ...)你知道怎么做吗? Array consists of double numbers.数组由双数组成。

#include <math.h>
#include <stdio.h>
#define epsilon 0.0001
int main() {
  int i, k = 0, n;
  double arr[100];
  do {
    printf("Enter number of array elements: ");
    scanf("%d", &n);
  } while (n <= 0 || n > 100);
  printf("Enter array: ");
  for (i = 0; i < n; i++) {
    scanf("%lf", &arr[i]);
  }
  for (i = 0; i < n; i++) {
    if (fabs(arr[i + 1] - 2 * arr[i]) < epsilon) {
     
    }
  }
  return 0;
}

I think you need just check if next element is at least twice as large as previous and then print additional number in a sequence.我认为您只需要检查下一个元素是否至少是前一个元素的两倍,然后按顺序打印附加数字。

int main() {
  int i, k = 0, n;
  double arr[100];
  do {
    printf("Enter number of array elements: ");
    scanf("%d", &n);
  } while (n <= 0 || n > 100);
  printf("Enter array: ");
  for (i = 0; i < n; i++) {
    scanf("%lf", &arr[i]);
  }
  printf("%f ", arr[0]);
  double med = 0;
  for (i = 0; i < n - 1; i++) {
    if (arr[i+1] >= 2 * arr[i]) {
      med = (arr[i + 1] + arr[i]) / 2;
      printf("%f ", med);
    }
    printf("%f ", arr[i+1]);
  }
  return 0;
}

To address comments suggesting how to create array at runtime ...为了解决建议如何在运行时创建数组的评论......

(and a few other suggestions are in comments in code below.) (以及其他一些建议在下面代码的注释中。)

Rather than creating an array with set size, create one with the number of elements decided by user by using a VLA .与其创建具有设定大小的数组,不如使用 VLA创建一个具有用户决定的元素数量的数组。 It is a form of dynamically placing an array into stack (as opposed to heap) memory , as such, it does not require use of calloc() et.al.它是一种动态地将数组放入堆栈(而不是堆) memory的形式,因此,它不需要使用calloc()等。 so no need to call free() Example using your original code:所以无需使用您的原始代码调用free()示例:

int main(void) {//main() is not a prototyped signature of main. Use void parameter.
  int i = 0 /*k = 0*/, n;//k is not used, init i

//double arr[100] = {0};//better to initialized before using
                    //and even better to use VLA (if using C99 or newer.)
  do {
        if(i++ > 3) {//prevent infinite loop possibility
           printf("%s", "Exiting\n"); 
           return 0;    
        }
        printf("Enter number of array elements: ");
        scanf("%d", &n);
  } while (n <= 0 || n > 100);
  double arr[n];//Variable Length Array (VLA)
  memset(arr, 0, sizeof arr);/initialize to zero
  ...

Be sure to read the VLA link above, as VLA does have its caveats, but will work fine in the case of your code.请务必阅读上面的 VLA 链接,因为 VLA 确实有其警告,但在您的代码的情况下可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM