C Code Anyone?
Thread Starter
Joined: Jun 2006
Posts: 526
Likes: 0
From: BRISTOL!
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
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

Joined: Jun 2001
Posts: 238
Likes: 40
From: Bristol,UK
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?)
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?)

Joined: Feb 2007
Posts: 755
Likes: 26
From: Dublin, Ireland. (No, I just live here.)
On other forums you'd have CODE tags for this purpose, but since they don't work here, you can get the general idea across by using the Indent buttons like this:
#include <stdio.h>
void main (int argc, char * argv[])
{
#include <stdio.h>
void main (int argc, char * argv[])
{
int i,j;
int requested_value;
if (argc != 2)
{
...
int requested_value;
if (argc != 2)
{
printf( "\nUsage: %s value ", argv[0] );
return;
}return;
Joined: Feb 2003
Posts: 144
Likes: 0
From: Scotland
Thread Starter
Joined: Jun 2006
Posts: 526
Likes: 0
From: BRISTOL!
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.
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.
PersonalTitle to help support PPRuNe against legal bullying.
Joined: Sep 2005
Posts: 134
Likes: 0
From: France
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.
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.
Thread Starter
Joined: Jun 2006
Posts: 526
Likes: 0
From: BRISTOL!
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.
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.
Wunderbra
Joined: Aug 2006
Posts: 313
Likes: 0
From: Bedford, UK
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.

Joined: Feb 2007
Posts: 755
Likes: 26
From: Dublin, Ireland. (No, I just live here.)
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.




