LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-07-2014, 02:09 AM   #1
pipideni
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Rep: Reputation: Disabled
I have to write a function that returns the nth prime number in C. But getting error!


I might have mistakes in my code, any help would be welcomed.
But compiling error is expected declaration or statement at end of input!!! Cant find out why.
I would be thankful so much for any help.
#include <stdio.h>
int isPrime(int number);
int prime(int n);

int main (void)
{
int n;
printf("Enter the ordinal number of prime number you want to print out.\n", n);
scanf("%d", &n);
int result;
result=prime(n);
printf("(%d)PRIME is: %d \n", n, result);

return 0;
}
int isPrime(int number)
{

int counter=2;
if (number <= 1)
return 0; // zero and one are not prime

for (counter=2; counter<=number/number-1; counter++) {
if (number % counter == 0) return 0;
}
return 1;
int prime (int n)
{
int orderOfPrime=0;
int currentPrimeNumber;
int counter;
for(counter=0; counter<currentPrimeNumber; counter++);
{
if(isPrime)
{
printf("Exit the loop");
}

else
{
printf("Continue with next step");
}

}

return currentPrimeNumber;
}
 
Old 11-07-2014, 02:56 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,779

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
please use [code]here comes your code[/code] to keep formatting.
What kind of error did you get? Can you post it?
 
Old 11-07-2014, 03:06 AM   #3
pipideni
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
I got the "Expected declaration or statement at the end of input" in fry croon isPrime
 
Old 11-07-2014, 03:08 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,779

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
and again, please edit your original post and insert [code] and [/code].
Probably there is no closing } at the end of the function isPrime().
 
Old 11-07-2014, 03:10 AM   #5
pipideni
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Okay I will sorry. No I have checked that, there must be smth else.
 
Old 11-07-2014, 03:13 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,506
Blog Entries: 6

Rep: Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421
Quote:
Originally Posted by pipideni View Post
Okay I will sorry. No I have checked that, there must be smth else.
Perhaps you should check it again...
 
Old 11-07-2014, 03:16 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
As displayed, there is definitely a missing closing brace for isPrime ... so better check again or you have pasted incorrectly into the first post above.
 
Old 11-07-2014, 07:21 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
What did you intend the following code to do?


Quote:
Originally Posted by pipideni View Post
for (counter=2; counter<=number/number-1; counter++) {
number/number is 1.
 
Old 11-07-2014, 08:47 AM   #9
c0d3d
Member
 
Registered: Aug 2012
Posts: 74

Rep: Reputation: 12
I figure I should tell you that LinuxQuestions has its on Programming forum, and posting these kinds of questions there might get you better responses.
 
Old 11-07-2014, 08:58 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,838
Blog Entries: 12

Rep: Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010
Firstly the function isPrime() does not have a closing bracket.

That will get it to "compile".

Next if you use -Wall as part of the compilation, you get the following warnings/errors:
Code:
gcc -o prime -Wall prime.c
prime.c: In function ‘main’:
prime.c:8:5: warning: too many arguments for format [-Wformat-extra-args]
prime.c: In function ‘prime’:
prime.c:37:12: warning: the address of ‘isPrime’ will always evaluate as ‘true’ [-Waddress]
prime.c:32:9: warning: unused variable ‘orderOfPrime’ [-Wunused-variable]
prime.c:35:5: warning: ‘currentPrimeNumber’ is used uninitialized in this function [-Wuninitialized]
The warning for line 8 is because you have the argument n for your printf when you have no arguments in the format string such as %d.

The warning for line 37 is because you've not "called" the function isPrime so the compiler interprets this as the address of the function isPrime for lack of any better choices. The correct thing to do here is to recode that line to be:
Code:
if(isPrime(counter) == 1)
However that function appears to be ... weird. You passed a variable to it and you don't seem to use it. You don't initialize currentPrimeNumber, but you use it as a loop limit; so that can be and will be therefore arbitrary, probably some odd/very large indeterminate number. I guess instead of using counter for the isPrime() call your intentions would be to use the function argument of n.

The warning for line 32 is because you never used a declared local variable.

The warning for line 35 is exactly what I've covered above.

A suggestion is that you fix the closing bracket issue for the isPrime() function, fix these warnings, and look more closely at your code to determine if it's really doing what you intend it to do.

Another concern by the way is your for loop here:
Code:
    for (counter=2; counter<=number/number-1; counter++) {
C language precedence states that * and / are ABOVE + and - in precedence. That is to say that "number/number" will ALWAYS be evaluated first. Therefore the result of that will ALWAYS be ONE leaving the next evaluation to be "number/number-1" --> "1-1" --> "0". Ergo, the loop will NEVER execute.

Last edited by rtmistler; 11-07-2014 at 08:59 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i am using write() function to send string to my driver . it returns EFAULT. dpattnaik Linux - Newbie 0 06-22-2009 03:39 AM
Write a function that takes an input argument a file and returns the date to stdout 1subject Programming 1 06-17-2009 12:17 PM
Nth-order derivative of an arbitrary function ta0kira Programming 2 01-01-2009 02:07 AM
function to find the closest prime number... sachitha Programming 2 10-20-2006 03:47 PM
open function returns error sudheernair Programming 2 05-07-2004 12:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration