Tatention
Tatention

Reputation: 7

Getting an error initializing an my array

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

Upvotes: 0

Views: 53

Answers (2)

Harry
Harry

Reputation: 11688

There were a few errors in it. Try this. It compiles and runs. Not sure if output is what you want though...

First issue was this

for(int i = 0; i < 25; i++){
    double array[i] = array[0] * array[0];
}

The C++ compiler sees this as a new variable length array which it does not support. You did this a few times. Second problem was the test expression in this for loop...

for(int i = 0; 25 < i && i <50; i++){
  double array[i] =  array[25] * 3;
}

It would never test true because int i = 0; means 25 < i is always false.

#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++){
    array[i] = array[0] * array[0];
  }
  for(int i = 25; i < 50; i++){
    array[i] =  array[25] * 3;
  }
  for (int i = 0; i < 50; i++) {
    cout << array[i] << " ";
    if ((i + 1) % 10 == 0) {
      cout << endl;
    }   
  }
  return 0;
}

Upvotes: 1

paddy
paddy

Reputation: 63481

You have used array declarations everywhere by specifying double array[i]. This is not correct. Just use array[i]. i.e.

for(int i = 0; i < 25; i++) {
   array[i] = array[0] * array[0];
}

for(int i = 0; 25 < i && i <50; i++) {
   array[i] =  array[25] * 3;
}

for (int i = 0; i < 50;) {
    cout << array[i] << " ";
}

Note your second loop is bogus. It will never run. Just change it to:

for(int i = 25; i < 50; i++) {
   array[i] =  array[24] * 3;
}

And your first loop is going to square array[0] and store it in array[0], so that array[1] will be the original number to the power of 4. If that wasn't your intention, start iterating at i = 1, not i = 0.

Upvotes: 2

Related Questions