tags:

views:

258

answers:

3

Basically, a program to find the largest prime factor of a number. Don't know if the logic is correct cuz I can't run it to test it. I know this is long. But please forgive me.

//largest_prime.c
#include <stdio.h>
int main() {
    int num,large;
    int prime(int);
    int lprime(int);
    printf("Enter number: "); 
    scanf("%d",&num);
    large = lprime(num);
    printf("Largest Prime = %d\n",large);
    return 0;
}

int lprime(int num) {
    int i=num-1,large;
    while (i!=-1) {
        if (num%i==0) {
            if (prime(i)==1) {
                large=i;
                i=-1;
            }
            i--;
        }
        return large;
    }

    int prime(int num) {
        int prime,i=num-1;
        while (i!=-1) {
            if(num%i==0) {
                if(i!=1) {
                    i=-1;
                    return 0;
                }
                else
                    return 1;
            }
            i--;
        }
    }

I get this as output at terminal (I use Mac) using GCC:

largest_prime.c: In function ‘lprime’:
largest_prime.c:26: error: nested functions are disabled, use -fnested-functions to re-enable
largest_prime.c:39: error: syntax error at end of input

I tried -fnested-functions to re-enable. I get:

largest_prime.c: In function ‘lprime’:
largest_prime.c:39: error: syntax error at end of input

Please Help!

+10  A: 

lprime is missing trailing '}' (in fact i believe you lost while's trailing '}')

Drakosha
+6  A: 

your are missing a } after return large;

the compiler deduced you are beginning a new function declaration inside lprime hence the nested function error

Alon
+3  A: 

Functions cannot be nested in ISO C; enabling some compiler extension to allow then is not the solution; just make you code valid C. Your indentation style is not helping you here; but the fact that the last line of each function is not at the left margin should ring alarm bells. The following compiles but not without warnings (added lines marked):

//largest_prime.c
#include <stdio.h>

// Forward declarations
int lprime(int num) ;  //********************
int prime(int num) ;   //********************

int main() {
    int num,large;
    printf("Enter number: "); 
    scanf("%d",&num);
    large = lprime(num);
    printf("Largest Prime = %d\n",large);
    return 0;
}

int lprime(int num) {
    int i=num-1,large;
    while (i!=-1) {
        if (num%i==0) {
            if (prime(i)==1) {
                large=i;
                i=-1;
            }
            i--;
        }              //********************
    }
    return large;
}

int prime(int num) {
    int prime,i=num-1;
    while (i!=-1) {
        if(num%i==0) {
            if(i!=1) {
                i=-1;
                return 0;
            }
            else
                return 1;
        }
        i--;
    }
}

The warnings are:

main.cpp(34) : warning C4101: 'prime' : unreferenced local variable
main.cpp(46) : warning C4715: 'prime' : not all control paths return a value

I have not executed the code, so whether it works correctly is for you to determine.

Clifford
Thanks for the edit; I did not notice those declarations in the "unconventional" location. I added the previous declarations because I thought they were missing altogether. The lesson being; don't write 'unusual' code. And this is unusual in many ways!
Clifford