![]() |
C Code Anyone?
Hey...
Ages ago i asked for some help on some c code on here, and got a good response, well i got the answer. I now find my self stuck. I am trying to make some code, using loops, that can figure out all possibilities for a value of numbers. IE if i enter 5, then there is 1..2, 1..3, 1..4, 1..5, 2..3, 2..4, 2..5, 3..4, 3..5, 4..5, I then need to read this info in to some sort of array, however, i cant figure the above. i can get them, but i end up with 1..1, 2..2, 3..3, etc. Using loop with nested loop. thanks |
I do like C
#include <stdio.h>
void main (int argc, char * argv[]) { int i,j; int requested_value; if (argc != 2) { printf( "\nUsage: %s value ", argv[0] ); return; } requested_value = atoi(argv[1]); if (requested_value > 1) { for (i=1;i<=requested_value;i++) { for (j=i+1;j <= requested_value;j++) { printf("%d..%d, ",i,j); } } printf("\n"); } else { printf("no range\n"); } } (Why are leading spaces removed from posts?) |
Thanks for that =)
You dont by any chance know how to inverse a matrix or array do you? |
What do you mean by inverse a matrix or array?
|
Originally Posted by under_exposed
(Post 3986062)
(Why are leading spaces removed from posts?)
#include <stdio.h> void main (int argc, char * argv[]) { int i,j; ...int requested_value; if (argc != 2) { printf( "\nUsage: %s value ", argv[0] ); }return; |
|
Thats just what i need....
I have another problem now... The code is similar for(i=1;i<=number;i++) { for(j=i+1;j<=number;j++); { e = j + c^2 } } What i need to do is get the value for e, then on the next loop add the new e value to the old e value to sum up? Any ideas? Code is an example, so might be a little not to the point. |
like this?
e = 0; for(i=1;i<=number;i++) { for(j=i+1;j<=number;j++); { e += j + c^2 } } |
Buy this book
If you enjoy C, get this:
Title: "Expert C Programming" Author: "Peter van der Linden" I coded C for years and then read this book and at that stage I found it easy to read, interesting, both entry level AND expert level too. The guy ran the Sun "C" Compiler team and then later was a key player in the Java project. It explains all the quirks of the language, common pitfalls and it is not a doorstop. I can't recommend it enough, it' actually enjoyable and explains all the WTFs you get in a succinct way. P.S. It's orange with a blue fish on the front. |
Thanks for the book advice, i will see if i can hunt a copy down...
I am stuck on one more thing... My app calculates numbers, some are + and some are -. I need to some how strip the - figures and just make them +, as what i want to do is add them togeather. If it was a - and a - it would be ok, but i have mixed numbers and need to add them all together in a non matchmatical way of plus and minus powers. Ie I want -4,5,-2 and 4 to add up to 15. Any help is appreciated. |
Square them, then get the square root - that should make 'em all positive!
I'll get me coat... SD |
Saab, that's exactly what I would have suggested. Square all numbers then square root them, unless there's a specific function to ignore signs in C, long time since I've done any C programming. :confused:
|
I think you're after an "absolute value" function, called abs(int x) or fabs(double x), or something like that? You'd apply it to each value before summing them. I found this code example that shows how they're used.
|
| All times are GMT. The time now is 13:05. |
Copyright © 2026 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.