0

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

2
  • double array[i] declares a variable called array. To refer to variables that have already been declared, you use their name, array ; you don't repeat bits of their declaration. Commented Apr 12, 2016 at 23:42
  • Thanks i think i figured it out! Commented Apr 13, 2016 at 0:14

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i had it like that first but got an initialization error and got scared and changed them all because it gave me less errors. Thanks for the explanation. and by fixing the second loop like you did, you just eliminated the need for the and statement by just pushing forward the parameters correct?
Well yeah, not to mention make the loop actually run. The way you had it, i begins at 0, and the loop condition fails because 25 is not less than 0. You obviously wanted the loop to begin at 25, and end when reaching 50.
1

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;
}

1 Comment

Could use an explanation of what you changed and why. Code only answers lead to Cargo Cultists.

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.