Using Pointers

Using pointers to find the smallest in a array

Page 1 of 1

4 Replies - 2459 Views - Last Post: 23 March 2010 - 04:27 AM Rate Topic: -----

#1 marcelomg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 10-January 09

Using Pointers

Posted 22 March 2010 - 07:05 PM

I have this code that finds the smallest number in an array:
#include <iostream>
using namespace std;


int main()
{
	int i;
	int smallest;
	int nums[5] = {4, 20, 18, 7, 2};

	smallest = nums[0];
	for (i = 1; i < 5; i++) {
		if (nums[i] < smallest) 
	smallest = nums[i];
	}
	cout << smallest;


	return 0;
}




I'm trying to do the same code but with pointers instead, but something is wrong, I don't know what it is.
Can someone help?
Thanks,

Here it is:
#include <iostream>
using namespace std;

int main()
{

	int* pInt;
	int* pSmallest;
	int nums[5] = {4, 20, 18, 7, 2};

	pSmallest = nums;
	for (*pInt = 1; *pInt < 5; pInt++) {
		if (nums[pInt] < *pSmallest) 
	pSmallest = pInt;
	}
	cout << pSmallest;


 return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Using Pointers

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Using Pointers

Posted 22 March 2010 - 07:14 PM

There's several things amiss, all of which indicate a lack of pointer basics.

how much do you know about them?


The wrong way (see comments):

int main(){
    int* pInt;
    int* pSmallest;
    int nums[5] = {4, 20, 18, 7, 2};

    pSmallest = nums;
    for (*pInt = 1; *pInt < 5; (*pInt)++) { //can't use a pointer like this, you didn't allocate any space
		if (nums[*pInt] < *pSmallest) *pSmallest = nums[*pInt]; //if you did allocate space for it, need to derefernece to get integer value
    }
    cout << *pSmallest << endl; //dereference to get actual value pointed at

	return 0;
}




A possible right way:

int main(){
    int* pSmallest;
    int nums[5] = {4, 20, 18, 7, 2};

    pSmallest = nums; //or &nums[0], same thing
    for (int i = 0; i < 5; i++) { //no need for a pointer
		if (nums[i] < *pSmallest) *pSmallest = nums[i]; //don't forget to derefence!
    }
    cout << *pSmallest << endl; //dereference to get actual value pointed at

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 marcelomg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 10-January 09

Re: Using Pointers

Posted 22 March 2010 - 07:18 PM

Hello KYA,
I'm learning about this stuff now. This was actually an assignment. I had to come up with the first code, which is working fine.

The new code should not have i or smallest at all, just the array nums, and two pointers to int, pInt and pSmallest. No other variables. The code will therefore have no use of array notation...no brackets [], except in the declaration of nums itself. So instead of

smallest = nums[0];

...you’re going to have to use the pSmallest variable to keep track of the location (not value) of the smallest number so far:

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Using Pointers

Posted 22 March 2010 - 07:30 PM

well its just pointer arithmetic then, basically what KYA posted just changing the array notation to pointer notation
#include <iostream>

using namespace std;

int main()
{
    int nums[5] = {4, 1, 10, 7, 2};
    int *index = nums;
    int smallest = *index;

    for (int i = 0; i < 5; i++) 
	if(*(index+i)<smallest) smallest = *(index+i);
           
    
    cout <<smallest;

    cin.ignore();
    cin.get();
    return 0;
}


This post has been edited by ImaSexy: 23 March 2010 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Using Pointers

Posted 23 March 2010 - 04:27 AM

View Postmarcelomg, on 22 March 2010 - 08:18 PM, said:

[i]The new code should not have i or smallest at all, just the array nums, and two pointers to int, pInt and pSmallest. No other variables. The code will therefore have no use of array notation...no brackets [], except in the declaration of nums itself. So instead of


This is too fun not to play. Restrictions are a great challenge.

Here's a working answer along the lines of what I believe is expected. Lots of comments to read.
#include <iostream>

using namespace std;

int main() {
	const int NUM_SIZE = 5; // never have mysery size arrays, const in good
	int *pInt, *pSmallest, nums[NUM_SIZE] = {4, 20, 18, 7, 2};
	
	// init both pointers to the beginning of our array
	pInt = pSmallest = nums;
	
	// watch carefully, we increment pInt first 
	// so it starts at the second element
	// also, we use pointer arithmetic to check for the end of the array
	// it's better to put this in a variable, but I'm playing by the rules
	while(++pInt<(nums+NUM_SIZE)) {
		// compare values
		if (*pInt < *pSmallest) {
			// save pointer value
			pSmallest = pInt;
		}
	}
	
	cout << *pSmallest << endl;

	return 0;
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1