C程序在给定数组中查找素数及其位置 [英] C program to find a prime number and its position in a given array

查看:126
本文介绍了C程序在给定数组中查找素数及其位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是模块化的c程序,用于查找素数并在给定数组中显示其位置

i我试图通过定义函数来制作此程序,但没有编译时错误但是这个程序当我运行这个程序时崩溃

伙计们请帮助我我是新手编程



我尝试了什么:



this is the modular c program to find a prime a number and display its position in a given array
i am trying to make this program by defining a function there is no compile time error but this program is crashing when i am running this program
Guys please help me i am new to programming

What I have tried:

#include<stdio.h>
void prime_num(int num,int a[10]);
int main()
{
   int a[10],i,num;
    printf("enter the size of array");
      scanf("%d",&num);
    printf("enter the elements");
         
          for(i=0;i<num;i++)
              {
                 scanf("%d",&a[i]);
                    }
         printf("the entered array is ");
              for(i=0;i<num;i++)
           {
              printf("%d",a[i]);
                }

               prime_num(num,a[10]); //function called here

   }  //end of main function
void prime_num(int num,int a[10])
{
    int i,counter,j;
printf("all the prime numbers are");
for(i=0;i<=num;i++)
{
    counter=0;
    for(j=2;j<a[i];j++)
    {
        if((a[i]%j)==0)
        {
            counter=1;
            break;
        }


    }
    if(counter==0)
   {

    printf("the found prime no is at %d position and the number is %d",i,a[i]);
   }
}

}

推荐答案

引用:

没有编译时错误但是当我运行这个程序时这个程序崩溃了

there is no compile time error but this program is crashing when i am running this program



什么是错误信息,错误的位置,你的输入是什么?

你没有给我们的所有信息。




What is the error message, position of error, what is your input ?
All information you didn't gave us.

prime_num(num,a[10]);



这里, a [10 ] a 结束后的地址,因为大小为10,元素为0到9.

正确的代码应该是这样的:


Here, a[10] is the address of something after the end of a because the size is 10 and elements are from 0 to 9.
Correct code should be like:

prime_num(num,a);





作为程序员,你的工作也是修改你的代码,调试器是你选择的工具,你应该学会尽快使用它。

-----

你的代码没有你想象的那样,你不明白为什么!



T这是一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。



As programmer, your job is also to fix your code, the debugger is the tool of choice, you should learn to use it as soon as possible.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


由于您要求用户提供数组大小,因此必须为其动态分配/释放内存。尝试

Since you are asking the user for the size of the array, you have to dynamically allocate/deallocate memory for it. Try
#include <stdio.h>
#include <stdlib.h>

void prime_num(int size, int a[]);

int main()
{
  int * a, i, size;

  printf("enter the size of array\n");
  scanf("%d",&size);
  printf("enter the elements\n");


  //-> dynamic memory allocation
  a = malloc( size * sizeof (int) );

  if (! a )
  {
    printf("unable to allocate memory\n");
    return -1;
  }
  //<- 
         
  for(i=0;i<size;i++)
  { 
    scanf("%d",&a[i]);
  }
                    
  printf("the entered array is\n");

  for(i=0;i<size;i++)
  { 
    printf("%d\n",a[i]);
  }

  prime_num(size, a); //function called here


  free( a ); // dynamic memory deallocation

  return 0;

}  //end of main function

void prime_num(int size,int a[])
{
  int i, j;
  printf("all the prime numbers are\n");
  for(i=0;i<size;i++)
  { 
    for(j=2;j<a[i];j++)
    { 
      if((a[i]%j)==0)
        break;
    }
    if(j == a[i])
    { 
      printf("the found prime no is at %d position and the number is %d\n",i,a[i]);
    }
  }
}


首先整理缩进,以便您的代码更易于阅读和获取摆脱多余的评论(这是所有这些)。注释代码在做什么,而不是实际的指令是什么,我们可以阅读那些...

Start by sorting out your indentation, so your code is easier to read, and getting rid of redundant comments (which is all of them). Comment what the code is doing, not what the actual instructions are, we can read those...
int main()
{
   int a[10],i,num;
   printf("enter the size of array");
   scanf("%d",&num);
   printf("enter the elements");
   for(i=0;i<num;i++)
   {
       scanf("%d",&a[i]);
   }
   printf("the entered array is ");
   for(i=0;i<num;i++)
       {
       printf("%d",a[i]);
       }
   prime_num(num,a[10]);
   }

然后,开始计算你的代码有什么问题。

首先看看你输入的是什么:<$ c $的价值是多少c> num 例如?如果它大于10,则表示您遇到问题,因为您只允许输入10个值。您应该检查一下,并拒绝不适合的值(因为如果不这样,当您的用户输入错误值时,您的应用会崩溃)。



接下来要做的是查看你的 prime_num 函数。你究竟要传递给它的是什么?它是一个数组还是一个元素?



提示:如果你把它放在一个数组后面是什么呢,特别是十个元素的数组?

Then, start working out what is wrong with your code.
Start by looking at what you are entering: what is the value of num for example? If it's bigger than 10, you have a problem, because you only allowed for 10 values to be entered. You should check that, and reject values which won't fit (because if you don't, your app will crash when your user enters a "bad" value).

The next thing to do is to look at your prime_num function. What exactly are you passing to it? Is it an array, or a single element?

Hint: What does [10] do if you put it after an array, specifically an array of ten elements?


这篇关于C程序在给定数组中查找素数及其位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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