Recursive Functions return value - c

I don't seem to understand the logic behind this particular code. I don't understand why the answer is 18. You can check for the answer in a compiler as well. Anyone wh understands the logic please let me know.
Here's the code:
#include <stdio.h>
int GuessMe(int,int);
main() {
printf("%d", GuessMe(8,2));
}
int GuessMe(int x, int y) {
if ( y > x)
return x
else
return GuessMe(x-2, y+2) + x;
}

Initially, GuessMe is passed x=8,y=2:
x | y | y > x ? | initial return value | final return value
8 | 2 | NO | GuessMe(6, 4) + 8 | 18
6 | 4 | NO | GuessMe(4, 6) + 6 | 10
4 | 6 | YES | 4 | 4
Read down the initial return value column and then read back up the final return value column once you hit an initial return value that isn't recursive.

Related

How to create a responsive table using C?

I want create a responsive table using C, not C++ or C#, only the old C.
Basically, I create a multiplication table and put lines and borders using the symbols + - and |, but when I use a number with a width greater than one, they are disorganized, so I would like to know some way that, when I put this number, the lines follow it. My code, the actual output and the desired output:
int endTab, selecNum, CurrentRes;
printf("\n\t+----------------------+");
printf("\n\t| multiplication table |");
printf("\n\t+----------------------+\n\n\n");
printf("Enter the table number:");
scanf("%d", &selecNum);
printf("Enter which number will end in:");
scanf("%d", &endTab);
printf("\n\t+-------+---+\n");
// | 1 x 2 | 2 |
for (int i = 1; i <= endTab; i++){
CurrentRes = i*selecNum;
printf("\t| %d x %d | %d |\n", i, selecNum, CurrentRes);
printf("\t+-------+---+\n");
}
return 0;
current output
+----------------------+
| multiplication table |
+----------------------+
Enter the table number:1
Enter which number will end in:10
+-------+---+
| 1 x 1 | 1 |
+-------+---+
| 2 x 1 | 2 |
+-------+---+
| 3 x 1 | 3 |
+-------+---+
| 4 x 1 | 4 |
+-------+---+
| 5 x 1 | 5 |
+-------+---+
| 6 x 1 | 6 |
+-------+---+
| 7 x 1 | 7 |
+-------+---+
| 8 x 1 | 8 |
+-------+---+
| 9 x 1 | 9 |
+-------+---+
| 10 x 1 | 10 |
+-------+---+
expected output
+----------------------+
| multiplication table |
+----------------------+
Enter the table number:1
Enter which number will end in:10
+--------+----+
| 1 x 1 | 1 |
+--------+----+
| 2 x 1 | 2 |
+--------+----+
| 3 x 1 | 3 |
+--------+----+
| 4 x 1 | 4 |
+--------+----+
| 5 x 1 | 5 |
+--------+----+
| 6 x 1 | 6 |
+--------+----+
| 7 x 1 | 7 |
+--------+----+
| 8 x 1 | 8 |
+--------+----+
| 9 x 1 | 9 |
+--------+----+
| 10 x 1 | 10 |
+--------+----+
Things to note:
The output has two columns and you have to maintain width of both the columns for each row.
The maximum width of column 1 is width of selectNum x endTab including leading and trailing space character.
The maximum width of column 2 is the width of result of selectNum x endTab including leading and trailing space.
The length of separator after every row will be based on the maximum width of both the columns.
+---------------+-------+
\ / \ /
+-----------+ +---+
| |
max width max width
of col 1 of col 2
You can do:
#include <stdio.h>
#define SPC_CHR ' '
#define BIND_CHR '+'
#define HORZ_SEP_CH '-'
#define VERT_SEP_CH '|'
#define MULT_OP_SIGN 'x'
void print_label (void) {
printf("\n\t+----------------------+");
printf("\n\t| multiplication table |");
printf("\n\t+----------------------+\n\n\n");
}
void print_char_n_times (char ch, int n){
for (int i = 0; i < n; ++i) {
printf ("%c", ch);
}
}
void print_row_sep (int max_w1, int max_w2) {
printf ("\t%c", BIND_CHR);
print_char_n_times (HORZ_SEP_CH, max_w1);
printf ("%c", BIND_CHR);
print_char_n_times (HORZ_SEP_CH, max_w2);
printf ("%c\n", BIND_CHR);
}
void print_multiplication_row (int m1, int m2, int max_w1, int max_w2) {
printf ("\t%c", VERT_SEP_CH);
int nc = printf ("%c%d%c%c%c%d%c", SPC_CHR, m1, SPC_CHR, MULT_OP_SIGN, SPC_CHR, m2, SPC_CHR);
if (nc < max_w1) {
print_char_n_times (SPC_CHR, max_w1 - nc);
}
printf ("%c", VERT_SEP_CH);
nc = printf ("%c%d%c", SPC_CHR, m1 * m2, SPC_CHR);
if (nc < max_w2) {
print_char_n_times (SPC_CHR, max_w2 - nc);
}
printf ("%c\n", VERT_SEP_CH);
}
void print_multiplication_table (int m1, int m2) {
int col1_max_width = snprintf (NULL, 0, "%c%d%c%c%c%d%c", SPC_CHR, m1, SPC_CHR, MULT_OP_SIGN, SPC_CHR, m2, SPC_CHR);
int col2_max_width = snprintf (NULL, 0, "%c%d%c", SPC_CHR, m1 * m2, SPC_CHR);
for (int i = 0; i < m2; ++i) {
print_row_sep (col1_max_width, col2_max_width);
print_multiplication_row(m1, i + 1, col1_max_width, col2_max_width);
}
print_row_sep (col1_max_width, col2_max_width);
}
int main (void) {
int endTab, selecNum;
print_label();
printf("Enter the table number: ");
scanf("%d", &selecNum);
printf("Enter which number will end in: ");
scanf("%d", &endTab);
print_multiplication_table (selecNum, endTab);
return 0;
}
Output:
% ./a.out
+----------------------+
| multiplication table |
+----------------------+
Enter the table number: 1
Enter which number will end in: 10
+--------+----+
| 1 x 1 | 1 |
+--------+----+
| 1 x 2 | 2 |
+--------+----+
| 1 x 3 | 3 |
+--------+----+
| 1 x 4 | 4 |
+--------+----+
| 1 x 5 | 5 |
+--------+----+
| 1 x 6 | 6 |
+--------+----+
| 1 x 7 | 7 |
+--------+----+
| 1 x 8 | 8 |
+--------+----+
| 1 x 9 | 9 |
+--------+----+
| 1 x 10 | 10 |
+--------+----+
Note that if you want output in the way you have shown, i.e. like this -
+--------+----+
| 1 x 1 | 1 |
+--------+----+
| 2 x 1 | 2 |
+--------+----+
| 3 x 1 | 3 |
+--------+----+
....
....
+--------+----+
| 10 x 1 | 10 |
+--------+----+
then make following change in the statement of for loop of function print_multiplication_table():
print_multiplication_row(i + 1, m1, col1_max_width, col2_max_width);
^^^^^^^^^
arguments swapped
A couple of points:
If you want to maintain the width at the level of numbers printed one down other, in the first column, then calculate the width of maximum digit entered by the user and use it while printing the multiplication row.
Above program is just to show you the way to get the output in desired form. Leaving it up to you to do all sort of optimisations that you can do.
Read about printf() family functions. Read about sprintf(), snprintf() and their return type etc.
This is not the complete solution, but you may be able to work out exactly what you want/need based on the ideas here. (The key ingredient is that log10(), a math library function) will tell how much horizontal space will be needed. Feed it the largest value in each of the 3 numbers columns and you determine the widths needed from that.
#include <stdio.h>
#include <math.h> // for log10()
int demo( int m0, int m1 ) {
char buf[ 100 ]; // adequate
int wid0 = (int)( log10( m0 ) + 1);
int wid1 = (int)( log10( m1 ) + 1);
int widR = (int)( log10( m0 * m1 ) + 1);
int need = 0;
need++; // left 'box'
need++; // space
need += wid0; // first number
need += strlen( " x " ); // mult
need += wid1; // second number
need += strlen( " | " ); // middle box
need += widR; // result
need++; // space
need++; // right 'box'
memset( buf, '\0', sizeof buf ); // start null
memset( buf, '-', need );
puts( buf );
printf( "| %*d x %*d | %*d |\n\n", wid0, m0, wid1, m1, widR, m0 * m1 );
return 0;
}
int main() {
demo( 24, 25 );
demo( 15, 456 );
return 0;
}
Output:
-----------------
| 24 x 25 | 600 |
-------------------
| 15 x 456 | 6840 |
Use the %n directive to gather how many bytes have been printed up to a point and work from there to write your '-' printing loop, for example:
int field_width;
snprintf(NULL, 0, "%d%n\n", 420, &field_width);
char horizontal[field_width + 1];
memset(horizontal, '-', field_width);
horizontal[field_width] = '\0';
Now you can print a horizontal string that's the same width as 420 when printed. Part of your problem is solved by this.
I've adapted my initial example to use snprintf because it occurs to me that you need to work out the column widths from the largest numbers first. In your print loop you'll want to pad out each value to field_width wide; you could use %*d (right justified, space padded) or %-*d (left justified, space padded) or %.*d (zero prefix padded), depending on your choice, for example:
printf("%*d\n", field_width, 1);
... and there's the rest of your problem solved, if I am correct.

