Problem initializing or displaying 2-D array to zero

Hi, I want to initialize a 10x4 array to zero. The problem is that when I display it using a "cout," the elements of the tenth row display as something like:

"5.3e-308"

Is there any way that I can fix this and display a "0"?

Here is my code:
1
2
3
  double stack [12][4] = {0};
for (i=10; i>0; i--)
    cout<<setw(2)<<i<<":"<<setw(12)<<setprecision(2)<<stack[i][1]<<" ,"<<setw(12)<<stack[i][2]<<endl; 


thanks for your help
Hi there,

I can spot a couple of things here - one of which is certainly responsible for your problem.

1) You say you want a 10x4 array. You have created a 12x4 array.

2) The data 'locations' in an, (and your) array, begin at stack [0][0] and in your case, finish at stack [11][3].

3) If you are only wanting to have two columns and 10 rows, then I suggest a 2D array of stack [11][2] - This will allow you to have your numbers 10-1 printed alongside.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main ()
{ 
 int i; 
  
double  stack [11][2] = {0};
for (i=10; i>0; i--)
    cout<<setw(2)<<i<<":"<<setw(12)<<setprecision(2)<<stack[i][0]<<" ,"<<setw(12)<<stack[i][1]<<endl; 
	return 0;
	}
The initialization looks fine but what's with the format specifiers just to print a bunch of zeros? Is there something in between that modifies the array? Also neither of the loops make much sense since the value at offset [0][x] will ever get printed. Why are you iterating backwards? I think that you want >= 0 or just iterator forward with < dimensionSize.
Topic archived. No new replies allowed.