Array search loop

builds ok but crashes when run

Page 1 of 1

2 Replies - 557 Views - Last Post: 26 January 2009 - 08:56 AM Rate Topic: -----

#1 dbthegrandtour  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 15-December 08

Array search loop

Posted 26 January 2009 - 08:47 AM

so i am trying to search an array matrix using a while loop and everything builds ok but when run it crashs i think there is a logic error in my seconds while loop just can not figure out what it is

 
#include <stdio.h>

int main()
{
int x=0;
int item = 6;
int mat[10][10];	
int row=0;
	while (x !=item)
{	
	int col=0;
	while (x=mat[row][col])
	{
		col++;
	}	
		row++;
	}
		
		if (x = item)
{
	printf("FOUND/n");
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Array search loop

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6031
  • View blog
  • Posts: 23,413
  • Joined: 23-August 08

Re: Array search loop

Posted 26 January 2009 - 08:52 AM

Infinite loop here, because you're assigning the value of mat[row][col] to x, rather than comparing it.
while (x=mat[row][col])

= is assignment, == is equivalence.

Here, same principle (without the loop, obviously); you're assigning the value of item to x, so that's always going to be "true":
if (x = item)

Was This Post Helpful? 1
  • +
  • -

#3 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,402
  • Joined: 13-June 08

Re: Array search loop

Posted 26 January 2009 - 08:56 AM

View Postdbthegrandtour, on 26 Jan, 2009 - 07:47 AM, said:

so i am trying to search an array matrix using a while loop and everything builds ok but when run it crashs i think there is a logic error in my seconds while loop just can not figure out what it is

 
#include <stdio.h>

int main()
{
int x=0;
int item = 6;
int mat[10][10];	
int row=0;
	while (x !=item)
{	
	int col=0;
	while (x=mat[row][col])
	{
		col++;
	}	
		row++;
	}
		
		if (x = item)
{
	printf("FOUND/n");
}
}



while (x=mat[row][col])



The above is always true. You're assigning X the value of mat[row][col] and unless it is 0, it will be true. In essence, this is happening.

while(x=mat[row][col])

//is evaluated in two steps.
//first x is assigned the value of mat[row][col]
x=mat[row][col]
//then the while statement is run
while(x)



Essentially, column is being increased beyond the size of the matrix, so it crashes. I assume you mean this:

while(x==mat[row][col])



But moving to an underlying problem. This doens't really search the array at all. It goes down to here:

while(x==mat[row][col])



And unless mat[row][col] happens to be equal to x, col isn't increased and it goes on down to row++. So essentially, you're only comparing the first element of every row. This is how I see it, but maybe you should use for loops to search through the matrix instead of while loops?

Oops. Jackofalltrades beat me to it. - :)

This post has been edited by ccubed: 26 January 2009 - 08:57 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1