그래서 프로그래밍 할당을하고 있는데 문제가 발생했습니다. 배열을 헤더 파일에 전달하려고 할 때마다 컴파일하는 동안 오류가 발생합니다. 이 작업을 수행하는 방법이 너무 명확하지 않으며 이러한 배열을 전달하는 데 많은 도움을 주실 것입니다.배열을 매개 변수로 함수에 전달
다음은 헤더 파일 'sorting.h "
#include <iostream>
#include <cstdlib>
using namespace std;
int cost = 0;
void bubble(int Ar[],int N)
{
cost=0;
int swaps = 1;
while(swaps)
{
swaps=0;
for(int i = 0;i<N;i++)
{
if(Ar[i]>Ar[i++])
{
swap(Ar[i],Ar[i++]);
swaps = 1;
cost += 6;
}
cost++;
}
}
for(int i=0;i<N;i++)
{
cout<<Ar[i]<<endl;
}
cout<<cost<<endl;
}
void shellSort(int Ar[], int N)
{
cost=0;
int swaps = 1;
int gap = N/2;
while(gap>0)
{
while(swaps)
{
swaps = 0;
for(int i = 0;i<N;i++)
{
if(Ar[i]>Ar[i+gap])
{
swap(Ar[i],Ar[i+gap]);
swaps = 1;
cost+=6;
}
cost++;
}
}
gap=gap/2;
}
for(int i = 0;i<N;i++)
{
cout<<Ar[i]<<endl;
}
cout<<cost<<endl;
}
void quickSort(int Ar[],int left, int right, int N)
{
cost = 0;
int i=left,j=right,tmp;
int pivot = Ar[(left+right)/2];
/*partition*/
while(i<=j)
{
while(Ar[i]<pivot)i++;
while(Ar[j]>pivot)j--;
if(i<=j)
{
tmp=Ar[i];
Ar[i]=Ar[j];
Ar[j]=tmp;
i++;
j--;
cost +=6;
}
cost+=1;
}
/* recursion*/
if(left<j)quickSort(Ar,left,j,N);
if(i<right)quickSort(Ar,i,right,N);
for(int i=0;i<N;i++)
{
cout<<Ar[i]<<endl;
}
cout<<cost<<endl;
}
/*#if _INCLUDE_LEVEL__<1
int main()
{
}
#endif*/
이며 여기에 주 파일"sorting2.cpp "
#include <iostream>
#include <cstdlib>
#include "sorting.h"
using namespace std;
//void bubble();
//void shellSort();
//void quickSort();
int main()
{
int N = 20;
int Ar[N];
int Ar2[N];
for(int i = 0;i<N;i++)
{
Ar[i] = Ar2[i] = rand()%100;
}
bubble(Ar[],N);
for(int i = 0;i<N;i++)
{
Ar[i] = Ar2[i];
}
shellSort(Ar[],N);
for(int i = 0;i<N;i++)
{
Ar[i] = Ar2[i];
}
quickSort(Ar[],0,19,N);
}
미리 감사입니다!
'main()'에있는 함수 호출의 인자 목록에서'[]'을 잃습니다 – WhozCraig
당신이 실제로하고있는 소품과 관련이 없습니다 스왑 체크 (당신은 얼마나 많은 사람들이 놀라지 않을까?)와 함께 기품없는 버블 정렬을하지만 버블 정렬은 배열의 맨 마지막까지 반복적으로 정렬됩니다. 'while (swaps && N--) {swaps = 0; for (i = 0; i
WhozCraig
구현이 헤더에 있으므로, 다중 정의를 피하기 위해 함수는'inline'이어야합니다. (그러나 헤더/cpp에 선언과 정의를 나누는 것이 더 좋습니다.) – Jarod42