简体   繁体   English

创建一个程序,将两个数字以相等的间隔相除

[英]To create a program that divides two numbers at equal intervals

I made the first part of the program, but I don't understand the part of dynamic memory allocation and calculation using pointers.我做了程序的第一部分,但我不明白动态内存分配和使用指针计算的部分。 What should I coding?我应该编码什么? The contents of the program.节目内容。

  1. Consider creating a sequence of numbers by dividing the two numbers at equal intervals.考虑通过将两个数字以相等的间隔相除来创建一个数字序列。 For example, if you want to create 5 numbers that are evenly spaced from 0.0 to 2.0, you can divide the interval from 0 to 2 into 4 equal parts (considering the value at the end) and arrange the numbers every 0.5.例如,如果要创建5个0.0到2.0之间等距的数字,可以将0到2的区间分成4等份(考虑最后的值),每0.5个数字排列。 It becomes 0.0, 0.5, 1.0, 1.5, 2.0.它变成 0.0, 0.5, 1.0, 1.5, 2.0。
  2. Then, when you enter the first number, the last number, and the total number of values, an array containing the values ​​at equal intervals from the "first number" to the "last number" is created by dynamic memory allocation, and each value is created.然后,当您输入第一个数字、最后一个数字和值的总数时,通过动态内存分配创建一个包含从“第一个数字”到“最后一个数字”等间隔的值的数组,并每个价值都被创造出来。 After displaying, create a program (see execution example) that also displays the square of each value.显示后,创建一个程序(参见执行示例),该程序也显示每个值的平方。
  3. The "first number" and "last number" are floating-point numbers, and the "total number of values" is an integer. “第一个数字”和“最后一个数字”是浮点数,“值的总数”是一个整数。 The memory allocated by dynamic memory allocation should be the minimum required according to the "total number of values".动态内存分配分配的内存应该是根据“值总数”要求的最小值。 Display each value At the first display, output the value stored in the array as it is.显示每个值 在第一次显示时,按原样输出存储在数组中的值。

my source code我的源代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

    
    int main ()
    {
       int num, i;
       double dfirst, dlast;
       double * x;
    
       / * Enter the first value, the last value, and the number to divide * /
       printf ("Input first, last, total number of x []:");
       scanf ("% lf% lf% d", & dfirst, & dlast, & num);
    
       / * Create after this * /
    
       / * Allocate memory * /
    
      / * Determine the value to store.
         Be careful not to divide by an integer and decide the number of divisions in consideration of the end value * /
    
       / * Display results * /
    
       / * Post-processing * /
       return 0;
    }

Execution result want to do执行结果想做什么

% ./a.out
Input first, last, total number of x[]: 0.0  2.0  5
Values of x
0.000 0.500 1.000 1.500 2.000
Values of x^2
0.000 0.250 1.000 2.250 4.000
% ./a.out
Input first, last, total number of x[]: 1.0  2.0  11
Values of x
1.000 1.100 1.200 1.300 1.400 1.500 1.600 1.700 1.800 1.900 2.000
Values of x^2
1.000 1.210 1.440 1.690 1.960 2.250 2.560 2.890 3.240 3.610 4.000

In C you allocate memory dynamically with the function malloc .在 C 中,您使用函数malloc动态分配内存。 This function is quite low-level and the programmer must be very careful to specify the right size of the requested memory.这个函数是相当低级的,程序员必须非常小心地指定所请求内存的正确大小。 To make the function easier to use it can be convenient to define a macro which also provides error handling:为了使函数更易于使用,可以方便地定义一个也提供错误处理的宏:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NEW_ARRAY(ptr, n) \
    (ptr) = malloc((n) * sizeof (ptr)[0]); \
    if ((ptr) == NULL) { \
        fprintf(stderr, "Memory allocation failed: %s\n", strerror(errno)); \
        exit(EXIT_FAILURE); \
    }

With the macro function in place, you can simply add宏功能到位后,您只需添加

NEW_ARRAY(x, num);

to your program to allocate an array x of num elements.给你的程序分配一个由num 个元素组成的数组x When you are done with x you free the memory with free ( x ).当您完成x 时,您可以使用free ( x ) 释放内存。 In your program you also need to check the result from scanf to make sure the input is valid.在您的程序中,您还需要检查scanf的结果以确保输入有效。 Good luck!祝你好运!

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

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