Server Side Programming Articles - Page 2329 of 2650

How can I profile C++ code running on Linux?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

485 Views

In Linux platform there are many great profiling tools for profiling C++ programs. Valgrind is one of them. It is widely used. It is a programming tool for memory debugging, memory leak detection, and profiling. We can use Valgrind by passing the binary to it and setting the tool to callgrind. First generate the binary by compiling the program$ g++ -o abc.cpp abcNow use valgrind to profile it$ valgrind --tool=callgrind ./abcThis will generate a file called callgrind.out.x. You can read this file using a tool called kcachegrind.If you're using gcc, you can use the inbuilt profiling tool, gprof. You can ... Read More

Why is iostream::eof inside a loop condition considered wrong?

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

209 Views

The iostream::eof in a loop is considered as wrong because we haven’t reached the EOF. So it does not mean that the next read will succeed.When we want to read a file using file streams in C++. And when we use a loop to write in a file, if we check the end of file using stream.eof(), we are actually checking whether the file has reached end or not.Example Code#include #include using namespace std; int main() { ifstream myFile("myfile.txt"); string x; while(!myFile.eof()) { myFile >> ... Read More

C++ Program to Implement Sieve of eratosthenes to Generate Prime Numbers Between Given Range

Ravi Ranjan
Updated on 05-May-2025 18:42:30

3K+ Views

The Sieve of Eratosthenes algorithm is one of the most efficient ways to find prime numbers smaller than n when n is smaller than around 10 million. It follows a simple process of marking the multiples of already prime numbers as false i.e. non-prime numbers. 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 the 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 ... Read More

C Program to display hostname and IP address

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

3K+ Views

In this section we will see how to see the Host name and IP address of the local system in an easier way. We will write a C program to find the host name and IP.Some of the following functions are used. These functions have different task. Let us see the functions and their tasks.FunctionDescriptiongethostname()It finds the standard host name for the local computer.gethostbyname()It finds the host information corresponding to a host name from host databaseiten_ntoa()It converts an IPv4 Internet network address into an ASCII string into dotted decimal format.Example Code#include #include #include #include #include ... Read More

C++ Program to Implement Wheel Sieve to Generate Prime Numbers Between Given Range

Ravi Ranjan
Updated on 05-May-2025 18:42:57

472 Views

The wheel Sieve method is used to find the prime numbers within a given range. Wheel factorization is a graphical method for manually performing a preliminary to the Sieve of Eratosthenes that separates prime numbers from composites. In this method, prime numbers in the innermost circle have their multiples in similar positions as themselves in the other circles, forming spokes of primes and their multiples. Multiple of these prime numbers in the innermost circle form spokes of composite numbers in the outer circles. In this article, we have set the limit to 100. Our task is to implement the ... Read More

C++ Program to Emulate N Dice Roller

Ravi Ranjan
Updated on 15-Apr-2025 15:27:13

1K+ Views

Emulating an n dice roller refers to rolling n number of dice simultaneously. In each roll, all the dice will return a different value from 1 to 6. In this article, we are having 'n' number of dices, our task is to emulate rolling n dices simultaneously. The approaches are mentioned below: Using Loop with rand() Function Using Recursion Using Loop with rand() Function In this approach, we have used the rand() function to generate a random value of dice in each roll. The srand() function with time() is ... Read More

C Program to print hollow pyramid and diamond pattern

George John
Updated on 30-Jul-2019 22:30:25

3K+ Views

Here we will see how to generate hollow pyramid and diamond patterns using C. We can generate solid Pyramid patterns very easily. To make it hollow, we have to add some few tricks.Hollow PyramidFor the pyramid at the first line it will print one star, and at the last line it will print n number of stars. For other lines it will print exactly two stars at the start and end of the line, and there will be some blank spaces between these two starts.Example Code#include int main() {    int n, i, j;    printf("Enter number of lines: ... Read More

C program to print digital clock with current time

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

3K+ Views

In this section we will see how to make a digital clock using C. To work with time we can use the time.h header file. This header file has some function signatures that are used to handle date and time related issues.The four important components of time.h is like belowsize_t This size_t is basically the unsigned integral type. This is the result of sizeof().clock_t This is used to store the processor timetime_t This is used to store calendar timestruct tm This is a structure. It helps to hold the entire date and time.Example Code#include #include int main() { ... Read More

C++ Program to Generate Random Numbers Using Multiply with Carry Method

Ravi Ranjan
Updated on 05-May-2025 12:28:21

390 Views

The multiply-with-carry method is a variant of the add-with-carry generator introduced by Marsaglia and Zaman (1991). The main advantages of this method are that it invokes simple computer integer arithmetic and leads to a very fast generation of sequences of random numbers with immense periods, ranging from around 260 to 22000000. In this article, our task is to generate random numbers using the multiply-with-carry method. Here is the formula of multiply-with-carry method: Multiply With Carry (MWC) Formula The multiply-with-carry formula is as follows: Xn = (a * Xn-1 + Cn-1) mod 232 Cn = (a * Xn-1 + Cn-1) ... Read More

C program to write an image in PGM format

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

2K+ Views

The PGM is the Portable Gray Map. If we want to store a 2d array in C as images in PNG, JPEG, or any other image format, we have to do lots of work to encode the data in some specified format before writing into a file.The Netpbm format gives an easy and portable solution. The Netpbm is an open source package of graphics program and it is used basically in linux or Unix platform. It also works under Microsoft Windows systems.Each file starts with a two-byte magic number. This magic number is used to identify the type of the ... Read More

Advertisements