PPRuNe Forums

PPRuNe Forums (https://www.pprune.org/)
-   Computer/Internet Issues & Troubleshooting (https://www.pprune.org/computer-internet-issues-troubleshooting-46/)
-   -   C Code Anyone? (https://www.pprune.org/computer-internet-issues-troubleshooting/318604-c-code-anyone.html)

planecrazy.eu 17th March 2008 22:05

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

under_exposed 18th March 2008 08:50

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?)

planecrazy.eu 18th March 2008 12:57

Thanks for that =)

You dont by any chance know how to inverse a matrix or array do you?

under_exposed 18th March 2008 13:43

What do you mean by inverse a matrix or array?

bnt 18th March 2008 14:01


Originally Posted by under_exposed (Post 3986062)
(Why are leading spaces removed from posts?)

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[])
{
int i,j;
int requested_value;

if (argc != 2)
{
printf( "\nUsage: %s value ", argv[0] );
return;
}
...

cdtaylor_nats 19th March 2008 13:12

There is a matrix inversion algorithm here

http://www.thescripts.com/forum/thread513120.html

planecrazy.eu 19th March 2008 14:23

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.

under_exposed 20th March 2008 09:01

like this?

e = 0;
for(i=1;i<=number;i++)

{

for(j=i+1;j<=number;j++);
{
e += j + c^2

}
}

tallsandwich 20th March 2008 16:27

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.

planecrazy.eu 21st March 2008 14:48

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.

Saab Dastard 21st March 2008 17:27

Square them, then get the square root - that should make 'em all positive!

I'll get me coat...

SD

matt_hooks 21st March 2008 23:53

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:

bnt 22nd March 2008 02:06

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.