2013-03-06 51 views
-1

我正在嘗試計算數組中某個值的頻率。我認爲自己目前的參賽作品正處於正確的軌道上,但它似乎算錯了「數」。我會喜歡關於如何正確顯示陣列的頻率的第二意見!下面是我有:C - 計算數組中數字的頻率

#include <stdio.h> 

/************************************************************************/ 
/*      Function: frequency       */ 
/*                  */ 
/* Purpose: Obtains the frequency of a number in an array   */ 
/*                  */ 
/*                  */ 
/*                  */ 
/* Parameters: theArray-The array in question       */ 
/*    n- the size of the array        */ 
/*    x- the number to search for frequency within the array */ 
/*                  */ 
/* Returns: The frequency of a given number in an array    */ 
/*                  */ 
/************************************************************************/ 

int frequency (int theArray [ ], int n, int x) 
{ 
    int count = 0; 
    int i; 

    for (i = 0; i < n; ++i) 
    { 
     if (theArray[i]=x) 
     { 
      count = count + 1 ; 
     }  
     else 
     { 
      count = count ; 
     } 
    } 

     printf ("\nThe frequency of %i in your array is %i ",x,count); 
    } 

    int main() 
    { 
     int i;  
     int theArray[] = {}; 
     int n; 
     int x; 


     printf ("Enter The Amount Of Numbers In Your Array: "); 
     scanf("%i", &n);/*Stores Amound Of Numbers In The Array*/ 

     for (i = 0; i < n; ++i) 
     { 
      printf("\nEnter number for array: ");  
      scanf ("%i", &theArray[i]); 
     } 

     printf ("\nOK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? "); 
     scanf("%i", &x);/*Stores Number To Search For Frequency*/ 

     frequency(theArray,n,x); 

     return(0); /* success */ 

    } /* main */ 
+2

就像一個快速評論。如果你有一個if語句,其做一些事情,和別人不其什麼,只是省略了其他!此外,你已經宣佈你的頻率函數返回一個int值。目前它什麼都不返回。您應該將int更改爲void,或者返回一個值。 – 2013-03-06 01:53:48

回答

2

件3事情

  1. if (theArray[i]=x) =應該是==

  2. 整個其他{count = count;}是多餘的。

  3. 的功能應該是void

+1

關於數字1,那個'if'在這種形式中總是如此。 – 2013-03-06 02:01:54

+0

謝謝特拉維斯!這工作完美 – Americo 2013-03-06 02:10:42

3

if (theArray[i]=x)應該是==

而整個else {count = count;}是多餘的。

而你實際上並沒有返回計數(或任何其他) - 即使簽名說你做。

3

你有一個錯字:

if (theArray[i] = x) 

應該是:

if (theArray[i] == x) 

前者將設置theArray[i]x,將永遠爲if語句返回true,而你想要做的是什麼第二個是邏輯等同性檢查。

在附註中,您的else聲明也是多餘的,您不需要else { count=count; }聲部。此外,您的函數沒有返回值,因此最好將返回值從int更改爲void

0

創建數組後,不能將元素添加到數組中。 int theArray [] = {}; ...是一個空數組。

你必須:

  1. 詢問陣列
  2. 動態創建數組的大小(使用malloc())
  3. 添加數字
  4. 傳遞數組到頻率()作爲指針
  5. 在退出之前刪除數組(使用免費())
-2
#include<stdio.h> 

int main(){ 
    int n, t, i, j, arr[30],len, halflen,flag=0,count=0; 

    printf("Enter number of elements to insert in an array:"); 
    scanf("%d",&len); 
    printf("Enter elements to insert in an array:"); 
    for(i=0;i<len;i++){ 
     scanf("%d",&t); 
     arr[i]=t; 
    } 
    printf("\n"); 

    /*****************************/ 
    for(i=0;i<len;i++){ 
     count=1; 
     for(j=i+1;j<=len-1;j++){ 
      if(arr[i]==arr[j] && arr[i]!='\0'){ 
       count++; 
       arr[j]='\0'; 
      } 
     } 
     if(arr[i]!='\0'){ 
      printf("%d is %d times.\n",arr[i],count); 
     } 
    } 

    /*****************************/ 
    getch(); 
    return 0; 
} 
0
#include<stdio.h> 
#include<conio.h> 
#define MAX 10 
int flag=0; 
void display(int no,int cnt,int visi[]); 

void main() 
{ 
    int arr[]={1,1,1,2,3,4,2,2,3,1}; 
    int visited[MAX]; 
    int i,j,no,cnt=1; 
    clrscr(); 
    for(i=0;i<10;i++) 
    { 
    no=arr[i]; 
    cnt=1; 
    for(j=i+1;j<10;j++) 
    { 
     if(no==arr[j]) 
     cnt++; 
    } 
    display(no,cnt,visited); 
    } 
} 

void display(int no,int cnt,int visited[]) 
{ 
    int static i; 
    int j; 

    if(flag==1) 
    for(j=0;j<=i;j++) 
    { 
     if(visited[j]==no) 
     return; 
    } 
    i++; 
    flag=1; 
    printf("\n%d=%d",no,cnt); 
    visited[i]=no; 
}