簡體   English   中英

試圖在數組 c99 中找到最大的元素

[英]Trying to find the largest element in an array c99

我試圖找到最大的元素是一個大小的數組,但是我無法從概念上理解我做錯了什么如果有人能指出我正確的方向,我將不勝感激!

 #include <stdio.h>
         void max(int a[], int n)
    {
  int *max ;
  int * ind_max;
  int i;

  *max = a[0];
  * ind_max=*max ;
  for (i = 1; i < n; i++) {
    if (a[i] > *max) {
      *max = a[i]; *ind_max = i;
    }
    else
        ;
    printf("Largest element is at position: %p\n",(void * )ind_max );

  }
 }

int main(void)
{
    int k,ch;
    k=0;
    int a[k];
    while (1){
        printf("Enter a series of numbers seperated by a space:");
        ch=getchar();
        if (ch==' ')
            k++;
        if (ch=='/n')
            break;
    }
    max(a[k],k);


}

目前尚不清楚您得到的結果與您期望的結果,但正如@castle-bravo 指出的那樣,我沒有看到您填充數組,而且,數組索引從 0 開始,而不是 1: for(i = 0; i < n; i++)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM