Question:
problem with program I wrote (2D Arrays)?
anonymous
2010-11-11 19:08:53 UTC
Run into a bit of a snag with a program I wrote. I'm a bit new to C programming still (first programming course I've had in my life, really)

Basically, what the program is doing is reading data points from a textfile (you also have to name a textfile for it to create and then it prints a header in there-- but that part isn't so important). From the data, it produces a table and then proceeds to fill the table with all those temperatures. The "cell" where the probe is applied however has a different temperature that is supposed to remain constant as the program continues to update.

To update, each cell averages 5 values... its own value, and the four adjacent cells. If a cell is a side or an edge, the cell substitutes ambient temperature as one or two of the 5 values used in the averaging.

here is the program, the problem lies within the update plate void function, im 100% sure.

http://pastebin.com/JB4Uimts


So from the initial plate to plate 1, the program works fine. All the values match the correct output.

HOWEVER

When the update plate function reiterates, this is when I start to get incorrect numbers.

I don't understand why this would be the case. The values are not far off, but they aren't right

Here is what the 2nd table after initial plate should look like:

http://pastebin.com/g7g6fbJw

However, if you run the program with a textfile with the given input, it will not resemble this.... strange...



First person to find out where the problem is gets 10 points!

THANKS!


If you'd like me to post more information, by all means, ask away. I'm asking this question because I can't see how to fix this problem and it's literally driving me crazy
Three answers:
the+university_guy
2010-11-12 11:30:17 UTC
Isn't your MAX_COLS set to 6?





void initialize_plate(double plate[][MAX_COLS], double temp)



{

int i;

int j;



for (i=0; i <=7; i++) //<----------Access violation, b/c plates cannot have more than 6 columns.

{



for(j= 0; j <=7; j++)

{

plate [j][i] = temp;



}

}

}



When your program runs, it is accessing data outside of the 2D array and changing the values of something else. This could be the cause of your problems. Also, double check to make sure that your arithmetic is correct and that you don't have your input text file open in any other running process when you run your own program.



You should probably should stick with using variables instead of constant numbers to act as your sentinel values so that you won't risk making a careless mistake like this.
michael_white2
2010-11-11 19:55:05 UTC
I took a look at your code.



One thing that would concern me is when you go to get data to get an average, that you should do a test to ensure that you are not reaching out-of-bounds on the array sizes.



Do an if-test something like...



if (x >= 0 && x < SizeOfArray)

{

//Do work...

}



As it is, you run the risk of grabbing data from somewhere in your RAM without knowing for sure you're grabbing the right data.
anonymous
2016-10-03 05:53:38 UTC
the linked fee that's outlined in this subject is almost an take care of in basic terms such as you suggested.that's by the undeniable fact that int arrays are actually not comparable to strings and once you attempt to print an int array utilising the print function you get the handle of the variable.for people who use this code it will paintings: for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) approach.Out,print(grid[i][j] + " "); technique.Out.Print("n"); whilst n is the size of the array


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...