#include<stdio.h>
#include<stdlib.h>
int main(void){
int size = 2;
float* ptr;
float t;
int count = 0;
ptr = calloc(size, sizeof(float)); // dynamic memory allocation for an array to intial size of 5
while(1){
if(t == -100.0) // While loop repeats till a value of -100.0 is entered
break;
if(count == size){
size *= 2;
ptr = realloc(ptr, size * sizeof(float)); // reallocation of memory to array by double the size
}
scanf("%f", &t);
ptr[count] = t;
count++; // counting array elements
}
printf("You have entered the following temperatures : ");
for(int i=0; i<count-1; i++){
printf("%.1lf ", ptr[i]); // printing temperature values
}
printf("\nTemperatures in reverse order : ");
for(int i = count-2; i>=0; i--){
printf("%.1f\t", ptr[i]); // printing temperature values in reverse
}
free(ptr);
return 0;
}
c语言问题 无法运行
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答 默认 最新
- qfl_sdu 2021-05-23 21:24关注
一个是t没有初始化,二是ptr需要强制类型转换一下。代码修改如下,如有帮助,请采纳一下,谢谢。
#include<stdio.h> #include<stdlib.h> int main(void){ int size = 2; float* ptr; float t = 0.0; int count = 0; ptr = (float*)calloc(size, sizeof(float)); // dynamic memory allocation for an array to intial size of 5 while(1){ if(t == -100.0) // While loop repeats till a value of -100.0 is entered break; if(count == size){ size *= 2; ptr = (float*)realloc(ptr, size * sizeof(float)); // reallocation of memory to array by double the size } scanf("%f", &t); ptr[count] = t; count++; // counting array elements } printf("You have entered the following temperatures : "); for(int i=0; i<count-1; i++){ printf("%.1lf ", ptr[i]); // printing temperature values } printf("\nTemperatures in reverse order : "); for(int i = count-2; i>=0; i--){ printf("%.1f\t", ptr[i]); // printing temperature values in reverse } free(ptr); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 delta降尺度方法,未来数据怎么降尺度
- ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
- ¥15 再不同版本的系统上,TCP传输速度不一致
- ¥15 高德地图点聚合中Marker的位置无法实时更新
- ¥15 DIFY API Endpoint 问题。
- ¥20 sub地址DHCP问题
- ¥15 delta降尺度计算的一些细节,有偿
- ¥15 Arduino红外遥控代码有问题
- ¥15 数值计算离散正交多项式
- ¥30 数值计算均差系数编程