Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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]);
   }
}

}
Posted
Updated 1-May-18 22:11pm
v2

Quote:
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.

C++
prime_num(num,a[10]);

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:
C++
prime_num(num,a);


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.
 
Share this answer
 
v2
Since you are asking the user for the size of the array, you have to dynamically allocate/deallocate memory for it. Try
C
#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]);
    }
  }
}
 
Share this answer
 
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]);
   }
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?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900