Wikiposts
Search
Computer/Internet Issues & Troubleshooting Anyone with questions about the terribly complex world of computers or the internet should try here. NOT FOR REPORTING ISSUES WITH PPRuNe FORUMS! Please use the subforum "PPRuNe Problems or Queries."

C Code Anyone?

Thread Tools
 
Search this Thread
 
Old 17th Mar 2008, 22:05
  #1 (permalink)  
Thread Starter
 
Join Date: Jun 2006
Location: BRISTOL!
Age: 39
Posts: 526
Likes: 0
Received 0 Likes on 0 Posts
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
planecrazy.eu is offline  
Old 18th Mar 2008, 08:50
  #2 (permalink)  
 
Join Date: Jun 2001
Location: Bristol,UK
Posts: 225
Received 11 Likes on 7 Posts
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?)
under_exposed is offline  
Old 18th Mar 2008, 12:57
  #3 (permalink)  
Thread Starter
 
Join Date: Jun 2006
Location: BRISTOL!
Age: 39
Posts: 526
Likes: 0
Received 0 Likes on 0 Posts
Thanks for that =)

You dont by any chance know how to inverse a matrix or array do you?
planecrazy.eu is offline  
Old 18th Mar 2008, 13:43
  #4 (permalink)  
 
Join Date: Jun 2001
Location: Bristol,UK
Posts: 225
Received 11 Likes on 7 Posts
What do you mean by inverse a matrix or array?
under_exposed is offline  
Old 18th Mar 2008, 14:01
  #5 (permalink)  
bnt
 
Join Date: Feb 2007
Location: Dublin, Ireland. (No, I just live here.)
Posts: 733
Received 6 Likes on 5 Posts
Post

Originally Posted by under_exposed
(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;
}
...
bnt is offline  
Old 19th Mar 2008, 13:12
  #6 (permalink)  
 
Join Date: Feb 2003
Location: Scotland
Posts: 144
Likes: 0
Received 0 Likes on 0 Posts
There is a matrix inversion algorithm here

http://www.thescripts.com/forum/thread513120.html
cdtaylor_nats is offline  
Old 19th Mar 2008, 14:23
  #7 (permalink)  
Thread Starter
 
Join Date: Jun 2006
Location: BRISTOL!
Age: 39
Posts: 526
Likes: 0
Received 0 Likes on 0 Posts
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.
planecrazy.eu is offline  
Old 20th Mar 2008, 09:01
  #8 (permalink)  
 
Join Date: Jun 2001
Location: Bristol,UK
Posts: 225
Received 11 Likes on 7 Posts
like this?

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

{

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

}
}
under_exposed is offline  
Old 20th Mar 2008, 16:27
  #9 (permalink)  
PersonalTitle to help support PPRuNe against legal bullying.
 
Join Date: Sep 2005
Location: France
Posts: 134
Likes: 0
Received 0 Likes on 0 Posts
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.
tallsandwich is offline  
Old 21st Mar 2008, 14:48
  #10 (permalink)  
Thread Starter
 
Join Date: Jun 2006
Location: BRISTOL!
Age: 39
Posts: 526
Likes: 0
Received 0 Likes on 0 Posts
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.
planecrazy.eu is offline  
Old 21st Mar 2008, 17:27
  #11 (permalink)  
Spoon PPRuNerist & Mad Inistrator
 
Join Date: Sep 2003
Location: Twickenham, home of rugby
Posts: 7,396
Received 263 Likes on 172 Posts
Square them, then get the square root - that should make 'em all positive!

I'll get me coat...

SD
Saab Dastard is offline  
Old 21st Mar 2008, 23:53
  #12 (permalink)  
Wunderbra
 
Join Date: Aug 2006
Location: Bedford, UK
Age: 44
Posts: 313
Likes: 0
Received 0 Likes on 0 Posts
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.
matt_hooks is offline  
Old 22nd Mar 2008, 02:06
  #13 (permalink)  
bnt
 
Join Date: Feb 2007
Location: Dublin, Ireland. (No, I just live here.)
Posts: 733
Received 6 Likes on 5 Posts
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.
bnt is offline  

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Contact Us - Archive - Advertising - Cookie Policy - Privacy Statement - Terms of Service

Copyright © 2024 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.