Printing an array

I have an array filled with 1440 values. When I am reading in the file or outputting to a file, the values are being printed horizontally until it hits the window limit and starts a new line. Is there any way to write all these values in 12 columns to the file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void output(std::ofstream& object, double avg, double max, double min, int voltagedips, int vcount[], double svv[])
{
	int i; int s = 1; int z = 0;
	object << "The Average Voltage is: " << avg << "V" << std::endl;
	object << "The Maximum Voltage is: " << max << "V" << std::endl;
	object << "The Minimum Voltage is: " << min << "V" << std::endl;
	object << "The Total Number Of Voltage Dips is: " << voltagedips << std::endl;
	object << "Voltage Dips Per Hour: " << '\n' << std::endl;
	for (i = 0; i < 24; i++)
	{
		object << "Hour " << s << '\t' << vcount[i] << std::endl;
		s++;
	}
	object << '\n' << "Voltage Variation: " << '\n' << std::endl;
	for (i = 0; i < 1440; i++)
	{
		object << svv[i] << "V" << '\t';
		if (z % 12 == 0)
		{
			std::cout << std::endl;
		}
		z++;
	}
}


This is the values in the file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
106	110	16	36	5	38	110	37	14	86	48	67
50	15	97	21	46	97	112	105	10	53	18	109
114	39	32	43	90	10	100	50	105	66	83	52
104	54	41	61	12	6	0	104	37	84	7	28
96	98	67	95	49	65	57	26	67	68	1	31
63	42	44	54	37	96	103	38	83	91	92	14
97	5	108	19	60	87	84	37	4	90	11	41
55	71	100	54	55	54	102	2	15	65	20	93
6	3	65	40	54	58	29	66	77	32	87	7
19	69	110	111	114	81	66	57	22	83	59	106
14	0	8	29	37	96	4	30	37	102	27	32
26	75	50	51	114	111	49	110	90	87	108	18
13	2	6	33	43	38	12	110	88	27	67	86
0	90	56	52	107	26	115	97	110	33	37	95
107	91	90	103	67	66	7	64	86	94	107	42
4	44	28	67	102	70	17	51	37	19	87	38
82	19	9	30	27	104	54	54	69	103	7	9
56	27	21	4	20	16	45	86	43	21	1	53
69	104	49	61	40	52	113	72	88	35	105	58
18	45	34	3	51	101	15	19	53	82	11	104
62	79	64	20	11	18	31	35	54	21	24	58
64	55	34	101	15	40	70	50	78	21	85	96
10	14	44	35	110	112	109	100	104	95	17	11
98	47	81	44	110	4	39	57	82	29	100	46
29	56	3	101	13	79	21	114	54	25	115	43
67	10	54	63	39	51	69	92	29	106	32	82
25	105	12	9	5	23	107	9	94	90	114	49
79	98	113	44	47	107	7	21	29	68	101	46
54	27	5	47	28	113	96	99	105	89	69	10
37	100	5	47	32	7	56	102	28	66	28	104
43	82	87	88	104	30	68	9	8	58	67	87
94	95	50	37	70	2	101	101	93	39	11	33
22	1	35	26	39	14	38	11	91	78	65	115
90	89	52	54	89	39	85	5	104	46	74	85
115	6	105	103	97	3	77	47	25	67	81	90
62	44	66	53	66	9	65	95	99	86	115	96
92	94	68	17	14	40	58	46	93	30	23	42
35	101	29	60	93	37	91	38	114	16	22	87
82	77	47	10	110	53	94	41	36	4	60	99
103	59	41	9	83	105	62	11	44	19	31	64
77	33	95	17	23	93	24	2	41	53	6	71
113	82	108	8	18	15	101	104	87	110	16	50
79	110	80	49	56	89	22	31	73	109	91	5
59	112	8	86	113	40	23	30	75	34	45	23
27	60	18	78	115	70	24	96	59	0	91	111
35	30	95	52	77	66	6	9	26	112	13	25
95	101	99	13	6	4	7	44	33	64	60	64
34	9	29	51	27	89	52	77	86	24	32	114
106	50	95	29	83	25	112	99	109	60	25	41
89	78	5	25	1	28	63	72	43	20	70	4
1	48	10	72	58	101	30	3	98	57	85	14
105	108	27	102	44	73	25	11	10	39	57	24
34	80	91	100	61	34	83	13	63	108	31	82
68	114	47	54	6	23	65	53	22	23	43	54
16	89	43	51	95	5	90	84	57	67	36	76
115	80	53	88	42	109	85	44	77	21	35	100
14	82	71	35	55	19	110	113	4	44	97	82
87	98	15	15	19	36	57	102	8	61	108	24
101	20	61	37	103	102	71	95	78	103	10	49
31	51	12	72	113	37	25	100	11	24	28	77
115	109	54	99	67	56	104	108	6	93	110	18
46	24	71	48	29	97	56	12	4	40	59	97
98	114	100	13	89	55	17	89	21	111	51	103
52	25	9	74	66	23	11	109	46	19	108	51
12	106	52	8	104	22	88	99	36	24	65	95
37	21	72	74	13	7	44	67	50	79	57	114
4	54	100	84	105	38	77	13	82	5	2	8
51	53	95	63	104	101	84	51	108	104	103	25
111	86	105	78	3	52	52	25	33	87	57	96
67	21	106	49	5	26	63	90	100	89	53	90
31	77	96	11	107	74	64	19	45	52	64	98
50	45	50	8	73	73	64	15	14	67	31	109
77	47	4	39	6	71	64	77	4	29	73	106
66	59	74	90	58	6	43	73	106	106	65	58
54	79	25	14	28	76	36	103	91	106	63	35
3	79	55	34	39	33	24	32	43	60	68	108
9	79	51	90	2	108	11	31	35	84	11	104
1	16	78	109	21	0	55	44	70	22	50	33
81	33	19	61	48	107	83	110	111	88	91	79
52	34	97	16	29	12	102	80	38	79	102	66
32	16	14	107	112	62	62	75	26	110	38	78
91	23	13	21	72	53	31	92	66	41	97	84
84	114	47	105	93	102	82	76	19	30	36	93
105	77	111	54	76	86	94	28	115	38	5	24
34	56	72	71	107	99	47	9	99	91	74	100
23	19	78	15	56	0	54	89	105	47	23	52
98	83	110	1	76	30	76	92	62	24	88	69
43	5	105	89	83	96	84	17	5	48	27	70
81	92	64	103	104	76	27	73	15	101	103	13
23	8	77	18	65	18	2	11	65	103	55	34
106	60	1	110	43	61	15	64	35	92	4	69
59	53	106	17	88	77	92	113	4	9	63	107
5	29	83	73	31	83	21	26	102	51	70	109
32	8	8	73	86	46	110	46	89	86	20	10
5	59	40	43	63	27	16	21	60	93	53	62
21	2	88	51	6	28	71	88	71	108	114	97
9	74	37	57	27	104	49	114	30	36	21	67
49	78	50	53	81	3	100	115	111	74	22	59
75	34	58	46	5	63	16	8	100	35	36	106
98	27	11	104	62	11	95	43	78	109	80	84
32	33	54	91	20	5	7	58	58	69	12	64
37	59	89	88	57	103	111	58	88	82	5	32
0	24	12	55	44	53	27	54	36	0	21	37
45	50	71	87	60	52	14	60	25	106	113	16
0	106	14	41	40	113	47	114	39	83	0	66
13	106	19	5	4	93	56	51	78	11	13	94
67	115	91	3	1	88	73	57	30	115	48	84
75	110	51	61	60	57	89	89	70	73	45	21
89	16	89	31	87	59	22	30	59	50	113	111
48	56	89	30	29	22	97	17	24	95	112	60
84	67	61	115	79	54	87	104	58	32	66	49
71	93	23	51	47	24	55	101	7	87	34	94
50	115	1	49	27	84	61	61	63	72	104	46
103	74	27	115	55	23	5	43	97	0	104	21
103	66	73	4	114	16	45	106	41	15	32	46
40	31	18	5	80	40	14	67	98	6	17	9
6	47	15	25	90	107	41	89	28	9	36	15
37	20	4	23	34	4	65	11	61	49	8	20
18	46	99	77	56	30	52	114	7	61	51	17
17	111	27	22	101	74	42	0	113	87	113	77


