I was asked to create an assignment with the output: That would look like and execute like the following as long as the number is positive.
Please enter a number: 4
****
***
**
*
**
***
****
This works correctly with the for loop in which was created: However, I was told no for loop or any loop of any matter could be used. I was asked to change this to a recursive method and utilize the call in (2) if else statements. However, I have read all available published paper to change a for loop into recursive but I have been unsuccessful I would greatly appreciate some help to understanding with some in depth clarification.
static void printPattern(int pattern) {
for (int i=0; i<pattern; ++i) {
System.out.print("*");
}
System.out.println();
}
public static void printStars(int lines) {
if (lines<=1) {
printPattern(1);
} else {
printPattern(lines);
printStars(lines-1);
printPattern(lines);
}
}
}
Try
static void printPattern(int pattern) {
if(pattern>0){
System.out.print("*");
printPattern(--pattern);
}else{
System.out.println();
}
}
Related
I am trying to figure out this double recursion problem but I cannot. To visualize, I used the function stack.
if(n==1) {
return 1;
}
else {
return r((n+1)/2) + r(n/2);
}
}
Using the stack can someone help me understand how to get the answer if int x = r(10)?
I want to change the function inserted at the timing before the instruction with the same address.
What should I do?
For,example.
int count=10;
void insert_check_code(INS ins){
if(INS_Address(ins) == tmpaddr)
if(count > 5){
INS_InsertCall(ins,IPOINT_BEFORE,count--func)
}else {
INS_InsertCall(ins,IPOINT_BEFORE,count_printfunc)
}
}
In the above example,The count value returns to its original I want to change the function inserted at the timing before the instruction with the same address.
What should I do?
For,example.
int count=10;
void insert_check_code(INS ins){
if(INS_Address(ins) == tmpaddr)
if(count > 5){
INS_InsertCall(ins,IPOINT_BEFORE,count--func)
}else {
INS_InsertCall(ins,IPOINT_BEFORE,count_printfunc)
}
}
In the above example,The count value returns to its original value.
The target program is a simple server program, so we are using the fork () function.
Is it necessary to write a special description in Pintool for a program using the fork () function?value.
Move the if-else code into its own function and insert that function instead:
void conditional_func() {
if(count > 5){
count_decrement_func()
} else {
count_printfunc()
}
}
void insert_check_code(INS ins) {
INS_InsertCall(ins,IPOINT_BEFORE,conditional_func)
}
Regarding fork(), it depends on what behavior you want when fork()ing.
Keep in mind that if the application is multi-threaded, count access must be synchronized.
I used to depend on the package RcppProgress to check for user abortion inside a long loop with Progress::check_abort(). But I just received an email from the CRAN team to tell me (and to other maintainers) that RcppProgress has bugs and will be removed soon in absence of maintainer (actually it seems already removed). Is there another way to check for abortion?
I found that R_CheckUserInterrupt exists. How to change my code to use this function? In Writing R extensions the function returns void so I do not understand how it works. It seems to exit immediately.
Rcpp::checkUserInterrupt seems to present the same behavior. And R: How to write interruptible C++ function, and recover partial results presents a kind of hack not recomented by its author. I would like to exit the loop correctly cleaning object allocated on the heap and returning partial output
// [[Rcpp::depends(RcppProgress)]]
#include <progress.hpp>
#include <Rcpp.h>
// [[Rcpp::export]]
SEXP f()
{
for( int i = 0 ; i < 100000 ; i++)
{
if (Progress::check_abort())
{
delete some_var;
return partial_output;
}
else
//do_stuff();
}
}
After reading the sources of Rcpp I found that Rcpp::checkUserInterrupt() throw an internal::InterruptedException. This works:
for (long i = 0 ; i < 100000000 ; i++)
{
try
{
Rcpp::checkUserInterrupt();
}
catch(Rcpp::internal::InterruptedException e)
{
delete some_var;
return partial_output;
}
}
It is slow but exactly like Process::check_abort. Optionally, as advised in Rcpp Attributes, one can check only every 100 or 1000 iteration to speed up the code.
for (long i = 0 ; i < 100000000 ; i++)
{
if (i % 100 == 0)
{
try
{
Rcpp::checkUserInterrupt();
}
catch(Rcpp::internal::InterruptedException e)
{
delete some_var;
return partial_output;
}
}
}
so I have a program that is running a bunch of different recursive methods, and I cannot get it to compile/run. The error is in this method, according to my computer:
public static int fibo(int n)
// returns the nth Fibonacci number
{
if (n==0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else if (n>1)
{
return fibo(n-1) + fibo(n-2);
}
}
I have this method called correctly in my main method, so the issue is in this bit of code.
I think I can help you in this. Add return n; after your else if. Outside of the code but before the last curlicue.
The code will work as long as n ≥ 0 btw; another poster here is right in that you may want to add something to catch that error.
Make sure all possible paths have a return statement. In your code, if n < 0, there is no return statement, the compiler recognizes this, and throws the error.
public static int fibo(int n)
// returns the nth Fibonacci number
{
if (n<=0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else // All other cases, i.e. n >= 1
{
return fibo(n-1) + fibo(n-2);
}
}
Consider these C functions:
#define INDICATE_SPECIAL_CASE -1
void prepare (long *length_or_indicator);
void execute ();
The prepare function is used to store a pointer to a delayed long * output variable.
It can be used in C like this:
int main (void) {
long length_or_indicator;
prepare (&length_or_indicator);
execute ();
if (length_or_indicator == INDICATE_SPECIAL_CASE) {
// do something to handle special case
}
else {
long length = lengh_or_indicator;
// do something to handle the normal case which has a length
}
}
I am trying to achieve something like this in Vala:
int main (void) {
long length;
long indicator;
prepare (out length, out indicator);
execute ();
if (indicator == INDICATE_SPECIAL_CASE) {
// do something to handle special case
}
else {
// do something to handle the normal case which has a length
}
}
How to write the binding for prepare () and INDICATE_SPECIAL_CASE in Vala?
Is it possible to split the variable into two?
Is it possible to avoid using pointers even though the out variable is written to after the call to prepare () (in execute ())?
The problem with using out is that Vala is going to generate lots of temporary variables along the way, which will make the reference wrong. What you probably want to do is create a method in your VAPI that hides all this:
[CCode(cname = "prepare")]
private void _prepare (long *length_or_indicator);
[CCode(cname = "execute")]
private void _execute ();
[CCode(cname = "prepare_and_exec")]
public bool execute(out long length) {
long length_or_indicator = 0;
prepare (&length_or_indicator);
execute ();
if (length_or_indicator == INDICATE_SPECIAL_CASE) {
length = 0;
return false;
} else {
length = lengh_or_indicator;
return true;
}
}