Help with this C++ question

closed account (1hqj1hU5)
1. Write a C++ function prototyped as

unsigned int countLargerThan( int* arrayValuesToCheck, unsigned int sizeToCheck, int minimumValue );

When the first four values in the array are 5, 32, -12, 53, sizeToCheck is 4, and minimumValue is 5, the function will return 2.

You may not use indexes or [] within this function. You must use pointer arithmetic to cycle through the array elements along with the * and the & operators.

2. Write a C++ main function to test your countLargerThan() function. You may use [] notation when you declare the array.


What I have so far...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;


unsigned int countLargerThan( int* arrayValuesToCheck, unsigned int sizeToCheck, int minimumValue );

int main()
{

int arrayValuesToCheck[] = {5,32,-12,53};
unsigned int sizeToCheck = 4;
int minimumValue = 5;

    return 0;
}


unsigned int countLargerThan( int* arrayValuesToCheck, unsigned int sizeToCheck, int minimumValue )
{
    
}
Last edited on
Since you need to use pointer arithmetic have a look here:
https://www.tutorialspoint.com/cplusplus/cpp_pointer_arithmatic.htm
closed account (1hqj1hU5)
"The function will return 2 " ? what's that mean
If you have this array {5,32,-12,53}; and the minimum value is 5 then
2 is the number of values greater than 5
Sheesh. The assignment may as well read "do this the hard way instead of the easy way."

The code below walks through the array with a pointer. Note the limit of the for loop (arrayValuesToCheck+sizeToCheck). This works because pointer arithmetic is always scaled by the size of the thing pointed to. So if you have int *p then p+2 does not refer to two bytes after p, it refer to two integers after p. In other words, to compute p+2, the compiler generates code to to compute p + 2*sizeof(int).
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
#include <iostream>
using namespace std;

unsigned int countLargerThan( int* arrayValuesToCheck, unsigned int sizeToCheck, int minimumValue );

int main()
{
    int arrayValuesToCheck[] = {5,32,-12,53};
    unsigned int sizeToCheck = 4;
    int minimumValue = 5;

    std::cout << countLargerThan(arrayValuesToCheck, sizeToCheck, minimumValue) << '\n';
    return 0;
}

unsigned int countLargerThan( int* arrayValuesToCheck, unsigned int sizeToCheck, int minimumValue )
{
    unsigned int result = 0;	// I always use "result" for the result of a function.

    for (int *p = arrayValuesToCheck; p < arrayValuesToCheck+sizeToCheck; ++p) {
	if (*p > minimumValue) {
	    ++result;
	}
    }
    return result;
}

closed account (1hqj1hU5)
I got it! Thanks for the clarification.

I did something similar
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
#include <iostream>
using namespace std;


unsigned int countLargerThan( int* arrayValuesToCheck, unsigned int sizeToCheck, int minimumValue );

int main()
{

int arrayValuesToCheck[] = {5,32,-12,53};
unsigned int sizeToCheck = 4;
int minimumValue = 5;


cout << "Result is " << countLargerThan(arrayValuesToCheck,4,5);

    return 0;
}


unsigned int countLargerThan( int* arrayValuesToCheck, unsigned int sizeToCheck, int minimumValue )
{
    unsigned int result = 0;
    int *p = arrayValuesToCheck;

    for(int i = 0 ; i < sizeToCheck ; i++)
    {
        if(*p > minimumValue)
        {
            result++;
        }
        p++;

    }
    return result;

}

Last edited on
Topic archived. No new replies allowed.