Link to home
Start Free TrialLog in
Avatar of amlegacy

asked on

Passing functions using arrays


I need help creating a function using arrays and passing it to main. I've attached what I know so far:

// 12.1.cpp : Defines the entry point for the console application.
//Modified program to generate a table of prime numbers

#include "stdafx.h"

int my_prime (int prime []);

int main ()
      int ans;
      int result = my_prime(ans);


      return 0;

int my_prime (int p [])
      int p, is_prime, i, primes[50], prime_index = 2;

      primes[0] = 2;
      primes[1] = 3;

      for      (p=5; p<=50; p=p+2)

      { is_prime =1; //equal to primes[1]=3

            for (i=1; is_prime && (p/primes[i] >= primes[i]); ++i)

                  if ( p % primes[i] == 0) //if no remainder do this
                        is_prime = 0; //setting is_prime equal to primes[0]=2

                  if (is_prime) //if expression is true
                              primes[prime_index] = p;

Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have not idea why this topic should be deleted. I wrote an answer and well I'd expect at least some feedback about it.

Avatar of amlegacy


I'm sorry I didn't understand the answer. I thought it would be best for me to re-word the question.
Your solution was good because I wasn't specific about my question. You took the time to answer the question. I'm sorry that I didn't get back to you sooner.
What is unclear about the answer?
I suggested you wrote another function which tests a number on primeality.
Then you run a loop to fill the primes array.

and I wrote that the comments about the use if is_prime doe not mean what the code does.

I'm  sorry. It's all  my fault. I'm new to programming.
That does not help, if something is unclear ask. How should we know what "bothers" you if you don't tell us? I just suggested to divide the work, and not mix up array filling and prime tests in some "blob". So write a function which does nothing else but checking on primes, test it then if you are confident enough you can do whatever you like with it....

Programming is no black magic, it's just writing such that a machine can understand and translate it an then it makes something usefull....