An example of how they are printed
 
9V	5V	99V	79V	110V	77V	5V	78V	101V	29V	67V	48V	65V	100V	18V	94V	69V	18V	3V	10V	105V	62V	97V	6V	1V
Last edited on
Hello nicholasjb1996,

You could try this:
1
2
3
4
for (i = 0; i < 1440; i++)
{
	std::cout << std::setw(3) <<  e[i] << "V" << (i + 1 < 12 ? '\t' : '\n');	
}

Include header file "<iomanip>" for the "setw()". This will line all your numbers to the right with each one taking three spaces and any number less than 100 being padded with spaces.

This should work, but I have not tested it yet.

Hope that helps,

Andy
I fixed it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void output(std::ofstream& object, double avg, double max, double min, int voltagedips, int vcount[], double svv[])
{
	int i; int s = 1; int z = 1;
	object << "The Average Voltage is: " << avg << "V" << std::endl;
	object << "The Maximum Voltage is: " << max << "V" << std::endl;
	object << "The Minimum Voltage is: " << min << "V" << std::endl;
	object << "The Total Number Of Voltage Dips is: " << voltagedips << std::endl;
	object << "Voltage Dips Per Hour: " << '\n' << std::endl;
	for (i = 0; i < 24; i++)
	{
		object << "Hour " << s << '\t' << vcount[i] << std::endl;
		s++;
	}
	object << '\n' << "Voltage Variation: " << '\n' << std::endl;
	for (i = 0; i < 1440; i++)
	{
		object << svv[i] << "V" << '\t';
		if (z % 12 == 0)
		{
			object << '\n';
		}
		z++;
	}
}
Hello nicholasjb1996,

After testing I came up with this for what its worth:
1
2
3
4
5
6
7
int len{ 12 };

for (int i = 0; i < MAXVOLTS; i++, len += 12)
{
	std::cout << std::setw(3) << e[i] << "V" << (i + 1 < len ? '\t' : '\n');
	//std::cout << std::setw(3) << e[i] << "V" << (i + 1 < len ? "  " : "\n");
}

with output of:

The Supply Voltage Variation is:

106V    110V     16V     36V      5V     38V    110V     37V     14V     86V     48V     67V
 50V     15V     97V     21V     46V     97V    112V    105V     10V     53V     18V    109V
114V     39V     32V     43V     90V     10V    100V     50V    105V     66V     83V     52V
104V     54V     41V     61V     12V      6V      0V    104V     37V     84V      7V     28V
 96V     98V     67V     95V     49V     65V     57V     26V     67V     68V      1V     31V
 63V     42V     44V     54V     37V     96V    103V     38V     83V     91V     92V     14V



Andy
Topic archived. No new replies allowed.