How to get the lowest value as well as the highest even element in an array

Hello Guys! I've been working on a program that will allow one to enter 10 elements. Upon those entry's then calculate the combined average, find and print on screen the lowest of the input, and the highest even number. Now I've got it successfully compile and it does every function and outputs what I want to see, however I can't seem to get two things correct. I'm having trouble locating and printing the lowest element, as well as locating and print the highest even number in the array. Bellow is the code I've constructed and anything is helpful. Thank you to everyone in advance who's taken the time to assist me.
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
//preprocessor directives
#include <iostream>

//namespace statement
using namespace std;
//function prototypes
void getData(float arrayNums[]);
void calcFunc(float arrayNums[], float& sum);
void printFunc(float sum);
void calcSmall(float arrayNums[], float& small);
void printSmall(float small);
void calcLargest(float arrayNums[], float& largest);
void printLargest(float largest);
//main function
int main(){
//declare variables arrayNums[10], sum, small 
float arrayNums[10], sum, small, largest;
//functioncalls
getData(arrayNums);
calcFunc(arrayNums, sum);
printFunc(sum);
calcSmall(arrayNums, small);
printSmall(small);
calcLargest (arrayNums, largest);
printLargest (largest);
	
	//print message, wait for keypress, and end the main func.
	system ("pause");
	//ends main function
	return 0; 
}

//getData: gets 10 inputs from user and stores them in "arrayNums[index]
void getData(float arrayNums[]){
	//prompt the user to "Enter in 10 values". followed by a linebreak
	cout << "Enter in 10 values (press ENTER key after each entry): " << endl;
	//for loop: designed to take userinput an store within array, "arrayNums[index]
	for (int index  = 0; index <10; index= index +1)
	//userinputs gets stored in arrayNums[index]
	cin >> arrayNums[index]; 
	
}
//calcFunc: calculates the sum of the array and solves for the average
void calcFunc(float arrayNums[], float& sum){
	//give sum the value of "0"
	sum = 0;
	//for loop: designed to calculate the average the sum of the array
	for (int index = 0; index <10; index = index +1)
	//calculation for sum, used to conclude the average
	sum = sum +arrayNums[index] / 10;
}

//printFunc: prints the sum average of the array
void printFunc(float sum){
	//print on screen, "The average value of the array is: " followed by a linebreak
	cout << "The average value of the array is: " << endl;
	//print on screen, "sum"
	cout << sum << endl; 
}

//calcSmall: Calculates the smalles value in the array
void calcSmall(float arrayNums[], float& small){
	//give small the value of "10"
	small =0;
	//for loop: designed to calculate the smallest value in the array
	for (int index = 0; index < 10; index = index +1)
	//assign small the value of arrayNums[7]
	small = arrayNums[0];
}

//printSmall: Prints the smallest value of the array
void printSmall(float small){
	//print on screen, "the smallest value of the array is: " follwed by a line break
	cout << "The smallest value of the array is: " << endl;
	//print on screen, "small" followed by a line break
	cout << small << endl;  
}
//calcLargest: calculates the largest even number in the array
void calcLargest(float arrayNums[], float& largest){
	//assign largest the value of "-1"
	largest = -1;
	//for loop: designed to check the elements in the array, thus determing the largest even number
	for (int index = 0; index < 10; index = index +1);
	// if statement, used to check and see which of the elements in arrayNums[] preceed the highest even number
	if (arrayNums[0,1,2,3,4,5,6,7,8,9] > largest);
	//assign largest the value of arraynums
	largest = arrayNums[0,1,2,3,4,5,6,7,8,9];
		
}
//printLargest: prints the largest even number in the array
void printLargest(float largest){
	//print on screen, "The largest even number un the array is: " followed by a endline
	cout << "The largest even number in the array is: " <<endl;	
	//print on screen "largest", followed by a linebreak. 
	cout << largest << endl; 
}[code]

[/code]
Line 85 and 87, you have arrayNums[0,1,2,3,4,5,6,7,8,9] and you should have arrayNums[index]
and you need to add check for if it's even.

also in your for loops you can do index++ instead of index = index + 1.

in your caclSmall, you should first set small to first number in array, and then check if any number left in array is smaller than it.
and at line 68 you are always setting the small to arrayNums[0]
Last edited on
closed account (SECMoG1T)
What about using a single function

1
2
3
4
5
6
7
8
9
10
11
12
 void maxmini (float array [], int arraysize, float& largest, float & smallest)
  {
    largest=smallest=array [0];

     for (int count=1; count <arraysize; count++)
          {
             if (largest <array [count])
                 largest=array [count];
             if (smallest> array [count])
                 smallest= array [count];
           }
      }
yea (andy1992) I am having the trouble. Someone refered me to using "size", but im unclear on how to find it and initiate it into my code. haha
print the highest even number in the array.

Ok, here is valid (5-element) input: { 7, 3, 5, -23, 9 }
There are no even numbers in the input, and thus printing even number should print nothing.
From that in turn we can say that you cannot calcLargest, because such function always returns a value.

You could have a void printLargest( float array [], int arraysize ); // print largest even, if there is one

The extra twist is that you have to check for even and remember whether you have found such value at all.
closed account (SECMoG1T)
Ooh my function dint take into account even numbers refer to @keskiverto post you'll get the idea

Someone refered me to using "size", but im unclear on how to find it and initiate it into my code. 

The size of the array is the allocation size used in your case on line 17 arraysize is equal to 10 , you can use it as I have done in that function there. Thing is you want to evaluate all items in your array and there are (arraysize) elements , so you'll have to evaluate arraysize elements , in your case you need to evaluate 10 elements

Although thats the case you aren't limited to using arraysize only

1. You can use pointers ans pointer arthmetics for itetating through an array.
2 you can use library std::begin () and std::end ()
3 you can use range for loops
5 you can use normal iterations while checking a set sentinel value

All this will achieve a similar result
Last edited on
Topic archived. No new replies allowed.