Array printing

I have posted the same thing before but it wasn't clear, I apologize for that.
But can someone tell me why is Print_B printing gibberish?

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
  #include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int columns = 4;  //Declare constants
const int rows = 3;
void Fill_Array (int [][columns] , int , int ); // Fill_Array prototype
void Copy_Array (int [][columns], int [][rows], int, int); // Copy_Array prototype
void Print_A (int [][columns] , int , int  ); // Print_A prototype
void Print_B (int [][rows] , int , int  ); // Print_B prototype
void Print_Statistics (int [][columns], int, int); // Print_Statistics prototype
int main() { // main function
 int A[rows][columns]; // array A[3][4]
 int B[columns][rows]; // Array B [4][3]
Fill_Array ( A, rows, columns);; // test Fill_Array
Copy_Array (A, B, rows, columns ); // test Copy_Array
Print_A(A, rows, columns); //test Print_A
Print_B (B, rows, columns); // test Print_B
Print_Statistics(A, rows, columns); // test Print_Statistics
	return 0; // Program ends
} // end main
void Fill_Array (int A[][columns], int rows, int columns) // Fill_Array function, takes array A and fills it
{   
	srand(time (0)*time(0) - time(0) ); // seed set by clock
	 
for (int i =0; i < 3; i++){ // loop for number of rows
for (int j = 0; j < 4 ; j = j + 2){ //for odd numbers, first number (j = 0) is odd and so is the third number (j = 2)
	
A[i][j] = 2*(1 + rand() % 24) + 1; // generates random odd number between 1 and 49 
} // end odd loop
for (int j = 1; j < 4; j = j + 2) // for even numbers (j = 1, j = 3) and so on
A[i][j] = 2*(1 + rand() % 25) ; // generates random even number between 2 and 50
} // end loop for both

} // end function
void Copy_Array (int A[][columns], int B[][rows], int rows, int columns ) // Copy_Array function, takes B array, copies A into B
{int tem;
for (int i = 0; i < 3 ; i++){ // loop for number of rows for A, columns for B
for (int j = 0; j < 4; j++){ // loop for number of columns for A, rows for B
A[i][j] == B[j][i]; // rows and columns are reversed
} // end loop
} // end loop
} // end function
void Print_A (int A[][columns], int rows, int columns) // Prints A
{
	cout << " The elements int array A are: " <<endl;
for (int i = 0; i < 3; i++){ // rows loop
	for (int j = 0; j < 4; j++){ // columns loop
cout << A[i][j] << "   "; // prints A
if (j == 3) // skips line every fourth number
cout << endl; // end line
} // end loop
} // end loop
} // end function

void Print_B (int B[][rows], int rows, int columns) // Prints B, takes B as an argument
{
	cout << endl; // end line
		cout << " The elements int array B are: " <<endl;
for (int i = 0; i < 4; i++){ // rows loop
for (int j = 0; j < 3; j++){ // columns loop
cout << B[i][j] << "   "; // prints B
if (j == 2) // skips line every third character
cout << endl; // end line
} // end loop
} // end loop
} // end function

void Print_Statistics (int A[][columns], int, int ) // Print statistics
{
	cout << endl; // end line
	int evensum = 0, oddsum = 0, evenaverage, smallest, element; //declaration of integers, the sum of even and odd numbers, smallest, and temporary element
	double  oddaverage; // the average of odd numbers is sometimes decimal
	 cout << "The average of even numbers is: " << endl;
	for (int i = 0; i < 3; i++){ // rows loop
	for (int j = 1; j < 4; j = j + 2) // columns loop
 evensum = (A[i][j] + evensum); // sum of even numbers
	}
	evenaverage = evensum/6; // average of even numbers (sum divided by the number of even numbers)

 cout << evenaverage; // Prints even numbers average

cout << endl; // end line
cout << "The average of odd numbers is: " << endl;
	for (int i = 0; i < 3; i++){ //columns loop
	for (int j = 0; j < 4; j = j + 2) //  rows loop
 oddsum = (A[i][j] + oddsum); // odd numbers sum
	}
	oddaverage = static_cast <double>(oddsum) / 6; //odd number average, sum divided by number of odd numbers
 cout << oddaverage; // prints odd numbers average
cout << endl; // end line
smallest = A[0][0]; // begin by supposing first term is smallest
for (int i = 0; i < 3; i++){ // rows loop
for (int j = 0; j < 4; j++){ // columns loop
element = A[i][j]; // assigning the element to an int
if (element < smallest) // if this element is smaller than smallest
smallest = element; // this element becomes smallest
} // end loop
} // end loop
cout << "the smallest element is: " << endl;
cout << smallest; // cout smallest number
} //  end function 




Output:
The elements int array A are:
13 22 41 40
43 50 45 10
43 34 49 14

The elements int array B are:
134514544 134523088 134522836
134514365 -1216718272 -1216736824
134522756 134517042 1
-1080945708 -1080945700 -1219400115

The average of even numbers is:
28
The average of odd numbers is:
39
the smallest element is:
10
line 40 looks backwards to me. The array B should be on the left.
Last edited on
A couple of things:
1. Whitespace doesn't cost anything. I can barely read this. Potentials issues/errors might be more easily spotted if the code was clearer.
2. Your functions are taking, amongst other things, 2 ints, presumably for rows and columns values that you define on lines 5 and 6. But then in every function you use hard-coded values of 3 and 4. in other words you dont use these constants.

Maybe you have your 3 and 4's mixed up in different methods? I'm not sure as it's difficult to skim over and read.

edit:
line 40 looks backwards to me.

line 40 isn't doing anything. It's testing for equality and doing nothing with the result.
Last edited on
The problem is probably in your copy funciton. Look at line 40, seems faulty. I think you want to use an assignment there. And if that's the case, B should be the lvalue, not A. Basically, you aren't copying anything into B, hence it prints out those random values as the array is not initialized yet.

Also, why do you have parameter rows and columns in your functions when you don't use them. Further, you shouldn't name it column if it's also going to be the number of rows for B. It's confusing.
Thanks, problem solved
Topic archived. No new replies allowed.