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.
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.