Reilly Parker Reilly Parker - 1 month ago 11
C++ Question

Array Function. Would appreciate a little clarification

I have a question regarding a school lab assignment and I was hoping someone could clarify this a little for me. I'm not looking for an answer, just an approach. I've been unable to fully understand the books explanations.


Question: In a program, 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 .


This is what I have right now:

/*
Programmer: Reilly Parker
Program Name: Lab14_LargerThanN.cpp
Date: 10/28/2016
Description: Displays values of a static array that are greater than a user inputted value.
Version: 1.0
*/

#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 Value:" << endl;
cin >> n;

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

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;

}
}
}

Answer

For my whole answer I assume that this:

Question: In a program, 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 .

is the whole assignment.

  1. void arrayFunction(int[], int, int); is probably the only thing you could write. Note however that int[] is in fact int*.

  2. As others pointed out don't bother with receiving input. Use something along this line: int numbers[] = {2,4,8,5,7,45,8,26,5,94,6,5,8};. It will create static array for you;

  3. You have parameter int n but you never use it.

  4. You are trying to send variable to the function arrayFunction but I can't see definition of this variable!

  5. Use something called rubber duck debugging (google for it :) ). It will really help you.

If you have some more precise question, ask them.

As a side note: there are better ways of sending an array to the function, but your assignment forces you to use this old and not-so-good solution.

Would you use an if else statement? I've edited my original post with the updated code.

You have updated question, then I update my answer.

First and foremost of all: do indent your code properly!!!
If you do that, your code will be much cleaner, much more readable, and it will be much easier understandable not only for us, but primairly for you.

Next thing: do not omit braces even if they are not required in some context. Even experienced programmers only rarely omit them, so as a beginner you should never do so (as for example with your for loop).

Regarding if-else statement the short answer is: it depends.
Sometimes I would use if (note: in your case else is useless). But other times I would use ternary operator: condition ? value_if_true : value_if_false; or even a lambda expression.
In this case you should probably settle for an if, as it will be easier and more intuitive for you.

Comments