大佬帮忙看看代码哪里有问题,数组合并不成功,求中位数也没有成功的原因。
```c
#include "stdio.h"
int main()
{
int nums1[] = {1,2,3,4,5,6,7,8,9,10};
int nums2[] = {11,12,13,14,15,16,17,18,19,20};
//定一个空数组来装整个
int array[]={};
int Length_Size1 = sizeof nums1 / sizeof nums1[0];
int Length_Size2 = sizeof nums2 / sizeof nums2[0];
int Length_Size3 = ( sizeof nums1 / sizeof nums1[0]) + (sizeof nums2 / sizeof nums2[0]);
printf("Length_Size1:%d\n",Length_Size1);
printf("Length_Size2:%d\n",Length_Size2);
printf("Length_Size3:%d\n",Length_Size3);
//合并数组
for(int i = 0; i< Length_Size1;i++)
{
array[i]= nums1[i];
}
for(int i = 0;i < Length_Size2;i++)
{
array[Length_Size1+i] = nums2[i];
}
for(int i = 0; i < Length_Size3;i++)
{
printf("array[%d] = %d\n",i,array[i]);
}
for(int i = 0; i <= Length_Size3;i++)
{
if((sizeof array) / 2 == 0)
{
printf("%d\n",array[(sizeof array / 2)] + array[(sizeof array / 2 )+1]);
}
else if((sizeof array) / 2 != 0)
{
printf("%d\n",array[(sizeof array / 2)+1]);
}
}
return 0;
}
```
该程序存在以下问题:
数组定义时没有指定数组大小,导致在进行数组合并和访问数组元素时出现越界问题。
计算数组中位数时,使用了sizeof运算符计算数组大小,这其实是计算数组在内存中所占的总字节数,而不是数组的实际元素个数。
下面是修改后的代码,使得数组合并和计算中位数都正确:
#include <stdio.h>
int main() {
int nums1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int nums2[] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
int array[20];
int length1 = sizeof(nums1) / sizeof(nums1[0]);
int length2 = sizeof(nums2) / sizeof(nums2[0]);
int totalLength = length1 + length2;
//合并数组
for (int i = 0; i < length1; i++) {
array[i] = nums1[i];
}
for (int i = 0; i < length2; i++) {
array[length1 + i] = nums2[i];
}
// 输出合并后的数组
printf("Merged array: ");
for (int i = 0; i < totalLength; i++) {
printf("%d ", array[i]);
}
printf("\n");
// 计算中位数
if (totalLength % 2 == 0) {
int i = totalLength / 2 - 1;
int j = i + 1;
printf("Median: %d\n", (array[i] + array[j]) / 2);
} else {
int i = totalLength / 2;
printf("Median: %d\n", array[i]);
}
return 0;
}
输出结果如下:
Merged array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Median: 10
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
#include "stdio.h"
int main()
{
int nums1[] = {1,2,3,4,5,6,7,8,9,10};
int nums2[] = {11,12,13,14,15,16,17,18,19,20};
//定一个空数组来装整个
int array[20];
int Length_Size1 = sizeof nums1 / sizeof nums1[0];
int Length_Size2 = sizeof nums2 / sizeof nums2[0];
int Length_Size3 = ( sizeof nums1 / sizeof nums1[0]) + (sizeof nums2 / sizeof nums2[0]);
printf("Length_Size1:%d\n",Length_Size1);
printf("Length_Size2:%d\n",Length_Size2);
printf("Length_Size3:%d\n",Length_Size3);
//合并数组
for(int i = 0; i< Length_Size3;i++)
{
if(i < Length_Size1)
{
array[i]= nums1[i];
}
for(int j = 0; j < Length_Size2;j++)
{
array[Length_Size1+i] = nums2[i];
}
}
for(int i = 0; i < Length_Size3;i++)
{
printf("array[%d] = %d\n",i,array[i]);
}
if((sizeof array) / 2 == 0)
{
printf("中位数:%d\n",array[(sizeof array / 2)] + array[(sizeof array / 2 )+1]);
}
else if((sizeof array) / 2 != 0)
{
printf("中位数:%d\n",array[(sizeof array / 2)+1]);
}
return 0;
}
#include<stdio.h>
struct B
{
char c;
short s;
double d;
};
struct stu
{
//成员变量
struct B sb;
char name[20];//名字
int age;//年龄
char id[20];//学号
};
void print1(struct stu t)
{
printf("%c %d %lf %s %d %s\n", t.sb.c, t.sb.s, t.sb.d, t.name, t.age, t.id);
}
void print2(struct stu* ps)
{
printf("%c %d %lf %s %d %s\n", ps->sb.c, ps->sb.s, ps->sb.d, ps->name, ps->age, ps->id);
}
int main()
{
//s是局部变量
struct stu s = { {'w',20,3.14},"张三",30,"202005034" };//对象
//写一个函数打印s的内容
print1(s);//传值调用
print2(&s);//传址调用
return 0;
}
如果大家对函数有不理解的,可以去看我关于函数的文章,下面是链接卡片
对于当前代码有一个疑问,那就是print1() 和print2() 哪个函数更好?
答案:print2()更好
理由:
函数传参的时候,参数是需要压栈的,如果传递一个结构体对象的时候,结构体过大,
参数压栈的系统开销就会比较大,所以会导致性能的下降。
结论:结构体传参的时候,要传结构体的的地址
剖析:
如果采用print1(),采用的是传值调用。s是一个对象,要开辟一块空间。实参传给形参,形参t为了接受实参s,也要开辟一块相同大小的空间。s有多大,t就多大;s有什么数据,t就有什么数据。要把数据传过去,要为传过去的数据要开辟好一块空间,会准备一块额外的临时空间来接受传过去的数据,空间上会有一些浪费。而且把数据传过去也需要一定的时间,时间上也会有一些浪费。
如果采用的是print2(),采用的是传址调用。传过去的是s的地址,指针变量要接受s的地址,如果是32位平台这个指针变量有4个字节,如果是64位平台这个指针变量有8个字节。传参的时候,传过去的是一个地址,是一个编号,是一个4个字节或8个字节的地址。并没有开辟额外太多的空间,而传过去的地址回来之后还是指向s,因为传过去的是s的地址。
两种的区别:
第二种传址调用的传参效率会更高一些,print2()只是传递了一个指针大小的空间。
第一种传值调用,参数传过去的时候会造成时间和空间上的双重浪费,导致效率低下一 些。而且t是一个临时拷贝,改变t并不能改变s。
很抱歉,参考资料和问题并不匹配,参考资料提供的是链表相关的内容,而问题是关于数组合并和求中位数的。因此,无法给出参考资料对问题的帮助。如果您能提供具体的代码和问题描述,我会尽力给出解决方案。