Forum Navigation:


FORUMS > Programming and Software Forum
Replying to Topic: simple C code
Created On Thu Feb 03, 05 04:48 PM by SPAAGG


Username:
Password:

Remember my login


[ Forget your login information? | Join the forums ]




SPAAGG
Senior Member

Posts: 779
Joined: Mar 2003

Thu Feb 03, 05 04:48 PM
User is offline View users profile

Hi !

I am very weak in C, and need to compute a function which does the following (i write it in R):
v is a vector (unknown size)
a is a scalar


theFunction = function(v, a)
{
n = length(v)

y[1] = 10
for (i in 2:n)
{
y = v + a * v[i-1]
}

return(y)
}

that is, I enter a vector of unknown size, floating type, and a scalar, floating type, and want that the C function returns me a new vector. I think that in C, a function cannot return a vector. We need to use a procedure, that is void.

Could you help me

Tx in advance
 
Reply
   
Quote
   
Top
   
Bottom
     



Cuchulainn
Senior Member

Posts: 16364
Joined: Jul 2004

Thu Feb 03, 05 05:09 PM
User is offline View users profile

removed, just confused the issue

-------------------------
http://www.datasimfinancial.com/coursesandevents.php

Edited: Sat Feb 05, 05 at 08:14 PM by Cuchulainn
 
Reply
   
Quote
   
Top
   
Bottom
     



daveangel
Senior Member

Posts: 4978
Joined: Oct 2003

Thu Feb 03, 05 07:59 PM
User is online

SPAAG

this might do what you want ...

double * myFunc(const double *v,int n,double a,double *y)
{
     y[0] = 10;
     for (int i = 1;i < n; i++)
     {
          y[i] = v[i] + a*v[i-1];
     }

     return y;
}

