larger than n

alright fellow programmers turns out my reading comp skills suck because I came to class thinking I was done and I was sorely mistaken lol

the book asks:
create a program with a function that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and the integer "n." The function should display all of the numbers in the array that are greater than "n."

teacher instructions:
1. limit of 20 variables in the array.
2. no data verification needed.

my problem:
I came in there with a program that ask the user for n but nothing else not reading that user is supposed to tell you the array size to so therefor this has got me all confused. any help would be truly appreciated.

my program thus far:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void arrayFunction(int[], int, int); // Prototype for arrayFunction. int[] = array, int = size, int = n

int main()
{
int n; // Initialize user inputted value "n"
cout << "Enter a value between 1 to 25 :" << endl;
cin >> n;

const int size = 20; // Constant array size of 20 integers.
int arrayNumbers[size] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; // 20 assigned values for the array

arrayFunction(arrayNumbers, size, n); // Call function
return 0;
}


void arrayFunction(int arrayN[], int arrayS, int number) // Function Definiton
{
for (int i = 0; i<arrayS; i++)
{
if (arrayN[i] > number)
{

cout << arrayN[i] << " ";
cout << endl;

}
}
}

thanks in advance...
Using std::stable_partition to maintain the relative ordering of the elements (std::partition could also do the job but would not retain the relative ordering of the original elements):
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
#include <iostream>
#include <algorithm>
#include <iterator>

static int arrayN[] = { 1,3,2,4,5,19,7,8,6,16,11,12,13,14,15,10,17,18,8,20 };

void arrayFunction(int n[], size_t arraySize, const int divider)
{
    auto itr = std::stable_partition(n, n+arraySize, [&divider](int n){return n > divider;});
    //http://en.cppreference.com/w/cpp/algorithm/stable_partition
    //http://en.cppreference.com/w/cpp/algorithm/partition
    if (itr == n)
    {
        std::cout << "No array element is greater than the divider \n";
    }
    else
    {
        std::cout << "The array elements greater than " << divider << "\n ";
        std::copy(n, itr, std::ostream_iterator<int>(std::cout, " "));
        //http://en.cppreference.com/w/cpp/algorithm/copy
        std::cout << "\nThe array elements less than or equal to " << divider << "\n";
        std::copy (itr, n+arraySize, std::ostream_iterator<int>(std::cout, " "));
    }
}
int main()
{
    arrayFunction(arrayN, 20, 9);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
// note: const int array[] (let us be const-correct)
// the function does not need to (and it should not) modify the contents of the array
// we'll also give it a name that conveys more meaning than 'arrayFunction'
void display_filtered( const int array[], int size, int n )
{
    std::cout << "numbers greater than " << n << " are: " ;

    // for each number in the array: if it is greater than 'n', display it (with space as a separator)
    for( int i = 0 ; i < size ; ++i ) if( array[i] > n ) std::cout << array[i] << ' ' ;

    std::cout << '\n' ; // finally, print a new line
}


Example usage:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    const int size = 20; 
    const int array[size] = { 1,3,2,4,5,19,7,8,6,16,11,12,13,14,15,10,17,18,8,20 };

    int n;
    std::cout << "Enter a value between 1 to 25 : " ;
    std::cin >> n;

    display_filtered( array, size, n ) ;
}
jlborgus i don't see where yours does any different than mine. i need the users input on array size according to my instructor. unless im missing something?

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void arrayFunction(const int array[], int size, int n) // Prototype for arrayFunction.
{

std::cout << "numbers greater than " << n << " are: ";

// for each number in the array: if it is greater than 'n', display it (with space as a separator)
for (int i = 0; i < size; ++i) if (array[i] > n) std::cout << array[i] << ' ';

std::cout << '\n';

}

int main()
{

const int size = 20; // Constant array size of 20 integers.
const int arrayNumbers[size] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; // 20 assigned values for the array

int n;
std::cout << "Enter a value between 1 to 25 : ";
std::cin >> n;

arrayFunction(arrayNumbers, size, n); // Call function
return 0;
}

/****************************************************************************************
* *Description of code below:* *
* The For statement scans each variable, if the array values are greater than the *
* variable "n" inputted by the user the output is only those values greater than "n." *
****************************************************************************************/

void arrayFunction(int arrayN[], int arrayS, int number) // Function Definiton
{
for (int i = 0; i<arrayS; i++)
{
if (arrayN[i] > number)
{

cout << arrayN[i] << " ";
cout << endl;

}
}
}
I'm learning c++ at robotics right now... idk what to do to help you, have not learned all the material you are using yet. I'm sorry i wish i could help... and also, do u happen to go by the username "kohltastrophe"??? on any websites/games???
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
#include <iostream>

void display_filtered( const int array[], int size, int n )
{
    std::cout << "numbers greater than " << n << " are: " ;

    // for each number in the array: if it is greater than 'n', display it
    for( int i = 0 ; i < size ; ++i ) if( array[i] > n ) std::cout << array[i] << ' ' ;

    std::cout << '\n' ; // finally, print a new line
}

void fill( int array[], int size )
{
    std::cout << "enter " << size << " numbers in the array: " ;
    for( int i = 0 ; i < size ; ++i ) std::cin >> array[i] ;
}

void display( const int array[], int size )
{
    std::cout << "the numbers in the array are: " ;
    for( int i = 0 ; i < size ; ++i ) std::cout << array[i] << ' ' ;
    std::cout << '\n' ;
}

int main()
{
    const int MAX_SIZE = 20;
    int array[MAX_SIZE] = {} ;

    int size ;
    std::cout << "Enter the number of elements in the array [1-" << MAX_SIZE << "]: " ;
    std::cin >> size ;
    if( size > MAX_SIZE ) size = MAX_SIZE ;
    if( size < 1 ) size = 1 ;

    fill( array, size ) ;
    display( array, size ) ;

    int n;
    std::cout << "Enter a value: " ;
    std::cin >> n;

    display_filtered( array, size, n ) ;
}
i think that is it! thank you so much for your help!
Topic archived. No new replies allowed.