5 Replies - 977 Views - Last Post: 21 September 2012 - 11:29 AM Rate Topic: -----

#1 JMurray308   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-September 12

pointer notation in sorting?

Posted 21 September 2012 - 02:18 AM

//i am failing and getting errors with pointer notation in this program and it has been pointed out that
//search list is sposed to work with pointer notation

//This program demonstrates a linear search to verify lottery numbers present or not present in a
//in ten tickets.
#include <iostream>
using namespace std;

//function prototypes


int main()
{   
     int searchList(int [], int, int);
	 const int SIZE = 10;

	int results, value;   //declare variables
	int number[SIZE] = {13579, 26791, 26792, 33445, 55555,62483, 77777, 79422, 85647, 93121};
	int *numPtr;
	//declare fixed array of chosen tickets

	numPtr = number;

	cout << "The numbers in number array are:\n";
	for (int index = 0; index < SIZE; index++)
	{
		cout << *numPtr << " ";
	    numPtr++;
	}

	cout << "Please enter the ticket number:    " <<endl;  //get user input
	cin >> value;
	results = searchList(number, SIZE, value);   //perform search list
	

	
	if (results == -1)
		cout << "You did not win the lottery.\n";  //declare winner
	else
	{
		cout << "You won the lottery";
		cout << (results + 1) << ".\n";
	}
	return 0;
}




int searchList(int list[], int SIZE, int value)  //declare function header
{
	int *numPtr = list;   //declare variables
	int position = -1;
	bool found = false;

	while (numPtr <= &list[SIZE-1]) //establish while loop
	{
		if(*numPtr == value)
		{
			found = true;
			position = *numPtr;
		}
		numPtr++;
	}
	return position;
}



















This post has been edited by jimblumberg: 21 September 2012 - 05:48 AM
Reason for edit:: Took question out of the code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: pointer notation in sorting?

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: pointer notation in sorting?

Posted 21 September 2012 - 05:40 AM

Please post help questions in the C++ forum, not the challenge forum.

If you are getting compiler errors, post the error messages exactly as they appear in your development environment. Otherwise please ask specific questions about the code you posted. Why are you failing?


Jim

This post has been edited by jimblumberg: 21 September 2012 - 05:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: pointer notation in sorting?

Posted 21 September 2012 - 06:51 AM

In your searchList, you have a found flag but don't really use it. You're loop just checks for end, why not loop for SIZE and maybe check that flag. When you find the value, you assign the VALUE to position?!? You really want to assign the current position to position. Time to rethink this a little.

I'd start with:
// I'd forget list as an argument
// it might just be confusing you
// int searchList(int list[], int SIZE, int value) {
int searchList(int *numPtr, int SIZE, int value) {
	// let's just loop for the number of elements in the list
	for(int pos=0; pos<SIZE; pos++) {
		// your code here
	}
	// if you made it this far, you didn't find anything
	// so, this value will always be fail
	return -1;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 JMurray308   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-September 12

Re: pointer notation in sorting?

Posted 21 September 2012 - 08:39 AM



//This program demonstrates a linerar search to verify lottery numbers present or not present in a 
//in ten tickets and i can not get it to switch over to pointer notation...

#include <iostream>
using namespace std;

//function prototypes


int main()
{   
     int searchList(int [], int, int);   //declare variables
	 const int SIZE = 10;

	int results, value;   //declare variables
	int number[SIZE] = {13579, 26791, 26792, 33445, 55555,62483, 77777, 79422, 85647, 93121};
	int *numPtr;
	//declare fixed array of chosen tickets

	numPtr = number;


	cout << "Please enter the ticket number:    " <<endl;  //get user input
	cin >> value;
	results = searchList(number, SIZE, value);   //perform search list
	

	
	if (results == -1)
		cout << "You did not win the lottery.\n";  //declare winner
	else
	{
		cout << "You won the lottery";
		cout << (results + 1) << ".\n";
	}
	return 0;
}




int searchList(int list[], int SIZE, int value)  //declare function header
{
	int *numPtr = list;   //declare variables
	int position = -1;
	bool found = false;

	while (numPtr <= &list[SIZE-1]) //establish while loop
	{
		if(*numPtr == value)   //check for match
		{
			found = true;
			position = *numPtr;
		}
		numPtr++;   //increment numPtr
	}
	return position;
}


















Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: pointer notation in sorting?

Posted 21 September 2012 - 10:24 AM

You would need to post your observations / error messages.

Because it does exactly what I would expect from looking at the code.
$ g++ foo.cpp
$ ./a.out 
Please enter the ticket number:    
55555
You won the lottery55556.


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: pointer notation in sorting?

Posted 21 September 2012 - 11:29 AM

What do you mean by "pointer notation in sorting"
Are you looking to change:
int searchList(int list[], int SIZE, int value)


to
int searchList(int * list, int SIZE, int value)


or
int * searchList(int * list, int SIZE, int value)


?

Also, your title has the word "sorting", but you only seem to be searching.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1