0

I'm trying which elements in an array(also the Fibonacci numbers) are prime or composite number.

I am given array[1...N] of integer. I am supposed to write algoritm that returns information that if in array: -All elements, which index is one of the Fibonacci numbers are composite numbers. -At least one number of the rest index is a prime number. I get an error at int main(): cannot convert '' to 'int' in assignment and: too many initializer values. My code is as follows:

#include <iostream>
#include <cstdlib>
using namespace std;
const int N = 5;
void function1(int tab[]);
bool function2(int tab[], int);
int main() {

    int tab[N], i;
    tab[N] = { 4, 8, 12, 16, 13 };
    function1(tab);
    system("pause");
    return 0;
}

void function1(int tab[]) {
    int a, b, x, i;
    bool g = 1;
    for ( i = 0; i < N, g == 1; i++) {
        a = 0;
        b = 1;
        while (i < a && i < b) {
            a = a + b;
            b = a + b;
        }
        if (i == b || i == a) {
            x = function2(tab, i);
        }
        if (!x) {
            cout << "NO";
            g = 0;
        }
    }
    int licz_pier = 0;
    i = 0;
    if (g) {
        while (i < N && licz_pier == 0) {
            if (tab[i] != 0) {
                if (function2(tab, i)) {
                    i++;
                }
                else {
                    licz_pier = 1;
                    cout << "yes" << endl;
                }
            }
        }
    }

}

bool function2(int tab[], int i) {
    int k = 2;
    bool x = 1;
    while (k < sqrt(tab[i]) && x == 1) {
        if (tab[i] % k == 0) {
            x = 0;
            tab[i] = 0;
                return 1;
        }
        k = k + 1;
    }

    if (x) {
        return 0;
    }
    return 0;
}

ps. I am beginner, this is my first post, sorry for my English.

5
  • 1
    And what is your problem with your code? What is the expected and actual output from some input? You might want to read the Stack Overflow question checklist. Commented Dec 27, 2013 at 21:04
  • try putting the initialization of the tab array inline with the declaration. Commented Dec 27, 2013 at 21:05
  • 1
    The title of the question doesn't really hint about what the problem is, it should be a one-sentence summary of your actual problem. Also, when posting question that is about compiler errors, you should include the actual (complete and unedited) error log in the question. Commented Dec 27, 2013 at 21:07
  • Ok, sorry. Thanks for information! Commented Dec 27, 2013 at 21:30
  • 1
    If you have an issue, you should remove parts of the code not applicable to the issue (but make sure to compile what you end up with to make sure it has the same issue). For this particular issue, you should probably end up with a program of about 10 lines. Also, one issue per question - I see you commented on the answer that it still doesn't work - you should accept the answer if it solved this issue you're asking about, and post a new question if you can't sort out the new issue - but be sure to include expected and actual output, though ideally you should debug to find the issue yourself. Commented Dec 27, 2013 at 22:29

1 Answer 1

3

The statement

tab[N] = { 4, 8, 12, 16, 13 };

is not a legal one. You need to put the initialization in the definition:

int tab[N] = { 4, 8, 12, 16, 13 };

Or manually initialize the array after the declaration, one by one:

tab[0] = 4;
tab[1] = 8;
tab[2] = 12;
tab[3] = 16;
tab[4] = 13;
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, thank you! But still doesn't work, maybe because of wrong arguments of function1 and function2? Or, algorithm but I'll check again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.