簡體   English   中英

數組中質數的平均值

[英]Average of prime numbers in an array

好,問題就在上面。 總結起來,它可以編譯,但是我想我的主要想法是錯誤的。 我正在嘗試使用該代碼的是:

  • 我希望這個人給我們數組的元素,他想要多少(最多100個元素)。
  • 之后,我要檢查哪些數組位置是質數。(例如:位置2、3、5等,不是元素本身)。
  • 之后,我要對質數位置中的值進行平均。

而已。 有任何想法嗎? 請記住,我處於工程的第一階段,所以我並不是編程方面的佼佼者。

代碼如下:

#include <stdio.h>
#include <windows.h>
int main(void)
{
    int k, i, j, d, v[101], sum, prim, f;
    float ave;

    i = 0;

    while ((i < 101) && (j != 0) )
    {
        i++;
        printf("Set the value of the element %d => ", i);
        scanf("%d", &v[i]);
        printf("To stop press 0 => ");
        scanf("%d", &j);
    }
    k = 0;
    prim = 1;
    f = i;
    sum = 0;
    while (f > 2)
    {
        if (i % (f - 1) == 0)
        {
            prim = 0;
        }
        else
        {
            k++;
            sum = sum + v[f];
        }
        f = f - 1;
    }

    med = sum / k;
    printf("%d, %d, %d", k, soma, i);
    printf("The average is => %f \n", ave);
    system("pause");
}

對於那些想知道的人,這是我在正確答案中進行編輯后得到的結果:

int main(void)
{
    int v[101];
    int n = 0;
    int k,j = 0;
    int i=0;
    int sum = 0;

    while( i<100 )
    {
        i++;
        printf ("Set the value of the element %d => ", i);
        scanf ("%d", &v[i]);
        int x,primo=1;
        if (i>1){
          for (x=2; x*x<=i; x++) {
            if (i % x == 0) primo = 0;
          }
          if(primo==1)
          {
             sum = sum+ v[i];
             n++;
          }
        }
        printf ("To stop press 0 => ");
        scanf ("%d", &j);
        if(j == 0)
            break;
    }

    float ave =(sum /n);
    printf("%d, %d, %d", n,i,sum);
    printf("The average is => %f \n", ave);
    system("pause"); 
}

首先,讓一種可讀的方法來測試數字是否為質數。 來自另一篇SO帖子的答案為我們提供了一個很好的答案

int IsPrime(int number) {
  int i;
  for (i=2; i*i<=number; i++) {
    if (number % i == 0) return 0;
  }
  return 1;
}

其次,讓我們清理代碼,並計算到目前為止所遇到的所有質數的總和。 另外,我們將檢查scanf的返回值(但我們應避免scanf !)

第三,我們添加一些縮進。

int main(void)
{

    int n = 0;
    int i = 0;
    int j = 0;
    int k = 0;
    int sum = 0;

    while( i<101 )
    {
       i++;
       printf ("Set the value of the element %d => ", i);
       if(scanf ("%d", &k) != 1)
           continue;

       if(is_prime(k))
       {
        sum += k;
        ++n;
       }

       printf ("To stop press 0 => ");
       if(scanf ("%d", &j) == 1)
          if(j == 0)
              break;
    }

    float ave = sum / (double) n;
    printf("The average is => %f \n", ave);
    system("pause");
}

好吧,有幾句話要說。 首先是簡單的部分:如果允許讀取的最大整數為100,則變量“ v”應該為v[100] 這不是一個char數組,因此該數組不需要有一個額外的元素( v[100]將是一個從v[0]v[99]int數組;也可以調整循環限制)。

另外,您正在檢查變量f是否有質數,但是此var分配了變量i並且i不是數組的元素。 您想為f分配類似於v[i] (因為i等於0表示讀取的數字減一)。 因此,您將需要2個循環:您現在使用的一個循環用於檢查數字是否為質數,另一個循環將v[i]分配給f

要說的另一件事是,您兩次調用scanf進行讀取,您可以讀取數字並將其存儲在一個臨時變量中。 如果該數字不為零,則將其存儲在數組中並繼續讀取,否則停止讀取。

最后,我強烈建議您設置有意義的var名稱,僅對索引變量使用單個字母; 諸如temparraymaxcountnumbers應出現在您的代碼中。 您和其他所有人都可以更輕松地閱讀您的代碼,並且可以減少錯誤數量。

這是您的問題的解決方案。 很簡單的東西。

/* C program to find average of all prime numbers from the inputted array(you can predefine it if you like.) */        

#include <stdio.h>
#include <conio.h>
void main()
{
    int ar[100], i, n, j, counter;
    float avg = 0, numprime = 0;
    printf("Enter the size of the array ");
    scanf("%d", &n);
    printf("\n Now enter the elements of the array");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &ar[i]);
    }
    printf(" Array is -");
    for (i = 0; i < n; i++)
    {
        printf("\t %d", ar[i]);
    }
    printf("\n All the prime numbers in the array are -");
    for (i = 0; i < n; i++)
    {
        counter = 0;
        for (j = 2; j < ar[i]; j++)
        {
            if (ar[i] % j == 0)
            {
                counter = 1;
                break;
            }
        }
        if (counter == 0)
        {
            printf("\t %d", ar[i]);
            numprime += 1;
            avg += at[i];
        }
    }
    avg /= numprime;
    printf("Average of prime numbers is ℅f", avg);
    getch();
}

對於所有平均計算,您只需要如上所述的計數器變量即可。 (因為我們需要知道數組中素數的數量,因此我們可以將它們的總數相除,從而得出平均值。)不必擔心會向下進行類型轉換...此解決方案有效。 我自己寫的。

這是您想要做的事情的一項捷徑。 您不需要原始數量的變量。 同樣,在不知道您要如何處理質數的情況下,我只是在遇到質數時輸出。 同樣如前所述,使用函數檢查質數確實有幫助:

#include <stdio.h>
// #include <windows.h>

/* see: http://stackoverflow.com/questions/1538644/c-determine-if-a-number-is-prime */
int IsPrime(unsigned int number) {
    if (number <= 1) return 0; // zero and one are not prime
    unsigned int i;
    for (i=2; i*i<=number; i++) {
        if (number % i == 0) return 0;
    }
    return 1;
}

int main(void)
{
    int i, v[101], sum, pcnt=0, psum=0;
    float ave;

    i=0;

    printf ("\nEnter array values below, use [ctrl + d] to end input\n\n");
    printf ("Set the value of the element %d => ", i);
    while((i<101) && scanf ("%d", &v[i]) != EOF ){
        sum += v[i];
        if (IsPrime (v[i]))
            psum += v[i], pcnt++;
        i++;
        printf ("Set the value of the element %d => ", i);
    }

    ave=(float)psum/pcnt;

    printf("\n\n  Number of elements     : %d\n",i);
    printf("  The sum of the elements: %d\n",sum);
    printf("  The number of primes   : %d\n",pcnt);
    printf("  The average of primes  : %f\n\n", ave); 

    return 0;
}

樣本輸出:

Enter array values below, use [ctrl + d] to end input

Set the value of the element 0 => 10
Set the value of the element 1 => 20
Set the value of the element 2 => 30
Set the value of the element 3 => 40
Set the value of the element 4 => 51
Set the value of the element 5 => 11
Set the value of the element 6 => 37
Set the value of the element 7 =>

Number of elements     : 7
The sum of the elements: 199
The number of primes   : 2
The average of primes  : 24.000000

暫無
暫無

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

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