LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2005, 09:31 AM   #1
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Rep: Reputation: 45
What is wrong with this c code?


Hi,

can anyone tell me what goes wrong in this code?;

Code:
void rowReduce(float points[3][2], float params[2])
{
	float mat[3][2];
	int r,c,i;

	for(r=0;r<=2;r++)
	{
		for(c=0;c<=3;c++)
		{
			if(c != 3)
			{
				mat[c][r] = pow(points[r][0],2-c);
			}
			else
			{
				mat[c][r] = points[r][1];
			}
			printf("%f ",mat[c][r]);
		}
		printf("\n");
	}
	printf("\n");

	for(r=0;r<=2;r++)
	{
		for(c=0;c<=3;c++)
		{
			printf("%f ",mat[c][r]);
		}
		printf("\n");	
	}

	free(mat);
}	

int main(int argv, char **argc)
{
	float points[3][2]= {{-10.0,1.0},{0.0,0.0},{2.0,1.0}};
	float params[2];
	int i;

	rowReduce(points,params);

	return 0;
}
When you run this code, you will see two times a set of 4 by 3 floating point numbers. These two sets should be identical, by they ar not! What's going on?

Thanks in advance....
 
Old 06-02-2005, 09:46 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
You're lucky it doesn't segfault.

Code:
float mat[3][2];
This has three elements

You reference four elements here - you are probably overwriting the r & c variables.
Code:
for(c=0;c<=3;c++)
 
Old 06-02-2005, 09:51 AM   #3
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Original Poster
Rep: Reputation: 45
Oh... oh well i'm still learing. I changed it to
Code:
mat[4][3]
. That works much better. Thanks dude
 
Old 06-02-2005, 10:04 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Also consider not using float - there is no extra overhead on most boxes for using double.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is wrong with the following C code? indian Programming 17 12-04-2005 09:01 AM
What is wrong with this C++ code? frankie_DJ Programming 5 10-01-2005 06:19 PM
What is wrong with this C code ? indian Programming 8 04-16-2005 04:42 AM
what ' s wrong in code ? phoenix_fei Programming 4 12-17-2004 12:32 PM
What is wrong with this code? qanopus Programming 4 03-12-2004 07:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration