将2个排序的数组连接成一个数组? [英] joining 2 sorted array into one array ?

查看:90
本文介绍了将2个排序的数组连接成一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了一个代码:



i got a code :

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,p,q,x[50],y[50];
void merge(int x[],int y[],int p,int q);
printf("Enter the length of the 1st sorted array :");
scanf("%d",&p);
printf("Enter the sorted values of the 1st array :");
for(i=0;i<=p;i++)
scanf("%d",&x[i]);
printf("Enter the sorted values of the 2nd array :");
scanf("%d",&q);
printf("Enter the sorted values of the 2nd array :");
for(j=0;j<=q;j++)
scanf("%d",&y[j]);
merge(x[],y[],p,q);
getch();  }
void merge(int x[],int y[],int p,int q)
{
int i=0,j=0,k=0,z[100];
while(i<p mode="hold" />{
if(x[i]<y[j])>
{
z[k]=x[i];
i++;
k++;
}
else if(x[i]>y[j])
{
z[k]=y[j];
j++;
k++;
}
else
{
z[k]=x[i];
i++;
j++;
k++;
}
}while(i<p)>
{
z[k]=x[i];
i++;
k++;
}
while(j<q)>
{
z[k]=y[j];
j++;
k++;
}
printf("The new merged array is :");
for(i=0;i<k;i++)>
{
printf("%d",z[i]);
}
}









但在编译期间我得到了表达式语法....为什么???我不能理解......请帮助我.....





but during compilation i got " Expression syntax " ....why??? i can''t undrstand ...please help me.....

推荐答案

如果您使用Visual Studio C ++ 2010创建一个控制台应用程序,您应该得到类似的东西:



If you create a Console application using Visual Studio C++ 2010, you should get something like that:

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int i,j,p,q,x[50],y[50];
    void merge(int x[],int y[],int p,int q);
    printf("Enter the length of the 1st sorted array :");
    scanf("%d",&p);
    printf("Enter the sorted values of the 1st array :");
    for(i=0;i<=p;i++)
    scanf("%d",&x[i]);
    printf("Enter the sorted values of the 2nd array :");
    scanf("%d",&q);
    printf("Enter the sorted values of the 2nd array :");
    for(j=0;j<=q;j++) scanf("%d",&y[j]);
    merge(x,y,p,q);
    getch();
}
void merge(int x[],int y[],int p,int q)
{
    int i=0,j=0,k=0,z[100];
    while(i)
    {
        if(x[i]<y[j])
        {
            z[k]=x[i];
            i++;
            k++;
        }
        else if(x[i]>y[j])
        {
            z[k]=y[j];
            j++;
            k++;
        }
        else
        {
            z[k]=x[i];
            i++;
            j++;
            k++;
        }
    }
    while(i<p)
    {
        z[k]=x[i];
        i++;
        k++;
    }
    while(j<q)
    {
        z[k]=y[j];
        j++;
        k++;
    }
    printf("The new merged array is :");
    for(i=0;i<k;i++)
    {
        printf("%d",z[i]);
    }
    return;
}


看起来像是家庭作业...



1.什么是:

Looks like a homework assignment...

1. What is:
#include
#include







2.您是否听说过缩进?



3.首先,让我们来处理所需的头文件。



4.更改此行




2. Have you heard about indentation?

3. First, lets take care of the required header files.

4. Change this line

merge(x[],y[],p,q);



进入


into

merge(x,y,p,q);





5.更改



5. Change

<pre lang="c++">

while(i

into

while(i
into

while(i)





(我告诉你要修改你的代码!!!!)



6.这条线是什么?



(I TOLD YOU TO INDENT YOUR CODE !!!!)

6. What is this line?

if(x[i]<y[j])>





将其更改为



Change it to

if(x[i]<y[j])





7.与

相同



7. Same with

while(i<p)>









while(j< q)>



and

while(j<q)>






和还有



and also

for(i=0;i<k;i++)>





8.更改返回0;回归;因为合并没有定义为返回任何内容。它定义为



8. Change return 0; into return; as merge is not defined to return anything. It is defined as

void


这篇关于将2个排序的数组连接成一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