[英]Can't initialize a C++ array
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. 给我整数[1 ... N]。 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. 我在int main()处收到错误:无法在赋值中将''转换为'int'并且:初始化器值太多。 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. 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.