How can I modify this to do what I want it to do? Using pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Write a function that take an int and a pointer to an int and 
// returns the larger of the int value of the value to which the 
// pointer points. What type should you use for the pointer?

#include <iostream>
using std::cout;
using std::cin;

int larger_one(const int i, const int *const p)
{
    return (i > *p) ? i : *p;
}

int main()
{
    int i;
    cin >> i;
    cout << larger_one(i, &i);
    
    return 0;
}




5
5
Last edited on
closed account (z05DSL3A)
Not actually sure what you are asking.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int larger_one (const int i, const int *const p)
{
    return (i > *p) ? i : *p;
}

int main ()
{
    using std::cout;
    using std::cin;

    int i{}, j{ 5 }, *k{ &j };
    cin >> i;
    cout << larger_one (i, k);

    return 0;
}
4
5


8
8
So I am trying to allow the user to input two numbers then have the output be the larger integer value.
closed account (z05DSL3A)
In that case you would just need to read a second value into a different variable, like
1
2
3
4
int i{}, j{};
    cin >> i;
    cin >> j;
    cout << larger_one (i, &j);
Ahh okay I understand what I was missing and was doing wrong. Thank you so much. Plenty of thumbs up. New and improved code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int larger_one(const int i, const int *const p)
{
    return (i > *p) ? i : *p;
}

int main()
{
    int i{}, j{};
    cout << "Please enter two numbers (pressing enter in between) and enter when done: " << endl;
    cin >> i;
    cin >> j;
    cout << "\nThe larger of the two are: " << larger_one(i, &j);
    
    return 0;
}




Please enter two numbers (pressing enter in between) and enter when done:                                             
4                                                                                                                     
5                                                                                                                     
                                                                                                                      
The larger of the two are: 5 
Topic archived. No new replies allowed.