Need help!!

I need to find the maximum and minimum values of an array usings function with there prototypes and i dont whats the deal.

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
#include<iostream>
#include<iomanip>

using namespace std;

void min_element(int array[], int val);  //function prototypes
void max_element(int array[], int val);


int main(void)
{
    int x = 0;
    int myarray[100];
    int val = 0;
    int mymin, mymax;

    cout << "Enter the numbers of elements in the array: " << endl;
    cin >> x;

    if (x >= 100)
    {
        cout << "sorry.  too large.  100 max. "<<endl;
        return 1;
    }

    for(int i = 0;i < x;i++)
    {
        cout << "Enter the element No. " << i + 1  << "of the array ";
        cin >> val;
        myarray[i] = val;
    }

    mymin = min_element(myarray, x);
    mymax = max_element(myarray, x);
    


	cout << "The minimum element of the array is " << mymin << endl;

    cout << "The maximum element of the array is " << mymax << endl;

#ifdef WIN32
    system("pause");
#endif
    return 0;
}


	void min_element(int array[], int val);  //functions
	void max_element(int array[], int val);
{
		
		for (i = 0; i < val; i++)
	{
    if (array[i] < min )
    {
        min = array[i];
    }
    else if (array[i] > max )
    {
        max = array[i];
    }
}
Last edited on
put the code inside [code] tag to make it readable
alright im new to this but its readable now codder101
1
2
3
4
5
6
7
// void min_element(int array[], int val); 

// return the largest element
// const int array[]. const - we do not need to modify it
// sz == size of the array (number of elements in it)
// assumes that sz is positive (greater than zero)
int max_element( const int array[], int sz ) ; // declaration 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int max_element( const int array[], int sz ) // definition
{
     // initially, let the max element be the element right at the beginning
     int largest_so_for = array[0] ;

     // for every element starting at position one up to the end
     for( int i = 1 ; i < sz ; ++i )
     {
          if( array[i] > largest_so_for ) // if it is larger than the largest so far
                largest_so_for  = array[i] ; // it becomes the largest now  
     }  

     // we have gone through the entire array; 
     // largest so far is the largest element in the array

     // return it so that the caller of the function will get it as the result
     return largest_so_for  ;
}


And likewise for int min_element( const int array[], int sz )


I wrote this
program for a girl
yesterday!

have some extra functions
but may help like an example:

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
//ProcessArrayData.cpp
//#

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void printArray(const int a[],int size,const int NUM_PER_LINE); //function prototypes
void setArray(int a[],int size);
int getSumArray(const int a[],int size);
int getSmallestArrayElement(const int a[],int size);
int getHighestArrayElement(const int a[],int size);
double getAverageArrayElements(const int a[],int size);


int main(){

const int MAX=12;
const int NUM_PER_LINE=6;

int array[MAX]={}; //array elements initialized to 0

cout<<"Enter "<<MAX<<" numbers\n"<<endl;
setArray(array,MAX);
cout<<"\nArray elements are:\n";
printArray(array,MAX,NUM_PER_LINE);
cout<<"Sum: "<<getSumArray(array,MAX)<<endl;
cout<<"Average: "<<getAverageArrayElements(array,MAX)<<endl;
cout<<"Highest: "<<getHighestArrayElement(array,MAX)<<endl;
cout<<"Smallest: "<<getSmallestArrayElement(array,MAX)<<endl;



return 0; //indicates success
}//end main

void printArray(const int array[],int size,const int NUM_PER_LINE){
	for(int index=0;index<size;index++){
		cout<<array[index]<<((index+1)%NUM_PER_LINE==0?'\n':' ');
	}//end for
cout<<endl; //new line		
}//end function printArray

void setArray(int array[],int size){
	for(int index=0;index<size;index++){
		cout<<"Enter value #"<<index+1<<':';
		cin>>array[index];
	}//end for
}//end function setArray

int getSumArray(const int array[],int size){
	int sum=0;
	for(int index=0;index<size;index++)
		sum+=array[index];

return sum;
}//end function getSumArray

double getAverageArrayElements(const int array[],int size){

double average=0;
int sum;

	sum=getSumArray(array,size);
	average=static_cast<double>(sum)/size;

return average;
}//end function getAverageArrayElements

int getSmallestArrayElement(const int array[],int size){

int smallest=array[0];
	for(int index=1;index<size;index++)
			if(array[index]<smallest) smallest=array[index];

return smallest;
}//end function getSmallestArrayElement

int getHighestArrayElement(const int array[],int size){

int highest=array[0];

	for(int index=1;index<size;index++)
		if(array[index]>highest) highest=array[index];

return highest;
}//end function getLargestArrayElement  



Eyenrique-MacBook-Pro:Help Eyenrique$ ./ProcessArrayData 
Enter 10 numbers

Enter value #1:1
Enter value #2:2
Enter value #3:3
Enter value #4:4
Enter value #5:5
Enter value #6:6
Enter value #7:7
Enter value #8:8
Enter value #9:9
Enter value #10:10

Array elements are:
1 2 3 4 5 6
7 8 9 10 
Sum: 55
Average: 5.5
Highest: 10
Smallest: 1


EDIT: was for a girl
Last edited on
Topic archived. No new replies allowed.