input Max and Min in array

I can't seem to figure out a way to implement a max and minimum to an array. where it will only output numbers between the max and min. any help will be highly appreciated.

// Project8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <ctime>

using namespace std;


const unsigned int ARRAY_SIZE = 10;

// ============
int main() {

char GoAgain;
int sum = 0;

int a[ARRAY_SIZE];

do {

for (int ii = 0; ii < ARRAY_SIZE; ii++) {

a[ii] = rand() % 50;
cout << "a[" << a[ii] << "]" << endl;


}
int ii = 0;
while (ii < ARRAY_SIZE) {
sum = sum + a[ii];
ii++;

}//while loop
cout << "The sum ==> " << sum << endl;

cout << "would you like to go again y/n ? " << endl;
cin >> GoAgain;

} while (GoAgain == 'y');




return 0;
}


Here's one way to do it. Maybe someone can improve it or suggest a better way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// int (&arr)[ARRAY_SIZE] -> pass array by reference
// so it doesn't decay into a pointer -> can't use sort/find/find_end
// if it is a pointer
// not sure on the above, maybe someone can correct me
void ranged_print( int (&arr)[ARRAY_SIZE], int low, int high )
{
    sort( begin( arr ), end( arr ) );
    auto start = find( begin( arr ), end( arr ), low ), 
        fin_ = find( begin( arr ), end( arr ), high ),
        // find the last occurrence of high, in case of duplicates
        fin = find_end( begin( arr ), end( arr ), fin_, fin_ + 1 ) + 1;
        
    // prints [start, fin), hence the + 1 on fin
    while( start != fin ) {
        cout << *start << ' ';
        start++;
    }
}

int main( )
{
    int arr[ARRAY_SIZE]{ 1, 4, 3, 5, 9, 7, 6, 10, 8, 4 };
    ranged_print( arr, 3, 5 );
}


3 4 4 5  
Last edited on
If I understand the problem correctly, you just need to print all the elements in the array that are between a min value & a max value? Sorting might be a little overkill for a short array, but you're likely to find that sorting before-hand (if order doesn't matter) will improve performance.

You can pass a pointer to std::sort
1
2
3
void do_sort(int *arr, int size){
std::sort(arr, arr + size);
}

You shouldn't need to pass an array by reference, since passing an array as a parameter is really just passing a pointer to the 0th element.
You could try something like
1
2
3
4
5
6
7
8
void print_between_min_max(int *_arr, int size, int min, int max)
{
  for (int i = 0; i < size; ++i) {
    if (_arr[i] >= min && _arr[i] <= max)
      std::cout << _arr[i] << " ";
  }
  std::cout << std::endl;
}

Of course, it won't print in order. It'll print "4 3 5 4" for the array integralfx gave above.
it did not make since to me at first but what I'm asking is. The program will ask for a minimum and a maximum value. You input you're choice then it will output an array sequence between those two integers.
So, it should print the values or the indices?
Does the sequence have to be contiguous in the array or does it print all the entries that are between the given min and max values?
the min and max are constants so everything printed should be integers in between the min and max
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool out_of_range( int a )
{
    return a < MIN || a > MAX;
}

void ranged_print( int arr[ARRAY_SIZE] )
{
    for( int i{}; i < ARRAY_SIZE; i++ ) {
        if( !out_of_range( arr[i] ) ) 
            cout << arr[i] << ' ';
    }
}

int main( )
{
    int arr[ARRAY_SIZE]{ 1, 4, 3, 5, 9, 7, 6, 10, 8, 4 };
    ranged_print( arr );
}


4 3 5 4  
// Project8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <ctime>

using namespace std;

void MaxMin(int& );


const unsigned int ARRAY_SIZE = 10;

// ============
int main() {

char GoAgain;
int sum = 0;

int a[ARRAY_SIZE];
void MaxMin();

do {

for (int ii = 0; ii < ARRAY_SIZE; ii++) {

a[ii] = rand() % 50;
cout << "a[" << a[ii] << "]" << endl;


}
int ii = 0;
while (ii < ARRAY_SIZE) {
sum = sum + a[ii];
ii++;

}//while loop
cout << "The sum ==> " << sum << endl;

cout << "would you like to go again y/n ? " << endl;
cin >> GoAgain;

} while (GoAgain == 'y');



return 0;

}// End of main()

// ==============================================================
void MaxMin(int& ) {

int max, min, i;
cout << "enter two numbers " << endl;
cout << "Enter Max Number " << endl;
cin >> max;
cout << "Enter Min Number " << endl;
cin >> min;

if (max <= min) {
for (i = max; i <= min; i++) {
cout << i << endl;
}
}
else
{
for (i = min; i <= max; i++) {
cout << i << endl;
}
}




}








I still haven't really mastered the way of using prootoypefunctions and their refrences. but I want my program to ask for a min and max and then output a sequence of arrays just between the two numbers . I coded some of it but I need some help on how to get it to compile and run.
Topic archived. No new replies allowed.