C 创建以相等间隔分割两个数字的程序
我做了程序的第一部分,但我不了解动态内存分配和使用指针计算的部分。我应该如何编码? 节目内容。C 创建以相等间隔分割两个数字的程序,c,C,我做了程序的第一部分,但我不了解动态内存分配和使用指针计算的部分。我应该如何编码? 节目内容。 考虑通过将两个数字相除来创建一个数字序列 以相等的间隔。例如,如果要创建5个数字 如果间隔在0.0到2.0之间,则可以分割间隔 从0到2分成4等份(考虑末尾的值) 每0.5排一次。它变成了0.0,0.5,1.0,1.5, 2.0 然后,当您输入第一个数字、最后一个数字和 值的总数,包含值的数组同等 从“第一个数字”到“最后一个数字”的间隔由 动态内存分配,并创建每个值。之后 显示、创建一个程序(参
#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;
}
在C语言中,使用函数malloc动态分配内存。这个函数是非常低级的,程序员必须非常小心地指定所请求内存的正确大小。为了使函数更易于使用,可以方便地定义一个宏,该宏还提供错误处理:
#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); \
}
向程序分配一个数组x的num元素。当你用x完成时,你用free(x)释放内存。在您的程序中,您还需要检查scanf的结果,以确保输入有效。祝你好运 在C中,可以使用函数malloc动态分配内存。这个函数是非常低级的,程序员必须非常小心地指定所请求内存的正确大小。为了使函数更易于使用,可以方便地定义一个宏,该宏还提供错误处理:
#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); \
}
向程序分配一个数组x的num元素。当你用x完成时,你用free(x)释放内存。在您的程序中,您还需要检查scanf的结果,以确保输入有效。祝你好运 我强烈建议阅读和查找有关ammloc | calloc的信息。其中一个有用的链接是。下一步,因为有了开始、结束和除数,所以需要检查边界和特殊情况。然后使用素数或
end/2
来识别逻辑。我强烈建议阅读和查找有关ammloc | calloc的内容。其中一个有用的链接是。下一步,因为有了开始、结束和除数,所以需要检查边界和特殊情况。然后使用素数或end/2
来识别逻辑。
NEW_ARRAY(x, num);