Recursive factorial return statement

Why do we use return 1 to terminate the recursive function? Can any other value be used as a default value like 1.
And if we return 1 as return value of the function, then why 1 is not returned to main function.
#include<stdio.h>
int fact(int n)
{
if(n>=1)
return (n*fact(n-1));
else
return 1;
}
int main()
{
int a,ans;
scanf("%d",&a);
ans=fact(a);
printf("factorial of %d is %d ",a,ans);
return 0;
}
/*
explanation
fact(4);
if(4>=1) 4*fact(3)
if(3>=1) 4*3*fact(2)
if(2>=1) 4*3*2*fact(1)
if(1>=1) 4*3*2*1*fact(0)
if(0>=1) return 1;
*/
int fact(int n)
{
if (n >= 1)
return n * fact(n-1);
else
return 1;
}
Each time the fact() function is called, it runs either the return n * fact(n-1); statement OR the return 1; statement but not both.
You call fact(4) in main(). This is how it runs:
main:
compute fact(4)
fact(4):
| 4 >= 1? Yes!
| compute 4 * fact(3)
| fact(3):
| | 3 >= 1? Yes!
| | compute 3 * fact(2)
| | fact(2):
| | | 2 >= 1? Yes!
| | | compute 2 * fact(1)
| | | fact(1):
| | | | 1 >= 1? Yes!
| | | | compute 1 * fact(0)
| | | | fact(0):
| | | | | 0 >= 1? NO!
| | | | | return 1;
| | | | +--> 1
| | | | fact(0) is 1, return 1 * 1 (--> 1)
| | | +--> 1
| | | fact(1) is 1, return 2 * 1 (--> 2)
| | +--> 2
| | fact(2) is 2, return 3 * 2 (--> 6)
| +--> 6
| fact(5) is 6, return 4 * 6 (--> 24)
+--> 24
fact(4) is 24, assign it to `ans`, print it etc
// end of main
When a function uses the return statement (or after it executes its last statement if a return is not reached, the control is passed back to the expression that has called it.
why do we use "return 1" to terminate the recursive function
Because this is supposed to cover the case for when n is not >=1, in other words when n is 0. I don't think negative n are valid. 0! is 1, hence why it returns that value.
And if we return 1 as end of function, then why 1 is not returned to
main function.
If the function is called with 0 or 1 for n, then 1 is returned to the main function. For any other value, 1 is only returned in the recursive factorial calls and the value which is returned to the main function isn't 1, but (n*fact(n-1)), which isn't 1 in those cases.
The return statement is executed when n==0.
The factorial for n==0 is 1, so we return 1.
/*
explanation
fact(4);
if(4>=1) 4*fact(3)
if(3>=1) 4*3*fact(2)
if(2>=1) 4*3*2*fact(1)
if(1>=1) 4*3*2*1*fact(0)
if(0>=1) return 1; now return is default tend to multiply as we give 1 and
return has already 24 in its stack so 1*24 is returned to main()
if we give return 2; 2*24 is returned to main();
*/
we don't want our final result to be affected, to solve that error, anything multiple by 1 will result the same, so we use 1 as return in recursive function.
Actually return is also a stack register which holds temporary variable while calling with in the function, and it operates default by multiply property, as we can send only one return value always.

Storing user-defined logic in a database

I'm designing a database to store information about events that are dynamic in nature. What I mean by this is that, each type of event will have some variables attached to them that changes on each occurrence based on some rules defined by the user.
Let's say we have Event Type A with variable X and Y. In this event type, the user can define some rules that determines the value of X and Y on each occurrence of the event.
An example of a set of rules a user might define:
On first occurrence, X = 0; Y = 0;
On each occurrence, X = X + 1;
On each occurrence, if X == 100 then { X = 0; Y = Y + 1 }
By defining these rules, the value of X and Y changes dynamically on all occurrences of the event as follow:
1st occurrence: X = 1, Y = 0
2nd occurrence: X = 2, Y = 0
...
100th occurrence: X = 0, Y = 1
Now, I'm not sure how to store the "user-defined rules" in a database and later query them in my code. Can anyone point me in the right direction? Here's a start:
EVENTS
id;
name;
description;
event_type;
EVENT_TYPE_A_OCCURRENCES
id;
event_id;
X;
Y;
EVENT_RULES
id;
event_id;
frequency; // the frequency in which this rule applies
at_occurrence; // apply this rule at a specific occurrence
condition; // stores the code for the condition
statements; // stores the code for the statements
I'm no expert, please help me solve this problem. Thank you.
Assume following user defined rules stored in table:
-----------------------------------------------------------------
|eventid|occurance|keep-old-x|keep-old-y|x-frequency|y-frequency|
-----------------------------------------------------------------
| A | 1 | T | F | 1 | 100 |
-----------------------------------------------------------------
| B | 2 | F | T | -2 | 0 |
-----------------------------------------------------------------
| C | 5 | T | T | 100 | -3 |
-----------------------------------------------------------------
Lets say before event X = 10, Y = 12.
Event = A, ocuuurance = 1, keep-old-x = T, keep-old-y = F, x-frequency = 1, y-frequency = 100
if keep-old-x is T then
X = X + x-frequency
else
X = x-frequency
endif
if keep-old-y is T then
Y = Y + y-frequency
else
Y = y-frequency
endif
Now, X = 11, Y = 100
You may need to add two more columns to change value of X variable on specific value; as:
--------------------------
|if-x-value| x-new-value |
--------------------------
| 100 | 0 |
--------------------------
| 125 | 5 |
--------------------------
| 150 | 10 |
--------------------------
I hope this helps.

Binary chop search algorithm in C

I'm trying to write a binary chop search function in C and having some issues. First of all, after finding the midpoint values, the function isn't even entering any of the if loops.
The function is this:
int binarychopsearch(int i, int *array, int min, int N) {
min = 0;
int max = N - 1;
printf("Min = %d, Max = %d\n", min, max);
printf("i = %d\n", i);
int mid = (min + max)/2;
//printf("midpoint = %d\n", mid);
printf("array[%d] = %d\n", mid, array[mid]);
if (i < array[mid]) {
printf("in this loop\n");
printf("i = %d, array[mid] = %d\n", i, array[mid]);
// key is in lower subset
return binarychopsearch(i, array, min, mid - 1);
}
else if (i > array[mid]) {
printf("in the greater than loop\n");
printf("i = %d, array[mid] = %d\n", i, array[mid]);
return binarychopsearch(i, array, mid + 1, max);
}
else
//if (i = array[mid]) {
return mid;
}
I'm not including the main where the input values come from as I think the issue is in this function. It is compiling and running, but not entering the loops so not finding the location of the "i" value. I'm pretty stuck on this as I can't see where it's going wrong.
Any help is really appreciated!
Thanks
If i is larger (or equal) to array[mid] then you unconditionally return from the function. Instead you then should check the next condition, and then if that is false as well you know you have found the value you were looking for.
So it should look something like
if (i < array[mid])
{
...
}
else if (i > array[mid])
{
...
}
else
return mid;
There are other problems with your code as well.
Lets say you have the following array
int array[] = { 1, 2, 3, 4, 5 };
and you are looking for the value 5.
The calls will be like this:
| Call# | min | N | max | mid | array[mid] |
| 1 | 0 | 5 | 4 | 2 | 3 |
| 2 | 3 | 4 | 3 | 3 | 4 |
| 3 | 4 | 3 | 2 | 2 | 3 |
| 4 | 3 | 2 | 1 | 2 | 3 |
| 5 | 3 | 1 | 0 | 1 | 2 |
| 6 | 2 | 0 | -1 | 1 | 2 |
.
.
.
It's quite clear that this will not end well. In fact, you will soon end up with negative index, and that leaves you in the territory of undefined behavior.
I suggest you create your own table like this, on paper, when trying to fix your algorithm, for both cases of recursion.
If you don't call the recursive function with max but with N instead, i.e.
return binarychopsearch(i, array, mid + 1, N);
then you will have the following calls
| Call# | min | N | max | mid | array[mid] |
| 1 | 0 | 5 | 4 | 2 | 3 |
| 2 | 3 | 5 | 4 | 3 | 4 |
| 3 | 4 | 5 | 4 | 4 | 5 |
So the third call found the number, and returns the index 4.
You should also change the first call:
return binarychopsearch(i, array, min, mid);
The problem is when i >= array[mid]: you systematically return -1: use esle if
if(i < array[mid])
{}
else if(i > array[mid])
{}
else // i == array[mid]
{}

Weighted random integers

I want to assign weightings to a randomly generated number, with the weightings represented below.
0 | 1 | 2 | 3 | 4 | 5 | 6
─────────────────────────────────────────
X | X | X | X | X | X | X
X | X | X | X | X | X |
X | X | X | X | X | |
X | X | X | X | | |
X | X | X | | | |
X | X | | | | |
X | | | | | |
What's the most efficient way to do it?
#Kerrek's answer is good.
But if the histogram of weights is not all small integers, you need something more powerful:
Divide [0..1] into intervals sized with the weights. Here you need segments with relative size ratios 7:6:5:4:3:2:1. So the size of one interval unit is 1/(7+6+5+4+3+2+1)=1/28, and the sizes of the intervals are 7/28, 6/28, ... 1/28.
These comprise a probability distribution because they sum to 1.
Now find the cumulative distribution:
P x
7/28 => 0
13/28 => 1
18/28 => 2
22/28 => 3
25/28 => 4
27/28 => 5
28/28 => 6
Now generate a random r number in [0..1] and look it up in this table by finding the smallest x such that r <= P(x). This is the random value you want.
The table lookup can be done with binary search, which is a good idea when the histogram has many bins.
Note you are effectively constructing the inverse cumulative density function, so this is sometimes called the method of inverse transforms.
If your array is small, just pick a uniform random index into the following array:
int a[] = {0,0,0,0,0,0,0, 1,1,1,1,1,1, 2,2,2,2,2, 3,3,3,3, 4,4,4, 5,5, 6};
If you want to generate the distribution at runtime, use std::discrete_distribution.
To get the distribution you want, first you basically add up the count of X's you wrote in there. You can do it like this (my C is super rusty, so treat this as pseudocode)
int num_cols = 7; // for your example
int max;
if (num_cols % 2 == 0) // even
{
max = (num_cols+1) * (num_cols/2);
}
else // odd
{
max = (num_cols+1) * (num_cols/2) + ((num_cols+1)/2);
}
Then you need to randomly select an integer between 1 and max inclusive.
So if your random integer is r the last step is to find which column holds the r'th X. Something like this should work:
for(int i=0;i<num_cols;i++)
{
r -= (num_cols-i);
if (r < 1) return i;
}

Resources