Thread: lost! need some help!

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What is your:
    -OS
    -compiler
    -text editor or whatever
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    vista home premium
    cygwin compiler
    notepad as editor

    if worse comes to worse can you help me with any other part of the code? will the output be correct? if not what do i need to do to fix it?

    i can try to compile it tomorrow morning on the unix system before class.
    Last edited by hockey1; 02-15-2009 at 09:29 PM.

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If this is what you wanted to see:
    Code:
    a=5.600000, b=0.500000, c=1.000000
    result=28.706667
    a=7.300000, b=3.000000, c=3.300000
    result=10.082542
    a=6.000000, b=8.000000, c=6.500000
    result=-91.487930
    a=12.200000, b=10.300000, c=6.600000
    result=-58.818336
    a=2.000000, b=4.100000, c=8.400000
    result=-200.039963
    a=3.200000, b=4.300000, c=15.300000
    result=-683.492065
    Then this will work:
    Code:
    #include <stdio.h>
    #include <math.h>
    #define CONSTANT 5.9
    
    int main () {
    
    	float a[7] = { 5.6, 7.3, 6, 12.2, 2, 3.2 };
    	float b[7] = { 0.5, 3, 8, 10.3, 4.1, 4.3 };
    	float c[7] = { 1, 3.3, 6.5, 6.6, 8.4, 15.3 };
    	float result;
    	int i;
    
    	for ( i = 0; i < 6; ++i ){
    		result = CONSTANT*a[i]-2/(b[i]+c[i])-3*powf(c[i],2);
    		printf ("a=%f, b=%f, c=%f\nresult=%f\n", a[i], b[i], c[i], result);
    	}
    
    	return 0;
    }
    Hopefully you can spot the differences; the first element of an array is 0, not 1. And the precision is slightly better if you actually use powf().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #19
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    damn looks like i was close . thanks though, i really appreciate it and that output was exactly what i was looking for.
    i compiled the code you edited and it still came up with the _flock_ file errors so im guessing things will work on the unix system. hopefully :/

    anyways, again for the millionth time...THANKYOU u really know your stuff.

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not need pow function to calculare squares.
    just use
    Code:
    c[i]* c[i]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    notepad as editor
    Notepad sometimes save files in Unicode, with the Unicode byte-order marker (see here). This may be the cause of your odd characters. I would suggest using a different editor, one that will help you via syntax coloring and the like. For example, notepad++, SciTE, Crimson Editor. If you're used to Unix editors vim and emacs are also available for Windows.

    And vart's right...if you're only doing squares, it's probably overkill to bring in the math library.

  7. #22
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    ok sounds good, i found out why i got all the errors.

    i didnt include a -lm when i compiled it. should have looked like this...

    gcc assignment.c -lm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  2. lost disk
    By Benzakhar in forum Linux Programming
    Replies: 7
    Last Post: 01-11-2004, 06:18 PM
  3. So LOST!!!
    By Drew Vance in forum C Programming
    Replies: 6
    Last Post: 04-25-2003, 05:37 PM
  4. Lost ID number
    By ripper079 in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2002, 12:51 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM