Can any one help to show how to write a recursive function that accepts three parameters : a postive integer of 4 digits , and two adjecent digits with in this number and then swap their postions .for example : if the number is 1684 and the two digits are 68 then the out put will be 1864. or : if the number is 1221 and the ditgits are 1 and 2 then the out put will be 2112 .thanks
C++ : recursive function PLZ?
As shamus is saying this is a very strange problem to practice recursion.
Recursion's goal is to split a problem into many smaller parts until they become trivial to solve, and then recompose the smaller parts into the solution to the full problem.
Here, one way I see to apply this method to the problem, would be at each stage to simplify the initial number by trimming any starting or ending part that are not the two number to invert. At some point (after 2 steps in this case since we start with 4 digits numbers) you get only the two concerned numbers which you can invert if found or not if incorrect. Then each parent will replug the digit it trimmed, and recompose the original number with it's specified numbers inverted.
Obviously this is VERY inefficient. But it's a way to do it.
Hope that help
Reply:well...
a recrusive function is a function that calls itself...
IMHO it is bad coding, you could use loops for any situation that you may want to use recrusion for, and it would be easier to follow, and are faster to run. it is also alot easier to wind up with a forever loop in a recrusion situation
in fact, i can't seem to think of a way to use direct recrusion for what you want to do. indirect(where one function calls another, then that function calls the first one) maybe. but not a single function.
I would just use1 function, that breaks down the int into single digits, then compairs to swap values, rebuilds the var, then spit it out. no need for even a loop.
sorry.
Reply:Your task is a little ambiguous, but basically you are substituting one digit for another one. Here's a rather quick implementation :
int recurse(const int input, const int replace1, const int replace2)
{
int lastdigit= input %10;
// compare with search pattern
if (lastdigit == replace1) lastdigit= replace2;
else
if (lastdigit == replace2) lastdigit= replace1;
// send the rest of the number except for last digit
if (input%26gt;=10)
return lastDigit+ 10*recurse(input/10, replace1, replace2);
else
return lastDigit;
}
What it does is it splits off the final digit, and either keeps it intact or replaces it. Then the other digits are processed recursively and when the whole call unwinds they each get multiplied by 10 again to restore the whole number. For your understanding please try this out with a sample and write down exactly what number goes in and how the recursive function is being called.
Tuesday, July 14, 2009
How to call Javascript function from Visual C++?
How to establish connections between the two languages?
I created a small program in Visual C++. In my program, I want to call the function that is coded in Javascript. Is there a way to do this?
I tried to browse this in the internet and found a good source for me to implement it but the source codes are too complicated for me.. If you can answer my problem and if it they are codes please indicate their purpose and where to place them...
Please help...
Thanks...
How to call Javascript function from Visual C++?
Hmmmm
Honestly sounds impossible, but I suggest you look for a C++ to JavaScript translator. C++ is a whole different horizon to JavaScript...
sweet pea
I created a small program in Visual C++. In my program, I want to call the function that is coded in Javascript. Is there a way to do this?
I tried to browse this in the internet and found a good source for me to implement it but the source codes are too complicated for me.. If you can answer my problem and if it they are codes please indicate their purpose and where to place them...
Please help...
Thanks...
How to call Javascript function from Visual C++?
Hmmmm
Honestly sounds impossible, but I suggest you look for a C++ to JavaScript translator. C++ is a whole different horizon to JavaScript...
sweet pea
What is the C File Function to delete a letter say, 'A' in a File. Anything like fgetc, fputc etc?
I have developed a program for Bitstuffing. It scans for characters from a file, and if it encounters five 1's continuously, it will append a '0' after it and write to a new file. This concept is called Bit Stuffing. Now, I need to know how to remove that '0' in the file I wrote.
==##CODE ##==
FILE *fp1,*fp2; //Couple of File pointers
int i=0; //Counter
char c; //variable to get char from File
fp1=fopen("bit.txt","r"); //Open file in Read mode
fp2=fopen("b1.txt","w");//Open file in Write mode
while(!feof(fp1))
{
c=fgetc(fp1); //scan char by char till EOF
if(c=='1') i+=1;
else if(c=='0') i=0;
fputc(c,fp2);
if(i==5)
{
fputc('0',fp2); //append 0 after five 1s and reset i
i=0;
}
fclose(fp1);
fclose(fp2);
==## END OF CODE ##==
bit.txt content: 111111
b1.txt content: 1111101
How to remove that 0?
Help would be much appreciaited.
What is the C File Function to delete a letter say, 'A' in a File. Anything like fgetc, fputc etc?
Try this method:
1) Read b1.txt
2) Check for five 1's write the contents of b1.txt into a new file temp.txt
3) Once you have written it . delete b1.txt
4) rename temp.txt as b1.txt
Program
fp3=fopen("temp.txt","w");
rewind(fp2);
i=0;
while(!feof(fp2))
{
if (i%26lt;=5)
{
c=fgetc(fp2);
fputc(c,fp3);
if (c==1)
i++;
if(i==5)
i=0;
}
}
fclose(fp2);
fclose(fp3);
remove("b1.txt");
rename("temp.txt","b1.txt");
Reply:you are most welcome...why dont you send that program to my mail. Its ibndawood@gmail.com. Thanks in advance Happy Programming! Report It
Reply:Or else you may contact a C expert. Check websites like http://getafreelnacer.com/
==##CODE ##==
FILE *fp1,*fp2; //Couple of File pointers
int i=0; //Counter
char c; //variable to get char from File
fp1=fopen("bit.txt","r"); //Open file in Read mode
fp2=fopen("b1.txt","w");//Open file in Write mode
while(!feof(fp1))
{
c=fgetc(fp1); //scan char by char till EOF
if(c=='1') i+=1;
else if(c=='0') i=0;
fputc(c,fp2);
if(i==5)
{
fputc('0',fp2); //append 0 after five 1s and reset i
i=0;
}
fclose(fp1);
fclose(fp2);
==## END OF CODE ##==
bit.txt content: 111111
b1.txt content: 1111101
How to remove that 0?
Help would be much appreciaited.
What is the C File Function to delete a letter say, 'A' in a File. Anything like fgetc, fputc etc?
Try this method:
1) Read b1.txt
2) Check for five 1's write the contents of b1.txt into a new file temp.txt
3) Once you have written it . delete b1.txt
4) rename temp.txt as b1.txt
Program
fp3=fopen("temp.txt","w");
rewind(fp2);
i=0;
while(!feof(fp2))
{
if (i%26lt;=5)
{
c=fgetc(fp2);
fputc(c,fp3);
if (c==1)
i++;
if(i==5)
i=0;
}
}
fclose(fp2);
fclose(fp3);
remove("b1.txt");
rename("temp.txt","b1.txt");
Reply:you are most welcome...why dont you send that program to my mail. Its ibndawood@gmail.com. Thanks in advance Happy Programming! Report It
Reply:Or else you may contact a C expert. Check websites like http://getafreelnacer.com/
C++ predicate function?
I have no idea where to start with this question:
Write a predicate function
bool same_set(vector%26lt;int%26gt; a, vector%26lt;int%26gt; b)
that checks whether two vectors have the same elements in some order, ignoring multiplicities. For example, the two vectors
1 4 9 16 9 7 4 9 11
and
11 11 7 9 16 4 1
would be considered identical. You will probably need one or more helper functions.
C++ predicate function?
There are pretty smart techniques to do this (involving search trees and such), but I'll keep it simple here.
A mathematician would say that two sets A and B are equal if and only if A is a subset of B and B is a subset of A. Or:
bool same_set(vector%26lt;int%26gt; a, vector%26lt;int%26gt; b)
{
if (!is_subset(a,b))
return false;
if (!is_subset(b,a))
return false;
return true;
}
A 'dumb' way to check wether A is a subset of B is checking wether each element of A is in B:
bool is_subset(vector%26lt;int%26gt; subset, vector%26lt;int%26gt; set)
{
vector%26lt;int%26gt;::iterator iter;
for (iter = subset.begin(); iter %26lt; subset.end(); iter++)
{
if (!is_in(*iter, set))
return false;
}
return true;
}
A way to check wether something is part of a set should be trivial; just keep searching for it until you've found it in the set:
bool is_in(int element, vector%26lt;int%26gt; set)
{
vector%26lt;int%26gt;::iterator iter;
for(iter = set.begin(); iter != set.end(); iter++)
{
if (*iter == element)
return true;
}
return false;
}
bottle palm
Write a predicate function
bool same_set(vector%26lt;int%26gt; a, vector%26lt;int%26gt; b)
that checks whether two vectors have the same elements in some order, ignoring multiplicities. For example, the two vectors
1 4 9 16 9 7 4 9 11
and
11 11 7 9 16 4 1
would be considered identical. You will probably need one or more helper functions.
C++ predicate function?
There are pretty smart techniques to do this (involving search trees and such), but I'll keep it simple here.
A mathematician would say that two sets A and B are equal if and only if A is a subset of B and B is a subset of A. Or:
bool same_set(vector%26lt;int%26gt; a, vector%26lt;int%26gt; b)
{
if (!is_subset(a,b))
return false;
if (!is_subset(b,a))
return false;
return true;
}
A 'dumb' way to check wether A is a subset of B is checking wether each element of A is in B:
bool is_subset(vector%26lt;int%26gt; subset, vector%26lt;int%26gt; set)
{
vector%26lt;int%26gt;::iterator iter;
for (iter = subset.begin(); iter %26lt; subset.end(); iter++)
{
if (!is_in(*iter, set))
return false;
}
return true;
}
A way to check wether something is part of a set should be trivial; just keep searching for it until you've found it in the set:
bool is_in(int element, vector%26lt;int%26gt; set)
{
vector%26lt;int%26gt;::iterator iter;
for(iter = set.begin(); iter != set.end(); iter++)
{
if (*iter == element)
return true;
}
return false;
}
bottle palm
I need a function code in c that can delete an allocated structure?
Pls. send to me the codes.....thank you....A function that can delete an allocated structure in c.....
I need a function code in c that can delete an allocated structure?
Hi,
Suppose ur struct pointer is a
struct structure *a;
free(a);
free() releases the memory block allotted to *a pointer variable.
I need a function code in c that can delete an allocated structure?
Hi,
Suppose ur struct pointer is a
struct structure *a;
free(a);
free() releases the memory block allotted to *a pointer variable.
C++ array function involving standard deviation?
I need a function that takes two arguments: first argument is an array of numbers and the second argument is the size of the array, and the function returns the standard deviation of those numbers (all of type double).
C++ array function involving standard deviation?
#include %26lt;math.h%26gt;
double stddev(const double input[], size_t length)
{
double variance=0.0f;
double avg=0.0f;
size_t idx;
for (idx=0; idx%26lt;length; idx++)
{
avg+=input[idx] ;
}
avg/=length;
for (idx=0; idx%26lt;length; idx++)
{
variance+=(input[idx]-avg) * (input[idx]-avg);
}
variance/=length;
return sqrt(variance);
}
C++ array function involving standard deviation?
#include %26lt;math.h%26gt;
double stddev(const double input[], size_t length)
{
double variance=0.0f;
double avg=0.0f;
size_t idx;
for (idx=0; idx%26lt;length; idx++)
{
avg+=input[idx] ;
}
avg/=length;
for (idx=0; idx%26lt;length; idx++)
{
variance+=(input[idx]-avg) * (input[idx]-avg);
}
variance/=length;
return sqrt(variance);
}
C++ array function involving standard deviation?
I need a function that takes two arguments: first argument is an array of numbers and the second argument is the size of the array, and the function returns the standard deviation of those numbers (all of type double).
C++ array function involving standard deviation?
http://hep.ph.liv.ac.uk/~ajw/cppcourse/l... is your target, go there and enjoy C++ Language
Reply:nice link Report It
C++ array function involving standard deviation?
http://hep.ph.liv.ac.uk/~ajw/cppcourse/l... is your target, go there and enjoy C++ Language
Reply:nice link Report It
Subscribe to:
Posts (Atom)