In this tutorial, we are going to write a program that finds the result for lagranges's interpolation formula.You don't have write any logic for the program. Just convert the formula into code. Let's see the code.Example Live Demo#include using namespace std; struct Data { int x, y; }; double interpolate(Data function[], int xi, int n) { double result = 0; for (int i = 0; i < n; i++) { double term = function[i].y; for (int j = 0; j < n; j++) { if (j != i) ... Read More
To set the range of Y-axis for a Seaborn boxplot, we can take the following steps −Using set_style() method, set the aesthetic style of the plots.Load the dataset using load_dataset("tips"); need Internet.Using boxplot(), draw a box plot to show distributions with respect to categories.To set the range of Y-axis, use the ylim() method.To display the figure, use the show() method.Examplefrom matplotlib import pyplot as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True sns.set_style("whitegrid") tips = sns.load_dataset("tips") ax = sns.boxplot(x="day", y="total_bill", data=tips) plt.ylim(5, 50) plt.show()OutputRead More
In this tutorial, we are going to learn about largrange's four square theorem.The lagranges's four square theorem states that every natural number can be written as sum of squares of 4 numbers.The following code finds the 4 numbers which satisfies the above condition for the given number n.ExampleLet's see the code. Live Demo#include using namespace std; void printSquareCombinations(int n) { for (int i = 0; i * i
In this tutorial, we are going to write a program that finds the k-th smallest number in the unsorted array.Let's see the steps to solve the problem.Initialise the array and k.Sort the array using sort method.Return the value from the array with the index k - 1.Let's see the code.Example Live Demo#include using namespace std; int findKthSmallestNumber(int arr[], int n, int k) { sort(arr, arr + n); return arr[k - 1]; } int main() { int arr[] = { 3, 5, 23, 4, 15, 16, 87, 99 }, k = 5; cout
In this tutorial, we are going to find the k-th smallest element after every insertion.We are going to use the min-heap to solve the problem. Let's see the steps to complete the program.Initialise the array with random data.Initialise the priority queue.Till k - 1 there won't be any k-th smallest element. So, print any symbol u like.Write a loop that iterates from k + 1 to n.Print the root of the min-heap.If the element is greater than the root of the min-heap, then pop the root and insert the element.ExampleLet's see the code. Live Demo#include using namespace std; void findKthSmallestElement(int ... Read More
In this tutorial, we are going to write a program that finds the k-th prime number which is greater than the given number n.Initialise the number n.Find all the prime numbers till 1e6 and store it in a boolean array.Write a loop that iterates from n + 1 to 1e6.If the current number is prime, then decrement k.If k is equal to zero, then return i.Return -1.ExampleLet's see the code. Live Demo#include using namespace std; const int MAX_SIZE = 1e6; bool prime[MAX_SIZE + 1]; void findAllPrimes() { memset(prime, true, sizeof(prime)); for (int p = 2; p * p ... Read More
In this tutorial, we are going to write a program that finds the k-th odd number from the given array.Let's see the steps to solve the problem.Initialise the array and k.Iterate over the array.If the current element is odd, then decrement the value of k.If k is 0, then return the current element.Return -1.ExampleLet's see the code. Live Demo#include using namespace std; int findKthOddNumber(int arr[], int n, int k) { for (int i = 0; i
To write numerical values on the plot, we can take the following steps −Create points for x and y using numpy.Create labels using xpoints.Use the scatter() method to scatter the points.Iterate labels, xpoints and ypoints and annotate the plot with label, x and y with different properties.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True xpoints = np.linspace(1, 10, 25) ypoints = np.random.rand(25) labels = ["%.2f" % i for i in xpoints] plt.scatter(xpoints, ypoints, c=xpoints) for label, x, y in zip(labels, xpoints, ypoints): plt.annotate( ... Read More
To change the color of a line using radiobutton we can take following steps −Create x and y data points using numpy.Adjust the figure size and padding between and around the subplots.Create a figure and a set of subplots using subplots() method.Plot curve with x and y data points using plot() method.Add an axes to the current figure and make it the current axes, using axes() method.Add a radio button to the current axes.Change the color of the curve with radion button using change_color() method, that can be passed in on_clicked() method.To display the figure use show() method.Exampleimport numpy as ... Read More
To zoom a portion of an image and insert in the same plot, we can take the following steps −Create x and y points, using numpy.To zoom a part of an image, we can make data for x and y points, in that range.Plot x and y points (Step 1), using the plot() method with lw=2, color red and label.Use the legend() method to place text for the plot, Main curve.Create the axes using the axes() method by putting the rectangle’s coordinate.Plot x and y points (Step 2), using the plot() method with lw=1, color='green' and label, i.e., subpart of ... Read More