Found 34494 Articles for Programming

How to dynamically allocate a 2D array in C?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

15K+ Views

A 2D array can be dynamically allocated in C using a single pointer. This means that a memory block of size row*column*dataTypeSize is allocated using malloc and pointer arithmetic can be used to access the matrix elements. A program that demonstrates this is given as follows. Example Live Demo #include #include int main() { int row = 2, col = 3; int *arr = (int *)malloc(row * col * sizeof(int)); int i, j; for (i = 0; i < row; i++) ... Read More

Difference between pointer and array in C

Arjun Thakur
Updated on 26-Jun-2020 13:57:26

585 Views

The details about a pointer and array that showcase their difference are given as follows.PointerA pointer is a variable that stores the address of another variable. When memory is allocated to a variable, pointer points to the memory address of the variable. Unary operator ( * ) is used to declare a pointer variable.The following is the syntax of pointer declaration.datatype *variable_name;Here, the datatype is the data type of the variable like int, char, float etc. and variable_name is the name of variable given by user.A program that demonstrates pointers is given as follows.Example Live Demo#include int main () { ... Read More

When to use new operator in C++ and when it should not be used?

Ankith Reddy
Updated on 26-Jun-2020 13:58:34

7K+ Views

Use of the new operator signifies a request for the memory allocation on the heap. If the sufficient memory is available, it initializes the memory and returns its address to the pointer variable.The new operator should only be used if the data object should remain in memory until delete is called. Otherwise if the new operator is not used, the object is automatically destroyed when it goes out of scope. In other words, the objects using new are cleaned up manually while other objects are automatically cleaned when they go out of scope.The following is the syntax of new operator.pointer_variable ... Read More

Color game using Tkinter in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

1K+ Views

For developing GUI applications tkinter is very popular and easy. Using tkinter easily develop GUI games. Here also we are trying to develop color game. In this game the player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds and colors used in this game are Red, Blue, Green, Pink, Black, Yellow, Orange, White, Purple and Brown. The interface will display name of different colors in different colors. User has to identify the color and enter the correct color name ... Read More

Fetching top news using news API in Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

570 Views

News API is very famous API for searching and fetching news articles from any web site, using this API anyone can fetch top 10 heading line of news from any web site. But using this API, one thing is required which is the API key. Example Code import requests def Topnews(): # BBC news api my_api_key="Api_number” my_url = = " https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=my_api_key" my_open_bbc_page = requests.get(my_url).json() my_article = my_open_bbc_page["articles"] my_results = [] for ar in my_article: ... Read More

Minkowski distance in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

1K+ Views

The Minkowski distance is a metric and in a normed vector space, the result is Minkowski inequality. Minkowski distance is used for distance similarity of vector. scipy.spatial.distance.minkowski >>> from scipy.spatial import distance >>> distance.minkowski([1, 0, 0], [0, 1, 0], 1) 2.0 >>> distance.minkowski([1, 0, 0], [0, 1, 0], 2) 1.4142135623730951 >>> distance.minkowski([1, 0, 0], [0, 1, 0], 3) 1.2599210498948732 >>> distance.minkowski([1, 1, 0], [0, 1, 0], 1) 1.0 >>> distance.minkowski([1, 1, 0], [0, 1, 0], 2) 1.0 >>> distance.minkowski([1, 1, 0], [0, 1, 0], 3) 1.0 Example code from math import * from decimal import Decimal ... Read More

Python Program to crawl a web page and get most frequent words

Samual Sam
Updated on 30-Jul-2019 22:30:23

364 Views

Our task is to crawl a web page and count the frequency of the word. And ultimately retrieving most frequent words. First we are using request and beautiful soup module and with the help of these module creating web-crawler and extract data from web page and store in a list. Example code import requests from bs4 import BeautifulSoup import operator from collections import Counter def my_start(url): my_wordlist = [] my_source_code = requests.get(url).text my_soup = BeautifulSoup(my_source_code, 'html.parser') for each_text in my_soup.findAll('div', {'class':'entry-content'}): ... Read More

Python Program to extract email-id from URL text file

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

264 Views

Here we are using regular expression package for extracting email-id from the URL-text file. Given the URL- text file. Using regular expression package we define the pattern of email-id then use findall() function, using this method to check the text which will match with this pattern. Input text= Please contact us at contact@tutorialspoint.com for further information."+\ " You can also give feedback at feedback@tutorialspoint.com" Output ['contact@tutorialspoint.com ', 'feedback@tutorialspoint.com'] Example code import re my_text = "Please contact us at contact@tutorialspoint.com for further information."+\ " You can also give feedback at feedback@tutorialspoint.com" my_emails = re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", ... Read More

Python program to convert float decimal to octal number

AmitDiwan
Updated on 12-Aug-2022 11:57:33

483 Views

Octal Number uses eight digits, 0, 1, 2, 3, 4, 5, 6, 7. Also called as base 8 number system. Each position in an octal number represents a 0 power of the base (8). Last position in an octal number represents a x power of the base (8). Decimal number system has base 10 as it uses 10 digits from 0 to 9. In decimal number system, the successive positions to the left of the decimal point represent units, tens, hundreds, thousands, and so on. Given a float decimal value and input the decimal places number, our task is to ... Read More

Python program to convert floating to binary

AmitDiwan
Updated on 12-Aug-2022 11:54:31

3K+ Views

In this article, we will see how to convert floating-point value to binary. Binary uses two digits, 0 and 1. Also called as base 2 number system Each position in a binary number represents a 0 power of the base (2). Last position in a binary number represents a x power of the base (2). First, we take the integer part from the floating point value and convert it to binary then take fractional part and convert it to binary form and lastly combining both. Let’s say we have the following floating point number − 22.625 Convert decimal 22 ... Read More

Advertisements