here is my code, Its a simple code meant to initialize an array for 50 slots filling them with zero. Then ask the user for one number for the position array[0]. it then copies that number to the first 25 slots and squares it. then it takes the same number and multiplies it by three and places it in the last 25 slots. the program then prints the array making a new line every 10 elements.
I run into an error 13:39: error: array must be initialized with a brace-enclosed initializer 16:34: error: array must be initialized with a brace-enclosed initializer 19:11: error: expected primary-expression before 'double' 7:9: warning: unused variable 'i' [-Wunused-variable]
#include<iostream>
using namespace std;
int main()
{
double array[50] = { 0 };
double i;
cout << "Type in your index nummber" << endl;
cin >> array[0];
for(int i = 0; i < 25; i++){
double array[i] = array[0] * array[0];
}
for(int i = 0; 25 < i && i <50; i++){
double array[i] = array[25] * 3;
}
for (int i = 0; i < 50;) {
cout << double array[i] << " ";
if ((i + 1) % 10 == 0) {
cout << endl;
}
}
}
Fixed code below
#include<iostream>
using namespace std;
int main()
{
double array[50] = { 0 };
double i;
cout << "Type in your index number" << endl;
cin >> array[0];
for(int i = 1; i < 25; i++) {
array[i] = array[0] * array[0];
}
for(int i = 25; i < 50; i++) {
array[i] = array[24] * 3;
}
for (int i = 0; i < 50; i++) {
cout << array[i] << " ";
if ( (i+1) % 10==0){
cout << endl;
}
}
return 0;
}
Results:
Type in your index number
4
4 16 16 16 16 16 16 16 16 16
16 16 16 16 16 16 16 16 16 16
16 16 16 16 16 48 48 48 48 48
48 48 48 48 48 48 48 48 48 48
48 48 48 48 48 48 48 48 48 48
double array[i]declares a variable calledarray. To refer to variables that have already been declared, you use their name,array; you don't repeat bits of their declaration.