hi
i am new here and i registered because i couldn't find the answer in google search
my problem is that i could not find max and min of two arrays and the average too

#include<iostream>
using namespace std;
int array1[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int array2[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int a;
int print_menu()
{            
    int choice;
    cout<<"#----------------------------------\n";
    cout<<"# Manipulation of arrays          #\n";
    cout<<"#                                 #\n";
    cout<<"# BY MAFD                         #\n";
    cout<<"#----------------------------------\n";
    cout<<"# 1. Read Array 1                 #\n# 2. Read Array 2                 #\n";
    cout<<"# 3. Max_min_two_arrays           #\n# 4. sum_two_arrays               #\n";
    cout<<"# 5. Average_two_arrays           #\n# 6. Quit                         #\n";
    cout<<"#----------------------------------\n";
    cout<<"# Enter your Choice (1-6) :";
    cin>>choice;
    
    return choice;
    }
    void print_array(int[],int)
    {int number;
     cout<<"\nInitialization Of Array "<<a<<endl;
         for (int i=1 ; i<=10 ; i++){
         cout<<"Enter integer "<<i<<" : ";
         cin>>number;
         array1[i]= number;}
         }
     void read_array(int[],int)
     {cout<<"\n\nContent of array "<<a<<" : ";
         for (int i=1 ; i<=10 ; i++)
         cout<<array1[i]<<" ";
         cout<<"\n-------------------------\n";
         }
     void max_min_two_arrays(int[],int[])
     {  
        int max=array1[0]; 
        int min=array1[0]; 
        cout<<"\n\nContent of array 1 : \n";
        cout<<array1<<endl;
     
        for (int i=1 ; i<=10 ; i++)
        {if (array1[i]>max)
        {
        max=array1[i];
        }
        else if (array1[i]<min)
        {
        min=array1[i];     
        }
        }
        }
        
            
int main(){
   int choice; 
   do{
    
    system("cls");
    choice = print_menu();      
    
    switch (choice){
    case 1 :
         a=1;
         print_array(array1,1);
         read_array(array1,1);
         break;
     case 2 :
         a=2;
         print_array(array2,2);
         read_array(array2,2);
         break;
     case 3 :
         max_min_two_arrays; 
         break; 
     case 6 :
         cout<<"\nThanks and by... MAFD\n"<<endl;
         break;
         default:
         cout<<"*** Error: The choice must be 1 to 6 \n";  
         }
         system("pause");
         }
         while(choice!=6);
   
   return 0;
    }

Dani AI

Generated

As pointed out, the line max_min_two_arrays; does not call a function — parentheses and arguments are required. As noted, C++ arrays are zero‑based (use i < 10, not i <= 10). ’s Post #4 correctly finds min/max by hand, but it duplicates logic for the two arrays and mixes global state with I/O. A more professional, maintainable approach is to write small, reusable functions that operate on a container and return results; then combine those results for the two arrays.

A concise, safe modern example (uses <algorithm> and <numeric>):

#include <vector>
#include <algorithm>
#include <numeric>
#include <stdexcept>

std::pair<int,int> minmax_vec(const std::vector<int>& v) {
    if (v.empty()) throw std::invalid_argument("empty vector");
    auto mm = std::minmax_element(v.begin(), v.end());
    return { *mm.first, *mm.second };
}

double average(const std::vector<int>& v) {
    if (v.empty()) return 0.0;
    long long sum = std::accumulate(v.begin(), v.end(), 0LL);
    return static_cast<double>(sum) / v.size();
}

Use these to get each array’s min/max and average, then compute overall min/max with std::min/std::max. Benefits: single-pass min+max via std::minmax_element, robust empty checks, 64‑bit sum to avoid overflow, and no magic indices.

Practical tips: read/write arrays with loops using i = 0; i < n; ++i or range-based for/iterators; pass size explicitly for C-style arrays or use std::vector/std::array; avoid system("cls")/system("pause") for portability; validate user input; and keep I/O separate from logic (functions should return values, not print). These changes make the code clearer, safer, and easier to extend.

Recommended Answers

All 3 Replies

I see two major problems with the min/max code as it is now. First, when you attempt to call the function max_min_two_arrays , you did not give it's arguments; as a result, the function isn't actually getting called (it simpley returns the pointer to the function, which, since nothing is assigned on that line, goes into the ether), so the line of code doesn't have any effect.

The second is that, in the function itself, once you get the max and min values, you don't do anything with them.

Your i variable you use in your for loops should not exceed 9:

for(int i = 1; i < 10; i++)   // Not i <= 10 !
// ...
cout<<"\n\nContent of array 1 : \n";
cout<<array1<<endl;

This won't work. Print each value seperately instead, using again a for loop.

thank you Schoil-R-LEA and mikrosfoititis for trying to help me

but i fix the problem using this way

int min1,max1,max2,min2;
        int i;
 
       for(i=0;i<10;i++)
       {
       array1[i];
       }
       min1=array1[0];
       max1=array1[0];
       for(int i=0;i<10;i++)
	   {
	   if(min1>array1[i])
	   {
        min1=array1[i];
       }
       else if(max1<array1[i])
	   {
	    max1 = array1[i];
	   }
	}
      for(i=0;i<10;i++)
      {
       array2[i];
      }
      min2=array2[0];
      max2=array2[0];
      for(int i=0;i<10;i++)
	  {
      if(min2>array2[i])
      {
      min2=array2[i];
      }
      else if(max2<array2[i])
      {
      max2 = array2[i];
      }
  }  
    if(max1>max2)
    cout<<"\nMax_of_the_two_arrays = "<<max1<<endl;
    else
    cout<<"\nMax_of_the_two_arrays = "<<max2<< endl;
    if(min1<min2)
    cout<<"Min_of_the_two_arrays = "<<min1<<endl<<endl;
    else
    cout<<"Min_of_the_two_arrays = "<<min2<<endl<<endl;

but is there another way to solve it more Professionalism :)

and thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.