Using function arrays

Hello there c++ programmers.

I've been struggling to understand arrays, since a book requires me to use them in a problem solving case.

I finally got it to work like a charm! Tho I would like another one to please take a look on it and tell me if it is stable or correct use of function arrays.

Write a program that takes in 5 values and prints out the highest, the lowest, the average and then all 5 input values, one per line.

This was the practice problem in the book.

And this is the source code I made:

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

int populate(double array1[], int number);			// Prototype of the array function 'int populate'.
void spit(double array1[], int number);				// Prototype of the array function 'void split'.
int plus(double array1[], int number);				// Prototype of the array function 'int plus'.
int highest_value(double array1[], int number);		// Prototype of the array function 'int highest_value'.
int lowest_value(double array1[], int number);		// Prototype of the array function 'int lowest_value'.
int average_number(double array1[], int number);	// Prototype of the array function 'int average_number'.

int main(){

double input[5];				// Declaring an array to use for the function arrays.

populate(input, 5);				// Calls the array function 'int populate'.
spit(input, 5);					// Calls the array function 'void split'.
plus(input, 5);					// Calls the array function 'int plus'.
highest_value(input, 5);		// Calls the array function 'int highest_value'.
lowest_value(input, 5);			// Calls the array function 'lowest_value'.
average_number(input, 5);		// Calls the array function 'average_number'.

cout << endl;
cout << "------------------------------------------" << endl;

system("pause");
return 0;
}

int populate(double array1[], int number){  // User declares input.
	double input;
	cout << endl << endl;
	for(int i = 0; i < number; i++){
		cout << "enter value for item " << (i + 1) << ": ";
		cin >> input;
		cout << endl;
		array1[i] = input;
	}
	cout << "------------------------------------------" << endl;
		return input;
}

void spit(double array1[], int number){  // The console prints what the user input.
	
	cout << endl << endl << "CONSOLE LOG" << endl;
	cout << "You entered: " << endl;
	for(int i = 0; i < number; i++){
		cout << endl;
		cout << endl << "the value of item " << (i + 1) << " = " << array1[i];
	}
	cout << endl << endl << "-------------------------------------------" << endl;
}

int plus(double array1[], int number){       // + together the users input, so it = total amount.
	int sum = 0;
	for(int i = 0; i < number; i++){
		sum += array1[i];
	}
		cout << endl << endl << "The total of all the 5 numbers" << " = " << sum << endl << endl;
		return sum;
}

int highest_value(double array1[], int number){    // Finds the highest value the user input.
	int temp = 0;
	for(int i = 0; i < number; i++){
		if(array1[i] > temp)
		temp = array1[i];
	}
		cout << endl << "The highest value of all the 5 numbers" << " = " << temp << endl << endl;
		return temp;
}

int lowest_value(double array1[], int number){     // Finds the lowest value the user input.
	int small = array1[0];
	for(int i = 0; i < number; i++){
		if(array1[i] < small) 
		small = array1[i];
	}
	cout << endl << "The lowest value of all the 5 numbers" << " = " << small << endl << endl;
	return small;
}

int average_number(double array1[], int number){   // Finds the average number the user input.
	double sum = 0;
	for(int i = 0; i < number; i++){
		sum += array1[i];
	}
		sum = sum / number;
		cout << endl << "The average value of all the 5 numbers" << " = " << sum << endl << endl;
		return sum;
}



Thankyou for helping out!
Last edited on
One thing would be that instead of putting the number "5" everywhere, define a constant that is the array size and set it to 5. That way, if you ever want to change the array size, you just change one variable instead of changing it in several places. You would do this just by declaring const int SIZE = 5; right before the declaration of your array.
Thanks. But you can also fix it with putting 'int number' there instead of 5, I found out.
I have not understood your question. I can make some comments. Why did you name one of the functions 'spit' though in comments you are naming it as split? I think it would be more natural to name it something as print or display or output.
The return value of function populate has no any sense. Function highest_value is incorrect. It should be written the same way as function lowest_value. The both function shall not have the ouput statement.
Function average_number is also incorrect. Its return type shhall be declared as double.
Last edited on
Topic archived. No new replies allowed.