### Wierd Output when using arrays

For a college homework assignment, I was asked to
write an array called "alpha" composed of 50 components type double.
The first 25 have to be the square of the index, while the last 25 have to be 3 times the index, and it was to be a 2-D array with 10 numbers a line. However, my output seems to be weird.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double alpha[5][10] = {{1*1,2*2,3*3,4*4,5*5,6*6,7*7,8*8,9*9,10*10},
{11*11, 12*12, 13*13, 14*14, 15*15, 16*16,17*17,18*18,19*19,20*20},
{21*21, 22*22, 23*23, 24*24, 25*25, 26*3, 27*3, 28*3, 29*3, 30*3},
{31*3, 32*3,33*3, 34*3, 35*3, 36*3, 37*3, 38*3, 39*3, 40*3},
{41*3, 42*3, 43*3, 44*3, 45*3, 46*3, 47*3, 48*4, 49*3, 50*3}};

cout << alpha << endl;
}

The output comes out to be : "00D2F878"
Can someone help me out?
The variable alpha is an array, when you try to print like you have, you get the memory address.

To print out the entire array - you need nested for loops, that will print each element individually.

Always use code tags - select the code then press the <> button on the right.

Hope all goes well.
Indices begin at 0.

You require two indices to access an element of a 2D array, so your requirements don't make much sense.

You're outputting the address of the array.
Last edited on
You should also use the for loops to initialise the array, not hard code the values like that. Use the program to do work for you, not you do all the work yourself. This is the whole idea of programming.
Wow, thanks for the insanely fast reply. I had taken your advice on letting the program do the work, and I've come up with an idea. However we'll executing it, intelisense gives me the error "must be integral or enum type" where the "i" values are.. Keep in mind this isn't the whole program, I was just testing out a few values.

 ``123456789101112131415`` ``````#include #include using namespace std; int main() { double alpha[50]; double i; for(i = 0; i <= 9 ; i++) { cout << alpha [i] * alpha[i] << " "; cout << endl; }``````
Like your compiler says, the type of i should be integral.

Which element do you think alpha[4.33] should access?
It looks like in your second example you're trying to enter in the index squared for the value? The proper way to do that would be something like this

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

alpha[i] * alpha[i] won't work since alpha[i] doesn't have a value assigned to that index yet
And to store it as a double which is what your array is - you need to cast it to a double:

 http://www.cplusplus.com/doc/tutorial/typecasting/

If i understand right, you should output it as following:
 ``123456`` ``````int i,j; for(i=0;i<=5;i++){ for(j=0;j<=10;j++){ cout << alpha[i][j] << endl; } }``````

This works like this: when i=0 j=0 is 0,0 index, that is 1*1, result 1. Next i=0 j=1 index 0,1 that is 2*2, result 4 and so on. When j hits 10, the first for loop executes again, makes i=1 and second for loop executes utill j=10 again. Continues untill i equals 5.
I ended up finishing the assignment, thank you all for those who replied.
Although it probably wasn't the most efficent programming man has ever seen, it got the job done. I plan on meeting my instructor to better understand for loops later. Thanks again everyone for helping me. This forum is amazing.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include using namespace std; int main() { double alpha [50]; for (int i = 0 ; i <= 24; i++) {alpha[i] = static_cast(i * i); } for (int i = 25 ; i <= 49 ; i++) { alpha[i] = static_cast(i * 3); } for (int i = 0 ; i <=9; i++) {cout << setw(5) << alpha[i]; } cout << endl; for (int i = 10; i <=19 ; i++) {cout << setw(5) << alpha[i]; } cout << endl; for (int i = 20; i <= 29; i++) { cout << setw(5) << alpha[i]; } cout << endl; for (int i = 30; i <= 39; i++) { cout << setw(5) << alpha[i]; } cout << endl; for (int i = 40; i <= 49 ; i++) { cout << setw(5) << alpha[i]; } cout << endl; return 0; }``````

If you would like, I will gladly help explain and teach you loops and their usage. If you are interested, post your email and I'll contact you. :)
The body of the last 5 for loops are all the same, so that could be done with 1 for loop going from 0 to 49:

 ``1234`` ``````for (int i = 0; i < 50 ; i++) { cout << setw(5) << alpha[i]; } cout << endl;``````

The original question asked for output 10 lines at a time - so go back to the `double alpha[5][10] ` and make use of CrysMaster's advice.
Last edited on
 The body of the last 5 for loops are all the same

But what was between them wasn't accounted for.

 ``123456`` ``````for ( int i=0; i<5; ++i ) { for ( int j=0; j<9; ++j ) cout << setw(5) << alpha[i*10+j] ; cout << '\n' ; }``````
Topic archived. No new replies allowed.