int main()
{
     double v[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ; // create v
     double y[sizeof(v)/sizeof(double)]; // allocate y

     myFunc(v,10,5,y); // a = 5

     return 1;
}



-------------------------
Knowledge comes, but wisdom lingers.

Edited: Thu Feb 03, 05 at 08:00 PM by daveangel

 
Reply
   
Quote
   
Top
   
Bottom
     



mama
Member

Posts: 94
Joined: Apr 2003

Thu Feb 03, 05 08:06 PM
User is offline

Quote

Originally posted by: daveangel
SPAAG
double * myFunc(const double *v,int n,double a,double *y)
{
y[0] = 10;
for (int i = 1;i < n; i++)
{
y = v + a*v[i-1];
}

return y;
}

int main()
{
double v[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ; // create v
double y[sizeof(v)/sizeof(double)]; // allocate y

myFunc(v,10,5,y); // a = 5

return 1;
}

If SPAAG wants to do dynamic memory stuff then that's another story of course. If he is new to C, those pointers are going to be great fun, not.
I suggested vector because all that memory stuff is hidden.
this might do what you want ...


 
Reply
   
Quote
   
Top
   
Bottom
     



SPAAGG
Senior Member

Posts: 779
Joined: Mar 2003

Thu Feb 03, 05 09:56 PM
User is offline View users profile

thank you so much !

David
 
Reply
   
Quote
   
Top
   
Bottom
     



henny
Member

Posts: 178
Joined: Jan 2005

Thu Feb 03, 05 10:12 PM
User is offline

        Objective Caml version 3.08.2

# let testfun v a =
  let n = Array.length v in
  let y = Array.create n 0. in
  y.(0) <- 10.;
  for i = 1 to n-1 do
    y.(i) <- v.(i) +. (a *. v.(i-1))
  done;
  y;;
              val testfun : float array -> float -> float array = <fun>
# let a = [|1.;2.;3.;4.;5.;6.;7.;8.;9.;10.|];;
val a : float array = [|1.; 2.; 3.; 4.; 5.; 6.; 7.; 8.; 9.; 10.|]
# testfun a 1.;;
- : float array = [|10.; 3.; 5.; 7.; 9.; 11.; 13.; 15.; 17.; 19.|]



Edited: Wed Mar 30, 05 at 01:51 PM by henny

 
Reply
   
Quote
   
Top
   
Bottom
     



Cuchulainn
Senior Member

Posts: 16364
Joined: Jul 2004

Fri Feb 04, 05 11:00 AM
User is offline View users profile

removed, confused the issue, excuses


-------------------------
http://www.datasimfinancial.com/coursesandevents.php

Edited: Sat Feb 05, 05 at 08:13 PM by Cuchulainn
 
Reply
   
Quote
   
Top
   
Bottom
     



Cuchulainn
Senior Member

Posts: 16364
Joined: Jul 2004

Fri Feb 04, 05 11:26 AM
User is offline View users profile

Correct (now pasted from VS, not Word)
--

// testvalarray.cpp
//
// Show some of the functionality of the STL
// 'valarray'. This is a dynamic maths vector class.
//

#include <valarray> // Reusable vector class
#include <iostream> // I/O stuff
using namespace std; // STL namespace, STANDARD C++

template <class T> valarray<T> multiply(const valarray<T>& vec1, const T& factor)
{ // Multiply a vector by a scalar factor

valarray<T> result (vec1.size()); // Same size

for (int j = 0; j < result.size(); j++)
{
result[j] = vec1[j] * factor;
}

return result;
}

// Usually work with doubles, define handy alias
typedef valarray<double> DoubleArray;

int main()
{
int N;
cout << "Give size: ";
cin >> N;

double factor = 0.5;

DoubleArray inputArray(N);

// Initialisation of input array
for (int j = 0; j < inputArray.size(); j++)
{
inputArray[j] = double (j+1); // Simple values
}
DoubleArray outputArray = multiply(inputArray, factor);

for (int i = 0; i < outputArray.size(); i++)
{
cout << outputArray << ", ";
}

return 0;
}

-------------------------
http://www.datasimfinancial.com/coursesandevents.php

Edited: Fri Feb 04, 05 at 11:57 AM by Cuchulainn
 
Reply
   
Quote
   
Top
   
Bottom
     



Cuchulainn
Senior Member

Posts: 16364
Joined: Jul 2004

Sat Feb 05, 05 04:36 PM
User is offline View users profile

Quote

Originally posted by: daveangel
SPAAG
double * myFunc(const double *v,int n,double a,double *y)
{
y[0] = 10;
for (int i = 1;i < n; i++)
{
y = v + a*v[i-1];
}

return y;
}

int main()
{
double v[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ; // create v
double y[sizeof(v)/sizeof(double)]; // allocate y

myFunc(v,10,5,y); // a = 5

return 1;
}


this might do what you want ...


What happens if the CLIENT, i.e. main program inadvertently does ...

double a = 5.0;
myFunc(v,20,a,y); // overwrites someone else's memory

or even

double a = 5.0;
myFunc(v,6,a,y); // not all elements iterated over

So this code works but very brittle, unfortunately. In the first place I get a run-time error and 2nd the wrong answer.





-------------------------
http://www.datasimfinancial.com/coursesandevents.php

Edited: Sat Feb 05, 05 at 04:56 PM by Cuchulainn
 
Reply
   
Quote
   
Top
   
Bottom
     



daveangel
Senior Member

Posts: 4978
Joined: Oct 2003

Sat Feb 05, 05 05:17 PM
User is online

Look Cuchulain,

You have made three attempts at posting a solution to the SPAAGs problem .. remember the title of the thread "simple C Code".

I have no doubt that the design isnt foolproof but that was not what SPAAG wanted.

Cheers

-------------------------
Knowledge comes, but wisdom lingers.
 
Reply
   
Quote
   
Top
   
Bottom
     



Cuchulainn
Senior Member

Posts: 16364
Joined: Jul 2004

Sat Feb 05, 05 07:46 PM
User is offline View users profile

Quote

Originally posted by: daveangel
Look Cuchulain,

You have made three attempts at posting a solution to the SPAAGs problem .. remember the title of the thread "simple C Code".

I have no doubt that the design isnt foolproof but that was not what SPAAG wanted.

Cheers


Daveangel
Ok, point taken. My real reason was to warn about the difficult problem of memory management in C, not a slur on your code, certainly not.

I come across people who write C/C++ programs who are actually unaware of the C nasties.

Cheers also


-------------------------
http://www.datasimfinancial.com/coursesandevents.php
 
Reply
   
Quote
   
Top
   
Bottom
     



Cuchulainn
Senior Member

Posts: 16364
Joined: Jul 2004

Sat Feb 05, 05 08:17 PM
User is offline View users profile

Quote

Originally posted by: daveangel
Look Cuchulain,

You have made three attempts at posting a solution to the SPAAGs problem ..Cheers


first 2 versions have been removed. Solution in C++.

-------------------------
http://www.datasimfinancial.com/coursesandevents.php
 
Reply
   
Quote
   
Top
   
Bottom
     

FORUMS > Programming and Software Forum

Forum Navigation:

© All material, including contents and design, copyright Wilmott Electronic Media Limited - FuseTalk 4.01 © 1999-2010 FuseTalk Inc.