Array Functions

So I tackled this project yesterday, finding the max/min/average of an array using functions all outside of the main function, but my values are not coming up correctly, any help?
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
 
#include "stdafx.h"
#include <iostream>
#include <string>
#include <array>


// Max/Minimum Function
void
max(double array[],int sizeOfArray)
{
	using namespace std; 

	double max= array[0];

	cout << "\n";
	

	for 
		(int i = 1; i  <sizeof(array); i++)
	{
		if (array[i] > max) {
			max = array[i];
		}
	}

	cout << "                             Maximum Value: " << max<<endl;

	
}
void 
min(double array[], int sizeOfArray) {
	using namespace std;

	double min = array[0];
	for
		(int i = 1; i < sizeof(array); i++) {
		if (array[i] < min) {
			min = array[i];
		}

	}
	cout << "                             Minimum Value: " << min << "\n\n";

}


// Average Price Function
void
average_price(double array[],int sizeOfArray){
	using namespace std;

	double sum = 0, average = 0;
	int counter = 0,j=0;

	for (int i = 0; i < sizeof(array); i++) {
		sum += array[i];

	}
	average = (sum / sizeof(array));
	cout << "                             The Average price over 20 days:  \n" << "                             "<< average  << endl;

	for (int j = 0; j < sizeof(array); j++){
		if (array[j] > average) {
			counter ++;
		}
	}
	cout << "                             Number of days over Average: " << counter << "\n\n";

}



// Main Function
int main()
{
	// Initializes Arrays and also an X variable for the switch statement required for Report 4
	using namespace std;
	int x = 0;

	double ford[] = { 34.25,40.50,36.50,40.00,
		30.25,30.25,35.50,36.00,
		34.25,37.00,34.00,35.00,
		36.25,34.25,40.50,41.50,
		41.50,40.00,36.50,34.50 };

	double honda[] = { 40.25,38.50,34.50,33.50,
		30.50,29.75,37.50,36.00,
		34.75,38.00,34.25,37.00,
		34.25,37.50,34.50,38.50,
		37.50,37.25,38.25,37.50 };

	double audi[] = { 100.41, 90.45, 99.30, 102.99,
		98.54, 95.30, 92.32, 110.88 };

	string name[] = { "Ford","Honda","Audi" };

	// Calls the Max and Min Function, passing pointers from the arrays and also their length using the sizeof function
	// FIRST REPORT
	cout << "                             ***  Report 1 - Max and Minimum ***\n";
	cout << "                             Ford\n";
		max(ford, sizeof(ford));
			min(ford,sizeof(ford));
	cout << "                             Honda\n";
		max(ford, sizeof(ford));
			min(honda, sizeof(honda));
	system("                             pause");


	// Calls the Average Price function, passing pointers from the arrays and also their length using the sizeof function
	// SECOND REPORT
	cout << "                             Average Price of Ford\n";
		average_price(ford, sizeof(ford));
	cout << "                             Average Price of Honda\n";
		average_price(honda, sizeof(honda));
	system("pause");


	// Calls the two previous functions, but uses the Audi array for both
	// THIRD Report
	cout << "                             **  Audi Report - Maximum, Minimum & Average Price  **\n";
		max(ford, sizeof(ford));
			min(audi, sizeof(audi));
				average_price(audi, sizeof(audi));
	system("pause");


	// Calls the Average Price Function for one of the three car manufacturers using a switch statement for the user to choose
	// FOURTH REPORT
	cout << "To view the average price of a certain make: Press 1 for Ford, 2 for Honda, and 3 for Audi ";
		cin >> x;

	while (x > 3) {
		cout << "Please enter a value between 1 to 3\n";
		cin >> x;
	}
	switch (x)
	{
	case 1:			average_price(ford, sizeof(ford));
		break;
	case 2:			average_price(honda, sizeof(honda));
		break;
	case 3:			average_price(audi, sizeof(audi));
		break;
	}
	system("pause");
    return 0;
}


Is it because I am passing a sizeOfArray variable for all my functions as the second argument? I didnt want to pass a constant, I wanted it up be more abstract, when I run the code for the first report I get
Ford Max: 40.5
Min: 34.25
and Honda Max:40.5
Min : 33.5

For the average I get
Ford : 38.8125 -which is right, but
days over average : 2


Where is my logic incorrect? First time poster, thanks!
sizeof() returns the size of a data element in bytes. You want to know how many items are in the array. So basically you have to divide the number of bytes in the array by the number by bytes per item. A common way to do this is sizeof(theArray)/sizeof(theArray[0])
When I implement sizeof(theArray)/sizeof(theArray[0]), I get a decimal value >1 for my average. I am implementing this in my average algorithm within my average function, should it be instead be implemented into the parameters that are passed to my function??
avg of all those arrays is > 1. its @ 40 for ford and Honda approximately, for example.

sizeof (int) = 4.

int x[10];
sizeof(x) = 40.

sizeof(x)/sizeof(x[1]) = 10;

It looks like just about every use of sizeof here is too big.
Last edited on
Topic archived. No new replies allowed.