Linear Search Problem

hello eveyone

i'm making an Inventory for a game and i'm having a problem.
what i want is to search the array for NULL and if it finds NULL change it from NULL to 'T', now the problem is that if it finds more than 1 NULL it will change all NULLs to 'T' but that is not what i want, what i want is to change only the first NULL it finds to 'T'.

example
array[10]={NULL,NULL,NULL,'R','M',NULL,'S',NULL,NULL,P'}

Search for the first NULL and get the index, so first NULL index = 0, so change index 0 to NULL;

so it will look like this
array[10]={'T',NULL,NULL,'R','M',NULL,'S',NULL,NULL,P'}

NOT LIKE THIS
array[10]={'T','T','T','R','M','T','S','T','T',P'}

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
#include <iostream>

using namespace std;

int main()
{
	char Inventory[10]={'A',NULL,'B','C',NULL,'D','E','F','G','H'};

	for(int x=0; x<=9; x++)
		cout<< Inventory[x] << endl;

	cout << "------------------------------" << endl;
	int FistNum=0, LastNum=9;
	for(int x=FistNum; x<=LastNum; x++)
	{
		if(Inventory[x]==NULL)
		{
			Inventory[x]='T';
		}
	}

	for(int x=0; x<=9; x++)
		cout<< Inventory[x] << endl;

	system("PAUSE");
	return 0;
}
Here are two ways you can stop at the first replacement:
1
2
3
4
5
6
7
8
9
10
	bool done = false;
	int FistNum=0, LastNum=9;
	for(int x=FistNum; !done && x<=LastNum; x++)
	{
		if(Inventory[x]==NULL)
		{
			Inventory[x]='T';
			done = true; // terminate loop condition
		}
	}

And using break:
1
2
3
4
5
6
7
8
9
	int FistNum=0, LastNum=9;
	for(int x=FistNum; x<=LastNum; x++)
	{
		if(Inventory[x]==NULL)
		{
			Inventory[x]='T';
			break; // exit loop
		}
	}

The first method is useful if, after the loop ends, you need to know if the replacement happened or not. Otherwise the second is shorter.
thank you so much :)
I have one question ,


In your array char Inventory[10]={'A',NULL,'B','C',NULL,'D','E','F','G','H'};

where NULL is a pointer , so when i compiled your code in my g++
it generated many warnings as below :

1
2
3
r.cpp:7:63: warning: converting to non-pointer type ‘char’ from NULL
r.cpp:7:63: warning: converting to non-pointer type ‘char’ from NULL
r.cpp:21:20: warning: NULL used in arithmetic


Did you get any sort of similar warnings or is it compiler specific ?
Are there any solution to this warnings ?
Perhaps NULL isn't the best thing to use in this case. It might be more appropriate to use the null character '\0' that is used to terminate cstrings:
 
char Inventory[10]={'A', '\0', 'B', 'C', '\0', 'D', 'E', 'F', 'G', 'H'};
I have one question ,


In your array char Inventory[10]={'A',NULL,'B','C',NULL,'D','E','F','G','H'};

where NULL is a pointer , so when i compiled your code in my g++
it generated many warnings as below :

1
2
3


r.cpp:7:63: warning: converting to non-pointer type ‘char’ from NULL
r.cpp:7:63: warning: converting to non-pointer type ‘char’ from NULL
r.cpp:21:20: warning: NULL used in arithmetic



Did you get any sort of similar warnings or is it compiler specific ?
Are there any solution to this warnings ?


no i didnt get any errors, NULL is equal to 0 so if your having some errors then you should just replace NULL with 0.

ohh wait 0 is int well then just do as Galik said change it to '\0'

i'm using VS 2010 and i dont get any errors
good luck
Last edited on
Topic archived. No new replies allowed.