Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blindfutur3

macrumors member
Original poster
Oct 9, 2008
74
0
Hi guys, girls

I've recently begun teaching myself programming in order to gain a head start for when I begin University next year. After much procrastination about which language to begin with, and many people informing me that the language didn't matter, I chose C.

Since then I've been following the lectures by Richard Buckland (which can be found on iTunes U under COMP1917). As part of an assignment, the students at the time were tasked with writing a set of functions to complete a Sudoku Solver. Taking on this assignment myself, I've come across a small problem I'm having trouble understanding.

I'm currently writing a function to print the solved Sudoku puzzle. For the most part there's nothing too challenging, however I keep receiving the error
Code:
Array subscript is not an integer
on the line where I print out the value.

I've included the function below in order to help gain a better perspective of what I'm doing.

Code:
void showGame (sudokuGrid game) {
	double count;
	
	count = 0;
	
	// Step through the array, print each entry in the array
	while (count <= GRID_SIZE) {
		printf ("%c ", game[count]);
		
		// If count is divisible by 9, print a newline
		if (count - (count - 9) / 9 == 1) {
			printf ("\n");
		}
		count++;
	}
}

Thanks,

- John

P.S ~ As a side note, if anybody has any material, lectures or books they'd recommend a beginner to aid in my learning of programming concepts or C, I'd be most grateful to receive them.

Thanks again

- John
 
Your 'count' variable is declared as type 'double'.

See the 3rd paragraph of my post #4 in the following thread:
https://forums.macrumors.com/threads/859578/

If the necessary correction isn't obvious, then you need to review C's data types, and what they represent.

Thank you. I wasn't aware that they had to be integers. I'll go back and review data types before I continue. Your help is much appreciated.

To put some reasoning behind my choice of type. In the line,
Code:
if (count - (count - 9) / 9 == 1) {
I'm trying to print a newline every ninth character, and a colleague of mine who's currently studying CS informed me that using a double would be wise as an integer would cause problems(he didn't go into the specifics of it).
 
To put some reasoning behind my choice of type. In the line,
Code:
if (count - (count - 9) / 9 == 1) {
I'm trying to print a newline every ninth character, and a colleague of mine who's currently studying CS informed me that using a double would be wise as an integer would cause problems(he didn't go into the specifics of it).

You should ask your colleague for the specifics. Or have him write out the rationale and you can post it here. I can't think of any good rationale myself, so I'm wondering what his thinking might be.

You should also look at the modulus operator, which is the % symbol. Paste the following into a C file, compile and run, then look carefully at the output. I leave it to you to figure out how to use this in your own code:

Code:
#include <stdio.h>

int main (int argc, const char * argv[]) 
{
	int counter;
	for ( counter = 0;  counter < 30;  counter++ )
	{
		int modulus = counter % 9;
		printf( "%d mod 9 = %d\n", counter, modulus );
	}
	return 0;
}

I also see some other problems with your code, but I think it's better that you find and fix them yourself, after you get the array-subscript working. The problem will be obvious, I'm sure.
 
Thank you for your help Chown. I appreciate you not spelling it out for me, and leaving things unclear for me to work out alone. Appreciate all the help.
 
Thank you. I wasn't aware that they had to be integers. I'll go back and review data types before I continue. Your help is much appreciated.

To put some reasoning behind my choice of type. In the line,
Code:
if (count - (count - 9) / 9 == 1) {
I'm trying to print a newline every ninth character, and a colleague of mine who's currently studying CS informed me that using a double would be wise as an integer would cause problems(he didn't go into the specifics of it).

Cause problems like... overflowing?

if count was an int
count - 9 is an int
count - ^^ is an int
count / 9 is PROBABLY not an int, however since you test for equality...
^^ == 1, you can fix you math easily by not deviding by nine.

9*count - (count -9) == 9

integer safe calculation... though I don't know what it is trying to figure out so I can't tell you if its a good one. Just that it does what you want without relying on that nasty double type.
.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.