-3

The programming challenge I am trying to solve is states as: write a function that accepts three arguments: an array, the size of the array, and a number n. Assume that the array contains integers. The function should display all of the numbers in the array that are greater than the number n.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
void largerThanN(int a[], int size, int n);
int main()
{
    int size   = 3, 
    n      = 1, 
    arr[3] = { 0,5,6 };
    largerThanN(arr, size, n);
}

void largerThanN(int a[], int size, int n) 
{
    for (int i = 0; i < size; i++) 
    {
        if (a[i] > n) 
        {
            cout << a[size] << " is greater than n " << endl;
        }
    }
}

This is what I have convoluted so far but I don't know if this is even remotely close to solving the challenge, maybe some direction from pro C++ devs would help out.

EDIT: I've figured it out with some help from you guys in the comments, thank you guys for helping a newbie out. it means a lot.

  • 1
    `stdafx.h` is a non-standard header. SO is not a do-my-homework service. Read [more](http://en.cppreference.com/w/cpp) about C++, use its standard [containers](http://en.cppreference.com/w/cpp/container). Compile with all warnings and debug info, and use the debugger. The signature of `largerThanN` is wrong, that function should give a `bool` or maybe a `void` (and then it is badly named) – Basile Starynkevitch Dec 27 '17 at 12:45
  • 1
    @OP Why are you accepting user-input with `cin`? From the problem description the int[] array (which you have not named in `largerThanN`) should contain all values you are meant to test against – wakey Dec 27 '17 at 12:57
  • 2
    You might want to grab a [good C++ book](https://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO Dec 27 '17 at 13:01
  • Maybe you should learn to read and understand a simple text before trying to implement it. –  Dec 27 '17 at 13:33
  • Rather than using a C-style array and having to pass the size, any sane person would instead use a [std::vector](http://en.cppreference.com/w/cpp/container/vector) (or a [std::array](http://en.cppreference.com/w/cpp/container/array) if the size is known at compile time). – Jesper Juhl Dec 27 '17 at 14:57
  • @manni66 i'm fully coherent that I don't understand c++ fully yet, i dont understand why you have to be so toxic about it. – Roosevelt Mendieta Dec 27 '17 at 16:27
  • You don’t have a problem with C++, you have a problem with reading. –  Dec 27 '17 at 17:01

2 Answers2

2

You can do something like this-

#include <iostream>
using namespace std;
void largerThanN(int[], int size, int n);

int main()
{
    int size;
    cin >> size;
    int a[size];
    for(int i=0; i < size; i++)
        cin >> a[i];
    int n;
    cin >> n;
    largerThanN(a, size, n);
    return 0;
}

void largerThanN(int a[], int size, int n)
{
    for(int i = 0; i < size; i++)
    {
        if(a[i] > n)
            cout << a[i] << endl;
    }
}
Mahmudur Rahman
  • 640
  • 1
  • 6
  • 23
Arnab Roy
  • 619
  • 5
  • 16
  • I hacked at it a little bit more and had the same exact thing for the function, but what you did in main() really helped me fully understand how to pass values to functions. thank you @arnab – Roosevelt Mendieta Dec 27 '17 at 16:24
0
#include "stdafx.h"
#include <iomanip>
#include <iostream> 
using namespace std; 
double largerThanN(int[], int size, int n);

int main() {
    int arr_size;    // array size
    cin >> arr_size;
    vector<int> arr(n);
    cout << "Enter values: ";
    for (int i=0; i<n; i++)
        cin >> arr[i];
    int n;
    cin >> n;       // the no. to search for
    largerThanN(arr, arr_size, n);
    return 0; }

void largerThanN(int[], int size, int n) {
    int index;
    for (index = 0; index < size; index++) 
    {
        if (values[index] > n)
            cout << values[n] << endl;
    } }