print function for array

hello,I have this program which reads elements from the user for an array of [4][4]. and i made functions to enter elements and find max and find min element. i'm trying to write a 4th functions to sum and print the summation of both min element and max element. but i can't figure it out. any ideas?

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

using namespace std;

int MaxE(int arr[4][4])
{
     int max=arr[0][0];
    for(int i=0;i<4;i++)
      for(int j=0;j<4;j++)
      {
         if(arr[i][j]>max)
         max=arr[i][j];
      }
      return max;
}

int MinE(int arr[4][4])
{
     int min=arr[0][0];
    for(int i=0;i<4;i++)
      for(int j=0;j<4;j++)
      {
         if(arr[i][j]<min)
         min=arr[i][j];
      }
      return min;
}

void read(int arr[4][4])
{
    for(int i=0;i<4;i++)
      for(int j=0;j<4;j++)
      {
          cout<<"Enter element ["<<i+1<<","<<j+1<<"] : ";
          cin>>arr[i][j];
      }
}

void printArray

int main()
{
 int a[4][4],SUM;
 
    cout<<"Please enter the elements of 2D Array "<<endl;
    read(a);
    cout<<endl;
    cout<<"The Sum of Max and Min element is "<< MaxE(a) + MinE(a)<<endl;

    return 0;
}
the way i did the summation is not function clearly .. i need a print function to call it which can sum both max and min and print the result for me
> i need a print function to call it which can sum both max and min and print the result for me
So you figured out
1
2
 if(arr[i][j]>max)
         max=arr[i][j];

and
1
2
if(arr[i][j]<min)
         min=arr[i][j];

but can't grok
 
sum += arr[i][j];

i don't get it :\
1
2
3
4
void printArray( int arr[4][4] )
{
  // do stuff
}

What don't you understand now?

You already know how to iterate through the array, pass an array as a parameter.
I don't get it either. do you want the lowest and highest 2 values added up? You have it, if its not working, I don't see any errors? If you need it in a function, you can do that, just move the print statement you already have into a function.

if you want the sum of everything, you can do that too.

if you want to combine them and find the lowest and highest in a single loop, add & print, its just a little variation on what you already did.
Last edited on
i need to call a funtion to take both max element and min element and sum them together and then print the result all that using one function. i did that with out a function
cout<<"The Sum of Max and Min element is "<< MaxE(a) + MinE(a)<<endl;
i want a void function to do that line .. i just can't tell how .. seriously
Sheesh
1
2
3
4
void printArray( int arr[4][4] )
{
 cout<<"The Sum of Max and Min element is "<< MaxE(a) + MinE(a)<<endl;
}

Happy now?
oh it was that easy lol i'm sorry, i was having an error about calling functions into a 3rd function, i thought it was alot more complicated. sorry once again, cheers bro :)
Topic archived. No new replies allowed.