Found 34486 Articles for Programming

Initializing array with variable vs a real number in C++

Arjun Thakur
Updated on 26-Jun-2020 14:12:22

228 Views

An array is a collection of same type of elements at contiguous memory location. The lowest address in the array corresponds to the first element while highest address corresponds to the last element. The array index starts with zero(0) and ends with the size of array minus one(array size - 1).An array can be initialized with variables as well as real numbers. A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; int main() {    int a = 5;    int b = 3;    int arr[4];    arr[0] = a;    arr[1] = 8;    arr[2] = b;    arr[3] = 2;    cout

How to return local array from a C++ function?

Ankith Reddy
Updated on 26-Jun-2020 14:13:36

652 Views

A local array cannot be directly returned from a C++ function as it may not exist in memory after the function call. A way to resolve this is to use a static array in the function. As the lifetime of the static array is the whole program, it can easily be returned from a C++ function without the above problem.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; int *retArray() {    static int arr[10];    for(int i = 0; i

How to pass a 2D array as a parameter in C?

George John
Updated on 26-Jun-2020 14:14:28

949 Views

A 2-D array can be easily passed as a parameter to a function in C. A program that demonstrates this when both the array dimensions are specified globally is given as follows.Example Live Demo#include const int R = 4; const int C = 3; void func(int a[R][C]) {    int i, j;    for (i = 0; i < R; i++)    for (j = 0; j < C; j++)    a[i][j] += 5; ; } int main() {    int a[R][C];    int i, j;    for (i = 0; i < R; i++)    for (j = 0; ... Read More

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

589 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

573 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

365 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

Advertisements