Server Side Programming Articles - Page 2366 of 2650

Python Implementation of automatic Tic Tac Toe game using random number

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

1K+ Views

This is very funny game. In this game no player is needed, it is an automatic game. Here we are using two Python modules numpy and random. In this game the mark on the board is put automatically instead of asking the user to put a mark on the board, and it will display the board after each turn unless a player wins. It returns -1 If the game gets draw. Example code import numpy as np import random from time import sleep # first creates an empty board def my_create_board(): return(np.array([[0, 0, 0], [0, ... Read More

When are static C++ class members initialized?

Arjun Thakur
Updated on 26-Jun-2020 13:41:03

574 Views

Static C++ class members can be defined using the static keyword. The static member in a class is shared by all the class objects as there is only one copy of the static class member in the memory, regardless of the number of objects of the class.The static class member is initialized to zero when the first object of the class is created if it is not initialized in any other way.A program that demonstrates static class members in C++ is given as follows.Example Live Demo#include using namespace std; class Example {    public :    static int a;   ... Read More

Why C/C++ variables doesn’t start with numbers

Ravi Ranjan
Updated on 26-May-2025 11:51:12

2K+ Views

In C/C++, a variable name can have alphabets, numbers, and the underscore( _ ) character. There are some keywords in the C/C++ language. Apart from them, everything is treated as an identifier. Identifiers are the names of variables, constants, functions, etc. Why Variables in C/C++ Can't Start with Numbers? In C and C++, variable names (also, known as identifiers) cannot start with a digit due to how the compiler processes code during compilation. First, we need to understand the phases of compilation or compiler. There are seven phases in a typical compiler: Lexical Analysis ... Read More

Declare variable as constant in C

Ankith Reddy
Updated on 26-Jun-2020 13:44:11

20K+ Views

Variables can be declared as constant using the const keyword or the #define preprocessor directive. Details about these are given as follows.The const keywordVariables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.A program that demonstrates the declaration of constant variables in C using const keyword is given as follows.Example Live Demo#include int main() {    const int a;    const int b = 12;    printf("The default value of variable a : %d", a);    printf("The ... Read More

Initialization of global and static variables in C

Arjun Thakur
Updated on 26-Jun-2020 13:45:22

5K+ Views

In C language both the global and static variables must be initialized with constant values. This is because the values of these variables must be known before the execution starts. An error will be generated if the constant values are not provided for global and static variables.A program that demonstrates the initialization of global and static variables is given as follows.Example Live Demo#include int a = 5; static int b = 10; int main() {    printf("The value of global variable a : %d", a);    printf("The value of global static variable b : %d", b);    return 0; }OutputThe ... Read More

Calling a member function on a NULL object pointer in C++

Ravi Ranjan
Updated on 13-Jun-2025 18:53:46

1K+ Views

You can call a class member function using a NULL object pointer in C++. This is an undefined behavior and there is no guarantee about the execution of the program. The actual results depend on the compiler used. Class Member Function A class member function is a function that is defined either inside a class or outside a class using a scope resolution operator. Example Here is an example of class member functions in C++ where the print() function is an example of the Inside class function and the print2() function is ... Read More

Initialization of a normal array with one default value in C++

Ravi Ranjan
Updated on 26-May-2025 11:39:09

18K+ Views

An array is a fixed-size collection of elements of the same data type that stores data in contiguous memory locations. Initializing an array with one default value in C++ is an easy task and can be easily implemented using various approaches that we are going to understand with code examples. Initializing Array with Zeroes in C++ To initialize all elements of an array with 0, we will simply initialize the array with 0 or just use empty curly braces. Example Here is an example that uses the above mentioned methods to initialize the array with 0: #include using ... Read More

Which is the fastest algorithm to find prime numbers using C++?

Ravi Ranjan
Updated on 02-May-2025 13:17:12

3K+ Views

The fastest algorithm to find the prime numbers is the Sieve of Eratosthenes algorithm. It is one of the most efficient ways to find the prime numbers smaller than n when n is smaller than around 10 million. In this article, we have a given number as 'num'. Our task is to find all the prime numbers less than or equal to num using Sieve of Eratosthenes algorithm in C++. Example Here is an example to find prime numbers less than 10: Input: num = 10 Output: 2 3 5 7 The explanation of the above ... Read More

Calling class method through NULL class pointer in C++

Arjun Thakur
Updated on 26-Jun-2020 13:51:40

496 Views

A class method can be can be called using a NULL class pointer.Note − This is undefined behaviour and there is not guarantee about the execution of the program. The actual results depend on the compiler used.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; class Example {    public :    void func() {       cout func();    return 0; }OutputThe output of the above program is as follows.The function is called through Null class pointer.Now, let us understand the above program.The class Example contains a member function func(). This function displays ... Read More

Can main function call itself in C++?

Ravi Ranjan
Updated on 13-May-2025 19:34:44

2K+ Views

The main() function can call itself in C++. This is an example of recursion, i.e., a function calling itself. In recursion, a function calls itself over and over again with modified arguments until it reaches its base case or the termination condition, where the recursion stops. In this article, we will go through three C++ examples where the main function calls itself. Counting Numbers up to N To count numbers up to 'n', we will recursively call the main function until it reaches 'n'. Example The following example calls the main() function recursively to print the numbers from 1 ... Read More

Advertisements