lost! need some help!

This is a discussion on lost! need some help! within the C Programming forums, part of the General Programming Boards category; hi guys i know this forum isnt for homework but im taking a software design class and wasnt able to ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    22

    lost! need some help!

    hi guys i know this forum isnt for homework but im taking a software design class and wasnt able to make class on friday so im pretty lost on this assignment. i tried the book and managed to come up with this but its not working at all. the program is supposed to calculate the values of the equation with the givin values of a, b, and c taken from the arrays and use a for loop and output them as follows:

    set 1: a=1, b=2, c=3
    result=58
    set 2: a=4, b=5, c=6
    result=59
    .
    .
    .
    .
    .

    here's what ive come up with so far:
    insert
    Code:
    #include <stdio.h>
    #define CONTSANT 5.9
    main()
    {
    	int a[7] = { 5.6, 7.3, 6, 12.2, 2, 3.2 }
    	int b[7] = { 0.5, 3, 8, 10.3, 4.1, 4,3 }
    	int c[7] = { 1, 3.3, 6.5, 6.6, 8.4, 15.3 }
    	int countera;
    	int counterb;
    	int counterc;
    
    	for ( countera = 1; i <= 6; ++i )
    		aoutvalues[countera] = CONSTANT*a-2/(b+c)-3*c^2;
    
    	for (countera = 0; i <= 6; ++i )
    		printf ("a = %i, b = %i, c = %i\n", a, b, c, aoutvalues);
    
    	for ( counterb = 1; i <= 6; ++i )
    		boutvalues[counterb] = CONSTANT*a-2/(b+c)-3*c^2;
    
    	for (counterb = 0; i <= 6; ++i )
    		printf ("a = %i, b = %i, c = %i\n", a, b, c, boutvalues);
    
    	for ( counterc = 1; i <= 6; ++i )
    		coutvalues[counterc] = CONSTANT*a-2/(b+c)-3*c^2;
    
    	for (counterc = 0; i <= 6; ++i )
    		printf ("a = %i, b = %i, c = %i\n", a, b, c, coutvalues);
    }
    - any help would be much appreciated. again, i only came to this as a last resort.

    thanks so much!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What is your program's output, please?

    Will your code compile?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Not on gcc it won't, and there's some obvious reasons.

    Why don't you at least try and compile it once yourself before you post? You have gotten well beyond the point where that should have happened already.

    Viewed as pseudo-code, I would say you can turn those 6 seperate loops into 2 nested ones.
    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. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    no it won't compile.
    parse error before '{' oken
    parse error before "int"
    parse error before "for"

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    ok i found the obvious semi colons missing and put them in so the code looks like this now. still wont compile though...

    insert
    Code:
    #include <stdio.h>
    #define CONTSANT 5.9
    {
    	int a[7] = { 5.6, 7.3, 6, 12.2, 2, 3.2 };
    	int b[7] = { 0.5, 3, 8, 10.3, 4.1, 4,3 };
    	int c[7] = { 1, 3.3, 6.5, 6.6, 8.4, 15.3 };
    	int countera;
    	int counterb;
    	int counterc;
    
    	for ( countera = 1; i <= 6; ++i )
    		aoutvalues[countera] = CONSTANT*a-2/(b+c)-3*c^2;
    
    	for (countera = 0; i <= 6; ++i )
    		printf ("a = %i, b = %i, c = %i\n", a, b, c, aoutvalues);
    
    	for ( counterb = 1; i <= 6; ++i )
    		boutvalues[counterb] = CONSTANT*a-2/(b+c)-3*c^2;
    
    	for (counterb = 0; i <= 6; ++i )
    		printf ("a = %i, b = %i, c = %i\n", a, b, c, boutvalues);
    
    	for ( counterc = 1; i <= 6; ++i )
    		coutvalues[counterc] = CONSTANT*a-2/(b+c)-3*c^2;
    
    	for (counterc = 0; i <= 6; ++i )
    		printf ("a = %i, b = %i, c = %i\n", a, b, c, coutvalues);
    }
    and if i turn all those for loops into just a couple how would i go about doing that?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hockey1 View Post
    no it won't compile.
    parse error before '{' oken
    parse error before "int"
    parse error before "for"
    That's because you forgot some ; after some } on lines 5-7. After that, you will find out what further problems exist

    That's why you should have started by testing your method on just one set before you proceeded to write more code. You can even do this:
    Code:
    int main() {
    	int a[7] = { 5.6, 7.3, 6, 12.2, 2, 3.2 };
    	return 0;
    }
    Which will compile without an error, whereas this does not:
    Code:
    int main() {
    	int a[7] = { 5.6, 7.3, 6, 12.2, 2, 3.2 }
    	return 0;
    }
    While this program doesn't do anything (and confuses floating point numbers with integers), you at least know it will create an executable you can run. So every time you add a line or so, try compiling so you don't end up wasting time applying erroneous methods.

    Sorry, our posts crossed. You next problem is a missing "i".
    Last edited by MK27; 02-15-2009 at 03:33 PM.
    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

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hockey1, you have a wrong idea about this forum. We help students with homework all the time!

    What we don't do, is code up programs for students that won't work at the programming assignment, and just post up their assignment, along with a big "help me!".

    Your compiler should also be giving you the line # where that error was made. Please add that info to your posts in the future.

    And welcome to the C forum!

    Please add the int main(void) function to the program, Hockey1! Every C program must have a main() function. For technical reasons, int main(void), is the way to go. Note that you'll need to have a "return 0" at the end of that main() function. That just means that your program terminated normally.
    Last edited by Adak; 02-15-2009 at 03:34 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Adak View Post
    What we don't do, is code up programs for students that won't work at the programming assignment, and just post up their assignment,
    I actually tried. However, when I got to this point:
    Code:
    #include <stdio.h>
    
    #define CONSTANT 5.9
    
    int main () {
    	float a[7] = { 5.6, 7.3, 6, 12.2, 2, 3.2 };
    	int i;
    
    	for ( i = 1; i <= 6; ++i ) {
    		aoutvalues[countera] = CONSTANT*a-2/(b+c)-3*c^2;
            }
    
    	return 0;
    }
    I realized I don't know what a, b, and c are, or what aoutvalues was supposed to be.
    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

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I've done that myself, more than once, MK.

    Some assignments or programming idea's are just *interesting*, and then I want to do them.

    The last one for me was the password generator. I had written up little permutation listing programs, but never a permutation generator that included all the subsets of those permutations, (the shorter length parts).

    So I had fun with that, and of course, the original poster, never bothered to follow up on his request for help. I believe there is an inverse relationship at work - the more interesting the assignment/idea, the more unlikely the OP will ever return so we can enjoy it!

    Se la vi!

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    ok guys i refined it even more after searching the book and heres what i got it down to....

    insert
    Code:
    #include <stdio.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 };
    	int i;
    
    	for ( i = 1; i < 7; ++i ){
    		result = CONSTANT*a[i]-2/(b[i]+c[i])-3*c[i]^2;
    	}
    
    	for ( i = 1; i < 7; ++i ){
    		printf ("a=%f, b=%f, c=%f\n, result=%f\n", a, b, c, result);
    	}
    
    	return 0;
    }
    and i'm not trying to get someone to write me the code, just direct me in the right direction as well as maybe a few hints .

    mk27, the a,b, and c values are the ones defined in the arrays at the top. each value for a, b and c are to be plugged into the equation and then i must print the results of the equation in 6 sets corresponding to the values.

    the output should look as follows:
    set1: a = 5.6, b = 0.5, c = 1
    result = (whatever the result of the equation is)

    ***this one wont compile its giving me the following errors:

    error: stray '\127' in program (whats that mean???)
    error: syntax error at '#' token (arent my preprocessor directives correct?)
    error: parse error before '<' token (in the for loop? isnt this correct?)
    error: parse error before "for" (again, i thought it looks right)

    thanks again guys.
    Last edited by hockey1; 02-15-2009 at 07:10 PM.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    In fact ^ is a bit manipulation operator in C.

    To do exponents you need to use math.h and pow(), or in this case powf().

    You also have some cheese in the first byte of your posted code, if the cheese is real, it could explain
    error: stray '\127' in program (whats that mean???)
    error: syntax error at '#' token (arent my preprocessor directives correct?)
    Last edited by MK27; 02-15-2009 at 07:35 PM.
    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

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    You can see the extra character at the start of your code block in the post, the first character.

    Also,
    Code:
    result = CONSTANT*a[i]-2/(b[i]+c[i])-3*c[i]^2;
    Your last operator here does not do what you're expecting. Assuming you want a power, you want this.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    i edited what you guys said and came up with this

    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 };
    	int i;
    
    	for ( i = 1; i <= 6; ++i ){
    		result = CONSTANT*a[i]-2/(b[i]+c[i])-3*pow (c[i],2);
    	}
    
    	for ( i = 1; i <= 6; ++i ){
    		printf ("a=%f, b=%f, c=%f\n, result=%f\n", a, b, c, result);
    	}
    
    	return 0;
    }
    however, the same 127 error occured and a huge mess of others came up after changing the code dealing with files? maybe the include <math.h>?

    is the rest of the code correct?

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    however, the same 127 error occured and a huge mess of others came up after changing the code dealing with files? maybe the include <math.h>?
    Now that is bizarre, isn't it? It might have to do with whatever outlandish thing you are using to create this. Seriously it is very weird and probably nothing to do with C.

    is the rest of the code correct?
    There may be some bad news, hockey1. As soon as you get those alien figures out, the compiler will send you a clue.
    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

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    its coming up with _flock_t errors and parse errors before '*' token??

    im so confused now, its only when i have the #include <math.h> after i take that out it compiles with out the crazy mess of errors.

Page 1 of 2 12 LastLast
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, 05: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, 02:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21