Dear Sir/Madam,

I'm currently trying to compare a value from an array vs (In the first instance) A set character, and (in the second instance) against a set integer.

None of the comparisons are ever returning true even when they appear to be the same on a printf statement, although I would assume this is due to how I've stored such array and variables, and the way they're being outputted.

(Please note this should show the appropiate code listing, but some has been cropped as it's a lot that's not involved in this area)

char Input[3][100]; 
int ch;

=======================

do // Cycles through file and takes any item that's not a space or EoL and inputs to array (Used Integer due to needing it for testing EOF)
{
    ch = fgetc(file);
	printf("%c", ch);

	if (ch == ' ')
	{
		Counter += 1;
	}
	else if (ch == '\n')
	{
		Counter = 0;
		Counter2 += 1;
	}

	else
	{
		Input[Counter][Counter2] = ch;
		
	}

	} while (ch!=EOF);
	
	fclose(file);

	}

}

=============================

I'm trying to compare the value of each cell in the array as I believe the way I've done it it stores them as an ascii value, and as such I need to do:

if (Input[0][countinc] == 'A')   //Compares array values and will return K to a value equal to input[0][0]-65
k = 0;

This code works fine, but is limiting me to having to repeat the code for every single ascii value, rather than being able to loop through just using a cast of the value to compare it to it's ascii value. For example if Input[0][0] = A or the ascii of A, k = ascii - X = 0, thus B = 1, C = 2 etc.

I have tried combinations of casting such as:

k = ((int)(Input[0][countinc])-64);
k = (int)k;
k = atoi(Input[0][countinc]);

None of these appear to work (and I have a feeling I just fail on casting :<)


Later in the program I'm trying to compare the same array values to an integer value for use in finding the smallest value in said array.

char CountMin[1];
	CountMin[0] = "8";

	for  (y = 0; y <= (Vortex*Vortex); y++)
	{
		char k[1];
		k[0] = Input[2][y];
	
		printf("'%c'\n", k);
		printf("'%c'\n", CountMin);

		if (strcmp(k[0],CountMin[0])==0)

I've tried setting K as a string, and as an integer, and using this for a strncmp, strcmp, and simply a comparison of the two values using the above casts, Neither of which appear to work again.

====================================

I hope that wasn't too waffly to follow >.< and I apologise if I'm just being stupid here, Just none of the normal ways I'd do it appear to work, and the only reason I can assume is due to my taking "ch" in as Int for EOF, compared to take it in as char normally. Thus has the array saved my ch as the pure ascii values so when I take them out they show as something strange... But I don't know how to check.

Hope my waffling makes sense,

And thankies in advance.

Steph ^.^

What exactly are you trying to do here??

char CountMin[1];	
CountMin[0] = "8";

storing string in a charachter??

Comment on your first code segment: I would have been tempted to use fgets() but your code should work.

Second and Third code segment: The following will always subtract 65 (which is the ASCII code for 'A'). Is there any time you wouldn't want to do it? (For example, if the character were a '5' you will get a negative number.)

int k;
/* - - - */
k = Input[0][countinc] - 'A';

Fourth code segment: In order to use strcmp() you need to have null terminated strings. You would have to declare both CountMin and k as char[2] and put '\0' in CountMin[1] and k[1]. But if all you want to do is to compare the character '8' and Input[2][y], you can just do that:

if (Input[2][y] == '8')

The code above is equivalent to what I think you were trying to do.

Heya,

I feel so stupid >.< lol the parts for the Ascii value worked lovely thank you. The reason it was always taking 65 off is I'm putting a char value and using this to determine where in a 2D matrix it would go, Thus A = Cell 0, B = 1 etc.

if (Input[2][y] == '8')

The code above is equivalent to what I think you were trying to do.

There is one slight problem I have here, Without knowing the input values into Input[] I'm assuming I would have to write a Case for every ascii char. Is there a simpler way to deduce the '8' from a counter variable which is incremented on each check? (I know that simply checking it as an integer won't as it's wrong type, and I'm assuming even casting it back to char from int I'll get same problem I had before of needing nulls on the end?

Thankies again Steph ^.^

That was a nice paragraph, but I still have no idea what you wanted to do with the data from Input[2].

"deduce the '8' from a counter variable"?

Why don't you write (in english) what you need to do with each character (or the set of characters if that's easier).

That was a nice paragraph, but I still have no idea what you wanted to do with the data from Input[2].

"deduce the '8' from a counter variable"?

Why don't you write (in english) what you need to do with each character (or the set of characters if that's easier).

My apologies if the above wasn't explained properly,

By deducing the '8' from a counter variable, I was simply asking if it's possible to reach the conclusion of the compared value (8) by using an incremented counter (I know the below example won't work, but just to explain):

for (x= 1; x <=9; x++)
{
           if (input[0][x] == X)
               Do Y
}

Rather than having to "hardcode" the values such as '8', taking them via the Counter. Such that I can loop 1-10, and check the comparison each time:

{
           if (input[0][x] == '8')
               Do Y
           if (input[0][x] == '9')
               Do Y
           if (input[0][x] =='10')
               Do Y
}

The problem I'm having is where the original '8' is defined in the single quotes, it changes the value, thus simply comparing the counter variable won't return the same value.

Apologies if I waffled again >.< Trying to explain and keep it shorter.

Thankies again Steph

First comment...the single character input[0][x] can never be the invalid character constant '10' .

You still haven't stated in english what you're trying to accomplish. Whatever the "Y" is in "Do Y" may play a significant part in coming to an optimal solution.

So, poking in the dark again because you won't turn on the light, do any of the following benefit you in some way?

if (isdigit(input[0][x])){
    // do Y here?
}
if (input[0][x] >= '8' && input[0][x] <= '9'){
    // do Y here?
}
char interestingchars[] = "89.+-";
// some time later
if (strchr(interestingchars, input[0][x]) != NULL) {
    // do Y here?
}

First comment...the single character input[0][x] can never be the invalid character constant '10' .

You still haven't stated in english what you're trying to accomplish. Whatever the "Y" is in "Do Y" may play a significant part in coming to an optimal solution.

So, poking in the dark again because you won't turn on the light, do any of the following benefit you in some way?

My apologies again, I'll try to explain straight from the start:


My program is reading a text file formatted as such:

A B 2

Each line is taken in and the seperate values put into an array:

Input[0][X] = A
Input[1][X] = B
Input[2][X] = 2

The array represents a matrix, where A and B are changed into row and column values, and the number is added into the matrix:

ABC
A 012
B 101
C 210

This is sorted by the third column by decreasing value:

C D 4
R G 3
A B 2

I then wish to sort through the array to find a single instance of the array which has the lowest value:

(Vortex is the number of inputs, thus Vortex Squared shows how many items inside the matrix)

int FindRoute()
{
	for  (y = 0; y <= (Vortex*Vortex); y++)
	{
		if (Input[2][y] == '1')
		{
		printf("Node 1 found at %d\n", y);
		}

		if (Input[2][y] == '2')
		{
		printf("Node 2 found at %d\n", y);
		}
		if (Input[2][y] == '3')
		{
		printf("Node 3 found at %d\n", y);
		}
		if (Input[2][y] == '4')
		{
		printf("Node 4 found at %d\n", y);
		}
  	}

 printf("End");
}

This will search through the array, and at the first instance of '1' output the reference to the cell.

Thus if the file was as such:

C D 4
R G 3
A B 1

It would output: Node 1 found at 2.

All of the above code works perfectly.

===========================================

The problem is such that the input file may contain 2 or 1000 values. The vortex is automatically generated elsewhere, so the looping is no problem.

The problem with the last snippet above is if I wished to check for a value between 1 and Vortex^2, eg. 60, and a 777, I'd need to hard code:

if (Input[2][y] == '60')
		{
		printf("Node 60 found at %d\n", y);
		}
if (Input[2][y] == '777')
		{
		printf("Node 777 found at %d\n", y);
		}

And the same for every other value (ie. 999 more items, if 1000 inputs). Is it possible to use the counter variable "y" to also be the compared value.

This would allow the for loop above to test a comparison of every value possible within the loop.

Hope this explains it better >.<

Thankies again Steph

Thanks for the better explanation.

Is it possible that you might have an input line like:

F M 24

If so, your current data structure is insufficient to hold your data as the '24' will not fit inside a single character. If this were the first line in your input file, based on your previously posted input routine, you would have:

input[0][0] = 'F'
input[1][0] = 'M'
input[2][0] = '4'

I presume this is NOT the desired effect.

The array represents data to be loaded into a matrix.

Do you actually build and fill the matrix?

(Quick off-topic question, as I'm curious:
Is the matrix reflective?
Does A B 2 imply B A 2?
)

When you sort by the third column, are you sorting the array or the matrix?

When you are searching, are you searching the array or the matrix?

(If you're searching the array -- and I think you are -- there are only Vortex entries.)

As for your print loop, you could rewrite it as:

for (y = 0; y < Vortex; y++)
{
    printf("Node %c found at %d\n", Input[2][y], y);
}

But that's still not what you really want to do is it?

Righty,

There currently isn't a double-digit item in the input file, but now I think of it this would indeed break how I've done it :<

The matrix is built and populated as shown on the screenshot.

The matrix is reflective, as A B 2 says Length from A to B is 2, Thus B to A is also 2.

All sorting and searching is done directly onto the array. Currently the matrix is only used for outputting purposes.

Below you can see a screenshot of it's current working:

[IMG]http://img175.imageshack.us/img175/7610/ccodeea1.jpg[/IMG]

As you can see, this code:

int FindRoute()
{

	for  (y = 0; y <= (Vortex*Vortex); y++)
	{

		if (Input[2][y] == '1')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '2')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '3')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '4')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '5')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '6')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}
		

  
	}

 printf("End");
}

Correctly identifies where any value = 1-6 occur.

The problem enlies in where the item is not directly hardcoded.

If I hardcore 1-6, and someone inputs an 7 this won't check it.
If I hardcore 1-7, and someone inputs an 8 this won't check it.

So my question relates to:

Is it possible to take an arbitrary value, (say 200), and check the array for all values between 1-200, without hard coding this 200 times.

Hope that explains it >.< (expect you're bored of my wafflings by now)

Thankies again Steph

You are obviously searching the input array, but you are iterating Vortex * Vortex times. Isn't that more than the array can hold? (or is there another counter for the number of input pairs?)

Your code seems to be specifically skipping '0' nodes, right?

int FindRoute()
{
	for  (y = 0; y <= (Vortex*Vortex); y++)
	{
		if (Input[2][y] >= '1')
		{
			printf("Node %c found at %d\n", Input[2][y], y);
		}
	}

 printf("End");
}

You are obviously searching the input array, but you are iterating Vortex * Vortex times. Isn't that more than the array can hold? (or is there another counter for the number of input pairs?)

Your code seems to be specifically skipping '0' nodes, right?

The code does indeed skip 0 due to this effectively saying length A - A is 0, Well yes as it is itself.

The reason I used Vortex*Vortex is this is the maximum number that you could possibly link.

If you have Vortex 2, You can Link AA, AB, BA, BB
If you have Vortex 3, You can link AAA, AAB..... BBB etc.


Without this sounding rude, Do you understand what I'm after or is my explanation still "iffy" Not sure if I'm just explaining it silly while tired or there's a misunderstanding.

Think the best way to explain it would be:

I'm trying to shrink this code into a single test case in which '1' '2' '3'...'XXX' isn't directly hardcoded, but taken from an increasing variable. Thus any item between '1' and '200' will show up as:
Node XXX found at YYY

int FindRoute()
{

	for  (y = 0; y <= (Vortex*Vortex); y++)
	{

		if (Input[2][y] == '1')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '2')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '3')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '4')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '5')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}

		if (Input[2][y] == '6')
		{
		printf("Node %c found at %d\n", Input[2][y], y);
		}
		

  
	}

 printf("End");
}

Thankies again Steph ^.^

I agree that Vortex * Vortex is the maximum number you could have, but you know how many you had, why not use that number instead of looping beyond what really makes any sense.

Is there another criteria (besides not zero) that you want to use to select which ones print?

The lowest value? All nodes having the lowest value?

The highest value? All nodes having the highest value?

The lowest 3 values and the nodes that have any of those values?

You mentioned sorting the array from the highest to the lowest value. Are we looping through the sorted array?

If we are in the sorted array and want low values or high values, why not start on the right end of the array and stop looping when we've gone far enough?

Are you wanting something more like where we print all the '1's first, then the '2's, then the '3's?

If so, you could use brute force (**this is NOT a good idea, but it would work**) with something like:

for (match = '1'; match < '9'; match++) {
    for (y = 0; y < Vortex * Vortex; y++) {
        if (Input[2][y] == match) {
            printf("Node %c found at %d\n", match, y);
        }
    }
}

But sorting the array into the order you want to print it makes a lot more sense.

If you (for some reason) need to be able to print which node it was in from the original input, we could use an index and still sort.

If there are no other criteria as to which nodes need to be printed, the loop I last posted would print everything that was not zero.

You need to be able to explain (at least to yourself) in english what it is you want to print. It would help if you thought about how you might produce the output you want 'by hand' if you had each of the input lines on a 3x5 card.

If so, you could use brute force (**this is NOT a good idea, but it would work**) with something like:

That works perfectly! Thank you! ^.^


Steph

This article has been dead for over six months. Start a new discussion instead.