Thursday, July 30, 2009

More Programming Help?

I'm writing this program for a class and I'm suppose to create a program that will print out all of the prime numbers for a given number. The program is suppose to be written in three seperate files. I've written some of it but now I'm stuck. I can't seem to get the program to recognize the number I input and now I'm getting some errors that I can't figure out how to fix. Can someone please help?





Here is the program:





These files are in primes.h





#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;





int is_prime(int n);








These files are in is_primes.c








#include %26lt;stdio.h%26gt;





void prn_banner(void);


void read_data(void);


prn_prime_numbers();





int main(void)


{


prn_banner();


read_data();


prn_prime_numbers();





}


void prn_banner(void)


{


printf("\n%s%s%s\n",


"**************************\n",


"* PRIMES WILL BE PRINTED *\n",


"**************************\n");


}


void read_data(void)


{


int limit = 1000, n;


printf("\n Please enter a number \n");


scanf(" %d", %26amp;n);


}


prn_prime_numbers()


{


#include "is_prime.c"


return 0;


}











These files are in main.c





#include "primes.h"





int is_prime(int n)


{


intk, limit;





if (n == 2)


return 1;


if (n % 2 == 0)


return 0;


limit = n / 2;


for (k = 3; k %26lt;= limit; k +=2)


if (n % k == 0)


return 0;


return 1;


}








I’m getting the following errors:








Warning 1warning C4996: 'scanf' was declared deprecatedfile: main.c Line 7





Error2error C2143: syntax error : missing ';' before '{'file is_prime.c Line 4





Error3error C2065: 'n' : undeclared identifierfile is_prime.c Line

More Programming Help?
Firstly, I have noticed you do not have a return type for the function for printing the prime numbers in the is_primes.c file, "prn_prime_numbers();". Change this to "void prn_prime_numbers();"





Secondly, this function "prn_prime_numbers()", has the statement '#include "is_prime.c"' inside of it. This is an error in itself.





Thirdly, in the function, "void read_data(void):", you have the line, "int limit = 1000, n;". If you


want to initialise 'limit' to 1000, then you should have written the line as "int n, limit = 1000;". You were not giving 'n' a value, so it could not be on the same line after 'limit'.





Fourth, this involves some steps.


- You will want to remove the call to the function "prn_prime_numbers();" from "main(void)".


- You will want to change the parameter calls to this function "prn_prime_numbers();" to be "prn_prime_numbers(int n);"


- Now make a call to this function, "prn_prime_numbers(int n);" from the "void read_data(void):" function.


- In the function "prn_prime_numbers(int n);" you will want to call to "int is_prime(int n)" that is in the "main.c" file. You call this by writing "int r = is_prime(n);" 'n' is there as it was passed in as the parameter. Then check if 'r' is equal to 1, if so print out the number 'n', as it is prime.





Fifth, it appears you have either made a typing error when typing in, or you have mistakenly set the wrong names to the files.


- The first file you list is 'primes.h', this name is good.


- The second file you list, has the main method and should be called 'main.c'.


- The third file you list has the 'int is_prime(int n);' function from 'primes.h', so it should be named 'primes.c'.





Sixth, your '#include' statements are just a bit messy. Apart from what I mentioned before, in the file with the main function, you need to have '#include "primes.h"' or '#include %26lt;primes.h%26gt;', this will enable you to call the "int is_prime(int n)" function.





As to the errors, deprecated means the function is no longer used, but this is only a warning, so you can still use it.


Apart from placing a space between 'int' and 'k' in the "int is_prime(int n)" function, I can not see anything else. Try to compile this. If you still get errors that you cannot work out, come back and add some additional details, and we may be able to help more.
Reply:What did you mean for this line to do?


#include "is_prime.c"





it's causing most of your problems.


Try removing it, see how much it helps...





Also, regarding your algorithm, it may be a better idea to remember (ion a global array, perhaps) the primes you have generated so far - then, to check if the next number is a prime, you'd also need to try to divide it by those primes you already know, instead of checking all the odd numbers every time.


Also, the limit of (int) sqrt(n) is enough. There is no reason in going all the way up to n/2
Reply:fix it

flower girl

No comments:

Post a Comment