C Primer Plus(第六版)12.9 编程练习 第8题

#include <stdio.h>

#include <stdlib.h>

int * make_array(int elem, int val);

void show_array(const int ar[], int n);

int main(void)

{

int * pa;

int size;

int value;

printf("Enter the number of elements:");

while(scanf("%d", &size) == 1 && size > 0)

{

printf("Enter the initialization value:");

scanf("%d", &value);

pa = make_array(size, value);

if(pa)

{

show_array(pa, size);

free(pa);

}

printf("Enter the number of elements (<1 to quit):");

}

printf("Done!");

return 0;

}

int * make_array(int elem, int val)

{

int*p;

int i;

p=malloc(elem*sizeof(int));

for(i=0;i<elem;i++)

*(p+i)=val;

return p;

}

void show_array(const int ar[], int n)

{

int i;

for(i=0;i<n;i++)

{

printf("pa[%d]=%d",i,ar[i]);

if((i+1)%8==0)

printf("\n");

}

}

相关推荐
xiezhr1 天前
接口设计18条军规:写给那些半夜被“502”叫醒的人
java·api·restful
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
葡萄城技术团队3 天前
REST API 设计最佳实践指南 - 如何用 JavaScript、Node.js 和 Express.js 构建 REST API
restful
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔3 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔3 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing102329293 天前
Day03_刷题niuke20250915
c语言