# Arrays and Functions, prime numbers

I have been given code to write and run a program of prime numbers up to 50. This is my first introduction to arrays. I'm supposed to write a function and pass it through main. Only problem is; I have no idea how to pass an array to main. I haven't been taught how to do it and I really need help. So here is the problem:

main ()

{

int rating_counter [11]; //11 element array
int i, response;

for (i=1; i<=10; ++i)
rating_counters[i] = 0;

for (i=1; i<=20; ++i)

{

scanf ("%d", &responses);
++rating_counters[response];

}

printf ("\n\nRating           Number of Responses\n");
printf ("------------           --------------------------------\n");

for (i=1; i<=10; ++i)
printf ("%4d                         %d\n", i, rating_counters[i]);

}
Infinity08

membership
Create an account to see this answer
Signing up is free. No credit card required.
amlegacy

I did post the wrong code but that's not the problem. It's passing an array. Do you need to but a value in:
void fun(int [] array, int len)?

like void fun(int [50] array, int 50)
>> Do you need to but a value in:

No. They are function parameters. The values are put in when you call the function (see my example code).

Do you know about functions and passing arguments ? I suggest this nice tutorial :

http://www.cplusplus.com/doc/tutorial/functions.html
http://www.cplusplus.com/doc/tutorial/functions2.html

Ok. I think I get it now. I'm going to try it and see what happens.

Here is what I have so far. I'm getting some errors.

#include <iostream>

void my_prime (int [] prime, int element);

int main (void)

{
int ans[50] = {0};
my_prime (ans, 50);

return 0;

}

void my_prime (int a [])
{
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;
++prime_index;

}
}
}

I left out some code.

#include <iostream>

void my_prime (int [] prime, int element);

int main (void)

{
int ans[50] = {0};
ans = my_prime (ans, 50);

return 0;

}

void my_prime (int a [])
{
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;
++prime_index;

}
}

for (i=0; i < prime_index; ++i)
printf("%d   ", primes[i];

printf ("\n");
}

Ok, I figured it out. I got it to work. Thanks for your help.

>> Ok, I figured it out. I got it to work.

Great :) Sorry for my absence.

In any case, the problem was that your function implementation :

void my_prime (int a [])

did not match the prototype :

void my_prime (int [] prime, int element);

They have to specify the same parameters.

Furthermore, instead of creating a new primes array inside the my_prime function, you can simply use the array from the argument (a). Any changes you do to that array will be visible in main.
Maybe you can think a bit reading my vision on the implementation...
``````#include <stdio.h>

void my_prime(
int* prime_out /* the array that will be filled with prime numbers */,
int prime_out_len /* the lenght of the allocated memory for prime_out */,
int max_prime /* the maximum number for searching primes; e.g. 50 */,
int* prime_count /* the number of primes returned in the array */
);

int main(void)

{
int ans[50] = { 0 };
int prime_cnt = 0;
int i = 0;

my_prime(ans, 50 /* array size */, 50 /* max number to search */, &prime_cnt);

for (i = 0; i < prime_cnt; i++) {
printf("%d ", ans[i]);
}

printf("\n");

return 0;
}

void my_prime(
int* prime_out /* the array that will be filled with prime numbers */,
int prime_out_len /* the lenght of the allocated memory for prime_out */,
int max_prime /* the maximum number for searching primes; e.g. 50 */,
int* prime_count /* the number of primes returned in the array */
)
{
int p, is_prime, i;

/* always check sizes... */
if (prime_out_len < 2) return;

/* first 2 primes... */
prime_out[0] = 2;
prime_out[1] = 3;
*prime_count = 2;

for (p = 5; p <= max_prime && *prime_count < prime_out_len; p = p + 2) {
/* in the current loop p is checked if it is prime */
/* p is prime if it can't be divided by any previous prime */
is_prime = 1; /* assume that p is prime */

i = 0; /* i is index in prime_out */
while (is_prime && i < *prime_count && (p / prime_out[i] >= prime_out[i])) {
is_prime = (p % prime_out[i] != 0);
i++;
}

if (is_prime) {
prime_out[*prime_count] = p;
(*prime_count)++;
}
}
}``````
florin_ganea, we don't solve assignments here (that's against the rules). We do guide people in solving their assignments themselves.

Posting complete code for assignments is not allowed. And in any case, it's my opinion that you overcomplicated things quite a bit.

Florin_ganea Wow! That's some code. I will definitely take the time to analyze your solution. Thank you.

Infinity,

What is the difference between the array and its elements?
>> What is the difference between the array and its elements?

I'm not sure what you mean, but, an array is a series of elements. For example, an array of 10 ints contains 10 elements (which are ints).