LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-14-2003, 12:58 AM   #46
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374

I'll have to leave it up to Gtk to post a brilliant solution. However, rmartine came across basically the middle ground between individual variables and structures: arrays.

I'd provide some code, but it's 12:47 AM in Houston, and I need to get to bed. So, I'll leave you with some suggestions on what you should try doing to improve the program.

1) Look at your data types. How many floats do you have with different names? Do you think it might be possible to consolidate at least some of them into logical groups using arrays?

2) Look at your read_GPS function. Is it necessary to call it four times? Would it be possible to consolidate all four user input requests into one function call? There's a way to do it and pass a total of 5 arguments using only arrays.

I know, I probably sound like a TA, but like I said, it's... ugh.... 12:58 now. Gotta go.
 
Old 03-14-2003, 01:25 AM   #47
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Quote:
Originally posted by Randall
right
forgot that other code thingie
let's try that again
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#define RHO 6367
#define PI 3.141592654

void  read_GPS(float *,float *,float *,char *);
float calc_3DCoord(float *, float *);

int main(void)

{
	printf("Please enter in longitudinal co-ordinates for start point:\n");
		read_GPS(&deg_long_1,&min_long_1,&sec_long_1,s2);
                dec_deg_2 = convert_DD(&deg_long_1,&min_long_1,&sec_long_1);
	       	rad_long_1 = calc_SphereCoord_long(s2, &dec_deg_2);
		theta_long_1 = deg2rad(&rad_long_1);
		calc_3DCoord(&phi_lat_1, &theta_long_1);
                What are you doing with the zero return value?

	printf("%c", getchar());
	return(0);
}

void read_GPS(float *degrees,float *minutes,float *seconds,char *direction)
{
		scanf("%f%f%f%s", degrees, minutes, seconds, direction);
		return;
                return statement is redundant
}

float calc_3DCoord(float *phi, float *theta)
{
	float temp_1, temp_2, temp_3;
	temp_1 = (RHO * (sin(*phi)) * (cos(*theta)));
	temp_2 = (RHO * (sin(*phi)) * (sin(*theta)));
        temp_3 = (RHO * (cos(*phi)));
        printf("The cartesian co-ordinates are: %f %f %f\n", temp_1, temp_2, temp_3);
	return(0);
        Why are you returning a zero integer here?
        Maybe this function should return void.
}

Note: You can call a user defined function from another user defined function. For example: convert_DD could call calc_SphereCoord and pass the dec_deg_1 variable. I'm not so sure I want to go through your whole code because it is too unwieldly with all the variables. That's why the designers of the language invented structures and arrays.


 
Old 03-14-2003, 09:09 AM   #48
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
i know i have to clean it up
and it's really messy right now
i just wanted to get it working , passing all the information back and forth and that the result of the calculation is correct. if i accomplish that i can clean it up, remove those unescessary printf statements and consolidate some functions and variables.

i'd really like to thank you guys for your input and patience. it was most invaluable. i suppose eventually i'll learn the art of producing aesthetically pleasing as well as functional code. for now i'm just trying to get it to work.

thanks again guys
 
Old 03-14-2003, 11:03 AM   #49
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
It's getting too hard to read. I suppose that if I had the written requirements than it would be possible to read. I see some of the logic, but it's time to use structures and arrays. Yet if you are almost done, than all I can reiterate some ideas:

In this example, 'myvalue' will be updated in main, because 'SomeFunction' is going to return a copy of a result.
Code:
double SomeFunction();

int main() {
  double myvalue;
  myvalue = SomeFunction();
  return 0;
}
On the other hand, if you want any user defined variables updated in main, and you have more than one, than you can't return them all with the return statement (unless you use a structure) so you will have to use pointers.
Code:
void SomeFunction( double*, double*, double*);

int main() {
  double value1, value2, value3;
  SomeFunction(&value1, &value2, &value3);
  return 0;
}
Yet in another case, you don't need 'value2' to be updated in main. So you don't have to pass the argument by reference, you can instead pass the argument by value (a copy of it).
Code:
void SomeFunction(double*, double, double*);
int main() {
  double value1, value2, value3;
  SomeFunction(&value1, value2, &value3);
  return 0;
}
 
Old 03-15-2003, 11:04 AM   #50
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
i just thought i'd let you guys know, i got it to work, i eneded up calling functions within a function, that seemed to work well enough. i'm now in the process of cleaning up and commenting the code.

i can't thank you guys enough for all the valuable sugestions you gave.

thanks!

best regards,
Randall
 
Old 03-15-2003, 01:24 PM   #51
rmartine
Member
 
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618

Rep: Reputation: 30
Hope you get a good grade.
 
  


Reply



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
Question about C - 'restrict' in function declarations? lowpro2k3 Programming 1 09-11-2005 12:50 AM
C++ function declarations and definitions... AM1SHFURN1TURE Programming 2 08-29-2005 06:57 PM
FIO declarations missing from bits/ioctls.h srdennis Programming 0 04-22-2004 05:07 AM
dhcpd.conf shared network declarations adamantium Linux - Networking 0 02-24-2004 02:24 PM
Forward declarations somnium Programming 3 09-16-2003 05:00 AM

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

All times are GMT -5. The time now is 10:56 AM.

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