我有一個函數可以產生2個數組,其中一個是索引'i'是偶數,另一個是索引'i'是奇數時,所以我最終得到了兩個數組。偶數'i'數組稱爲W_e,由N個元素組成,奇數'i'數組稱爲W_o,也由N個元素組成。將兩個數組的條目合併爲另一個數組的偶數和奇數條目。
我現在需要將這兩個數組合併到另一個數組Wn(它有2 * N個元素),使得它看起來像Wn = [W_e [0],W_o [0],W_e [1],W_o [ 1],...,W_e [N-1],W_o [N-1]]但我不知道該怎麼做。我試圖使用嵌套循環,但它沒有工作。數組W_e和W_o根據我的計算正確生成,我只是無法將條目組合到一個數組中。
這是我到目前爲止。除了調用給我帶來麻煩的函數「double * MakeWpowers(int N);」之外,我在主函數中沒有做任何事情。請記住這適用於N> 2,我還沒有處理N = 1或N = 2。
任何幫助將不勝感激。謝謝!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#define pi 4*atan(1)
double *MakeWpowers(int N);
void print_vector(double *x,int N);
double *make_vector(double *x,int N);
int main(void)
{ int N;
double *Wn;
printf("\n Please enter the size of the NxN matrix:\n");
scanf("%d",&N);
Wn=MakeWpowers(N);
//print_vector(Wn,N);
free(Wn);
return(0);
}
double *MakeWpowers(int N)
{
double *Wn,*W_e,*W_o;
int i,j;
Wn=make_vector(Wn, 2*N);
W_e=make_vector(W_e, N);
W_o=make_vector(W_o, N);
for(i=0;i<=N-1;i++)
{
if(i%2==0)
{
W_e[i]=cos((2*i*pi)/N);
}
else
{
W_o[i]=sin((2*i*pi)/N);
}
}
printf("\nThis is the even vector W_e:\n");
print_vector(W_e, N);
printf("\nThis is the odd vector W_o:\n");
print_vector(W_o, N);
for(j=0;j<2*N;j++)
{
if(j%2==0)
{Wn[j]=W_e[i];}
//++i;}
else
{Wn[j]=W_o[i];}
//++i;}
printf("\nthis is Wn:\n\n");
print_vector(Wn, 2*N);
//Wn[j]=W_o[i];
//j++;
}
return(Wn);
}
void print_vector(double *x,int N)
{
int i;
for (i=0; i<N; i++)
{
printf ("%9.4f \n", x[i]);
}
printf("\n");
}
double *make_vector(double *x,int N)
{ int i;
double xi;
x=(double *)malloc((N)*sizeof(double));
for(i=0;i<N;i++)
{
x[i]=(double)xi;
}
return(x);
}
homeworkk? hein? – 2013-04-02 14:21:15
是的。但問題比這個大得多,這只是一個非常大的問題中的一個小問題。 –