需要帮助将值存储到数组中 [英] Need help in storing values into an array

查看:52
本文介绍了需要帮助将值存储到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印给定范围内的所有素数。所以,我打算把所有素数放到一个数组中,然后用它来显示。

这是我的尝试。



我尝试过:



I'm trying to print all the prime numbers from a given range. So, I'm planning to put all the prime numbers into an array and then use it to display.
Here's my try at it.

What I have tried:

#include<stdio.h>
#define size 100
int main()
{
        int n,i,j,m=0,a[size],p,count;
        printf("Enter a number upto which u want to pick out prime numbers: ");
        scanf("%d",&n);
        m = 0;
        for(i=1;i<=n;i++){
                for(j=1;j<=i;j++){
                        if(i%j == 0){
                                count++;
                                if (count == 2){
                                        a[m++] = i;
                                        p = m;
                                        }
                        }
                }
        }
        for(m=0;m<p;m++)
                printf("%d\n",a[m]);
}





但是我得到了很多垃圾值。您认为我的逻辑错在哪里?



But I'm getting so much of garbage values as result. Where do you think my logic went wrong?

推荐答案

嗯......您只在中存储任何值如果count等于2.那么这将发生一次,只发生一次。



我建议你做的是编写代码填写 a ,所有数字介于1和n之间(这是一个简单的 for 循环),然后编写代码以从中删除多个(所以你删除2 * 2 == 4,2 * 3 == 6,...然后3 * 2 == 6,3 * 3 == 9,...)

最简单的方法删除它们是为了使它们为负数:任何负数都不是素数,所以你不需要删除它的倍数。



当你这样做时,快速循环打印出所有正数很简单。
Well ... you only store any values in a if count is equal to 2. So that'll happen once, and once only.

What I'd suggest you do is write code to fill a with all the numbers between 1 and n (which is a simple for loop) and then write code to remove multiples from that (so you remove 2 * 2 == 4, 2 * 3 == 6,... then 3 * 2 == 6, 3 * 3 == 9, ...)
The simplest way to remove them is to make them negative: any negative number is not prime, so you don't need to remove its multiples.

When you've done that, a quick loop through printing out all the positive numbers is simple.


你的素数测试对我没有锁定。

无论如何我会把它放进去一个单独的函数,以使代码更整洁。例如

Your primality test doesn't lock correct to me.
In any case I would put it in a separate function in order to make the code tidier. e.g.
#include <stdio.h>

#define SIZE 100

int is_prime(int n);

int main()
{
  int limit;
  int prime[SIZE];
  int n;
  int count = 0;

  do
  {
     printf("Enter a number upto which u want to pick out prime numbers: \n");
  } while( scanf("%d", &limit ) < 1);


  for (n=2; n<limit && count < SIZE; ++n)
  {
    if ( is_prime(n) )
    {
      prime[count] = n;
      ++count;
    }
  }

  for (n=0; n<count; ++n)
    printf("%d\n", prime[n]);

  return 0;
}

int is_prime(int n)
{
  int i;
  for (i=2; i<=n/2; ++i)
    if ( (n % i) == 0 )
      return 0;

  return 1;
}


您的代码存在一些问题:



1。您将素数的数量限制为100( #define size 100 ),但您永远不会检查数组是否溢出。当它发生时,会发生各种不可预测的(和讨厌的!)事情。

2.你正在使用一种极其低效的质数测试方法。

2a。 [Think]在N%M中,为了确保N是素数,需要测试的最大M是多少?

2b。 [Think]如何使用已收集的信息来帮助计算更多素数?
There are a few problems with your code:

1. You limit the number of primes to 100 (#define size 100), but you never check whether your array has overflowed. When it does, all kinds of unpredictable (and nasty!) things will happen.
2. You are using an extremely inefficient way of testing for primes.
2a. [Think] In N % M, what is the largest M that needs to be tested in order to ensure that N is prime?
2b. [Think] How can you use the information already collected in order to help calculate more primes?


这篇关于需要帮助将值存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