Why are these codes doing this?

Here are two different codes with the same information just about. But each code is different. Each time I run one the output is vertical, when I run the other the output is horizontal. I'm too new at this to understand why. Can someone pinpoint the issue? Also, how would I include a comma for each array number in the output?

Horizontal code.
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
ifstream infile;  //declaring the file to open
ofstream outfile;  //the file to write to

/***********************************/
//function declarations
void printArray();
void addArrays();
void average();
void largest();
void smallest();
void averageOddNumbers();
/*************************************/
void printArray(int arr[],int n)//print array
{
    int i=0;
    for(i=0;i<n;i++)
    outfile<<arr[i]<<" ";
    outfile<<endl;
}

void addArrays(int a[],int b[], int c[],int n)//Add arrays
{
   int i=0;
   for(i=0;i<n;i++)
   c[i]=a[i]+b[i];
}

void multiply(int a[],int b[],int c[],int n)//Multiply arrays
{
    int i=0;
    for(i=0;i<n;i++)
    c[i]=a[i]*b[i];
}

void average(int a[],int n)//Average of array
{
  float sum=0;
  int i=0;
  for(i=0;i<n;i++)
  sum=sum+a[i];
  outfile<<(sum*1.0)/n<<endl;
}

int largest(int arr[],int n)//find the largest number in the array
{
   int max=-1;
   int min=-1;
   int i;
   for(i=0;i<n;i++)
   if(arr[i]>max)
   max=arr[i];
   return max;
}

int smallest(int arr[],int n)//find the smallest number in the array

{
   int min=-1;
   int i;
   for(i=0;i<n;i++)
   if(arr[i]<min)
   min=arr[i];
   return min;
}

void averageOddNumbers(int arr[],int n)//calculate odd numbers Average of array
{
int count=0;
int sum=0;
int i=0;
for(i=0;i<n;i++)
if(arr[i]%2==1)
{
sum=sum+arr[i];count++;
}
outfile<<(sum*1.0)/count<<endl;
}

int main()
{
    infile.open("numbers2.in");
    if(!infile)
    {
        cout<<"Cannot read file. Please try again.";
        return 0;
    }

    outfile<<fixed<<setprecision(1);
    outfile.open("operation.out");

int A[25]; //array A
int B[25]; //array B
int C[25]; //array C
int i=0;
int max;
int min;

for(i=0;i<25;i++)
{
   infile>>A[i];  //reading file into array A
   B[i]=rand()%100+1; //assigning random numbers 1-100 for B
}

//printing all the result
outfile<<"array A has :"<<endl;
printArray(A,25);
outfile<<endl;

outfile<<"array B has :"<<endl;
printArray(B,25);
outfile<<endl;

addArrays(A,B,C,25);
outfile<<"array C when A,B are added:"<<endl;
printArray(C,25);
outfile<<endl;

multiply(A,B,C,25);
outfile<<"array C after multiplying A and B:"<<endl;
printArray(C,25);
outfile<<endl;

outfile<<"Average of array A is:";
average(A,25);
outfile<<endl;

outfile<<"Average of array B is:";
average(B,25);
outfile<<endl;

outfile<<"largest number in array A is:";
max=largest(A,25);
outfile<<max<<endl;

outfile<<"largest number in array B is:";
max=largest(B,25);
outfile<<max<<endl;

outfile<<"smallest number in array A is:";
min=smallest(A,25);
outfile<<min<<endl;

outfile<<"smallest number in array B is:";
min=smallest(B,25);
outfile<<min<<endl;

outfile<<"Average of odd numbers in array A is:";
averageOddNumbers(A,25);
outfile<<endl;

outfile<<"Average of odd numbers in array B is: ";
averageOddNumbers(B,25);
outfile<<endl;

infile.close();  //closing the infile
outfile.close(); //closing the outfile
return 0;

}


Vertical Code
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

ifstream inF;
ofstream outF;

void printArray(int arr[], int n)
{
    int x=0;
    for(x=0;x<n;x++)
    outF<<arr[x]<<" "<<endl;
}
void Add(int a[], int b[], int c[], int n)
{
	int x=0;
	for(x=0;x<n;x++)
    c[x]=a[x]+b[x];
}
void Product(int a[], int b[], int c[], int n)
{
	int x;
	for(x=0;x<n;x++)
    c[x]=a[x]*b[x];
}
void Average(int a[], int n)
{
	float sum=0;
	int x=0;
	for(x=0;x<n;x++)
		sum+=a[x];
	outF<<(sum*1.0)/n<<endl;
}
int Largest(int arr[], int n)
{
	int max=0,x;
	for(x=0;x<n;x++)
    if(arr[x]>max)
        max=arr[x];
	return max;
}
int Smallest(int arr[], int n)
{
	int min=99, x;
	for(x=0;x<n;x++)
    if(arr[x]<min)
    min=arr[x];
	return min;
}
void Avg_Odd(int arr[], int n)
{
	int count=0, sum=0, x=0;
	for(x=0;x<n;x++)
    if(arr[x]%2==1)
    {
        sum+=arr[x];
        count++;
    }
    outF<<(sum*1.0)/count<<endl;
}
int main()
{
    inF.open("numbers2.in");
    if(!inF)
    {
        cout<<"Error, try beating it with your hand!";
        return 0;
    }

outF<<fixed<<setprecision(2);
outF.open("operation.out");

int a[25], b[25], c[25],x=0,max,min;

	for(x=0;x<25;x++)
    {
        inF>>a[x];
        b[x]=rand()%100+1;
    }

outF<<"array A has : ",
printArray(a,25);
outF<<endl;

outF<<"array B has : ",
printArray(b,25);
outF<<endl;

Add(a,b,c,25);
outF<<"array C when A,B are added:",
printArray(c,25);
outF<<endl;

Product(a,b,c,25);
outF<<"array C when A,B are Multiplied:",
printArray(c,25);
outF<<endl;

outF<<"Average of array A is:";
Average(a,25);
outF<<endl;

outF<<"Average of array B is:";
Average(b,25);
outF<<endl;

outF<<"largest number in array A is:";
max=Largest(a,25);
outF<<max<<endl;

outF<<"largest number in array B is:";
max=Largest(b,25);
outF<<max<<endl;

outF<<"smallest number in array A is:";
min=Smallest(a,25);
outF<<min<<endl;

outF<<"smallest number in array B is:";
min=Smallest(b,25);
outF<<min<<endl;

outF<<"Average of odd numbers in array A is:";
Avg_Odd(a,25);
outF<<endl;

outF<<"Average of odd numbers in array B is: ";
Avg_Odd(b,25);
outF<<endl;

outF.close();
inF.close();
return 0;
}
13 and fourteen puts then endl on the same line as the file output.
I am not entirely sure but maybe in the first code since it is on a second line it is only done once. I suggest you put brackets in your code to reduce ambiguity and maybe more things like that will show up.
I figured it out. I needed to remove an endl; in the code which prevented the numbers to align horizontal. Sorry for wasting your time if you've read this.
Just do us a favor and mark this thread as answered and yer good lol 😆
Topic archived. No new replies allowed.