2012-03-11 22 views
1

私はこのことに取り組んでいます。 私は、サイズnの整数の2つの配列を合計しなければならない関数を持っています と私は関数の引数として各配列の最初の要素を渡す必要があります これは閉鎖されていますか?整数の配列を関数のパラメータとしてCに渡す

12 void sumVect(int * v[], int * w[] ,int n) 
13 //adds the second vector to the first vector; the vectors v and w have exactly n components 
14 { 
15   int i; 
16   for(i=0;i<n;i++) 
17   { 
18     *v[i]+=*w[i]; 
19   } 
20 
21 } 

私はあなたが機能タイプint []が、あなたの正式なパラメータを渡しているコード全体

#include <stdio.h> 

void sumVect(int * v[], int * w[] ,int n) 
//adds the second vector to the first vector; the vectors v and w have exactly n components 
{ 
    int i; 
    for(i=0;i<n;i++) 
    { 
     *v[i]+=*w[i]; 
    } 

} 


int main() 
{ 
    int i,j; 
    int n = 4; //the size of a vector 
    int k = 5; //the number of vectors 
    int v[k][n]; //the vector of vectors 

    printf("How many vectors? k="); 
    scanf("%d",&k); 

    printf("How many components for each vector? n="); 
    scanf("%d",&n); 

    for(i=1;i<=k;i++) //read the content of each vector from the screen 
    { 
     printf("\nv%d |\n_____|\n",i); 

     for(j=0;j<n;j++) 
     { 
      printf("v%d[%d]=",i,j); 
      scanf("%d", &v[i][j]); 
     } 
    } 

    for(j=1;j<k;j++) 
    { 
     sumVect(&v[j], &v[j+1], n); 
     for(i=0;i<n;i++) 
     { 
      printf("%d",v[j][i]); 
     } 
    } 


    return 0; 
} 

答えて

4

int *[]であなたを与えるだろう。したがって、あなたの署名は次のようになります。

void sumVect(int v[], int w[] ,int n)

または

void sumVect(int *v, int *w,int n)

vwが両方int *またはint []であるため、また、あなたが

v[i] + w[i]のような機能でそれらにアクセスする必要があります(依存正式なパラメータメーターで)

また、あなたがすべき呼び出すとき:

sumVect(v[j], v[j+1], n);

または

sumVect(&v[j], &v[j+1], n);

v[j]int []のタイプであるとしてとして、&v[i]v[i]が同じアドレスに評価されるため、これがあります。

+2

おかげでたくさん、私はあなたに1つのビール:)) – NiCU

+0

downvoteの原因を借りて、完璧な 説明?(注:エラーを修正しました) – phoxis

+0

申し訳ありませんが、私はネイティブの英語ユーザーではありません、あなたはdownvoteの原因によって何を意味しましたか? – NiCU

1

これは関数sumVectの修正バージョンです。

void sumVect(int * v, int * w ,int n) 
//adds the second vector to the first vector; the vectors v and w have exactly n components 
{ 
    int i; 
    for(i=0;i<n;i++) 
    { 
     v[i]+=w[i]; 
    } 

} 

しかし、すべての修正が必要です。 このコードを見てください:

ここ
int n = 4; //the size of a vector 
int k = 5; //the number of vectors 
int v[k][n]; //the vector of vectors 

printf("How many vectors? k="); 
scanf("%d",&k); 

printf("How many components for each vector? n="); 
scanf("%d",&n); 

は、サイズ5x4の2次元配列の割り当てで、その後、あなたのプログラムは、配列のサイズが実際にどうあるべきかをユーザーに尋ねます。 これを修正する最も簡単な方法は、配列のヒープ割り当てを使用することです。

int n = 4; //the size of a vector 
int k = 5; //the number of vectors 
int **v = NULL; //the vector of vectors 

printf("How many vectors? k="); 
scanf("%d",&k); 

printf("How many components for each vector? n="); 
scanf("%d",&n); 
v = malloc(sizeof(int*), k); 
for(int i = 0; i < k; ++i) 
    v[i] = malloc(sizeof(int), n); 

最後に、あなたが同様に割り当てられたメモリを解放するが、それはあなたの宿題も聞かせてください。ここでは固定されたコードです。

+0

OK、それは私があなたの時間に感謝 – NiCU

1

一般に、配列とポインタは、cで同じものとして扱われます。配列変数は配列へのポインタを保持し、[]表記はポインタarithmaticのショートカットです。

nは符号なしの型でなければなりません。

void sumVect(int * v, int * w, uint n) 
{ 
    while (n--) 
    { 
     *v++ += *w++; 
    } 
} 
+0

はい、それを修正しますが、nは、それぞれのベクトルが持つどのように多くのコンポーネントを表し愚かな間違い た(問題の声明は、私はコンポーネントの元の同じ数を持つ二つの配列を追加しなければならないことを言います: v1 = 1 1 1、v2 = 2 2 2) – NiCU

関連する問題