Programming Articles - Page 2666 of 3366

Generate random numbers using C++11 random library

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

2K+ Views

In C++11, we can get the random library to generate random numbers. Here we have used random_device once to seed the random number generator object called mt. This random_device is slower than the mt19937, but we do not need to seed it. It requests for random data to the operating system.Example #include #include using namespace std; int main() {    random_device rd;    mt19937 mt(rd());    uniform_real_distribution dist(20.0, 22.0); //range is 20 to 22    for (int i=0; i> dist(mt) >> endl; }Output21.5311 21.7195 21.0961 21.9679 21.197 21.2989 20.6333 20.441 20.7124 20.2654 21.1877 20.4824 20.0575 20.9432 21.222 21.162 21.1029 20.2253 21.5669 20.3357

Java program to convert stream to typed array

Nancy Den
Updated on 15-Nov-2024 18:45:03

375 Views

In this article, we will learn how to change a Java Stream into a typed array in Java. By using the toArray() method with a constructor reference, we can ensure that the array has the right type. Problem StatementGiven a Stream of strings, write a Java program to convert it into a typed array and display the elements.Input Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming")Output Array...Bing Bang TheoryVampire DiariesGame of ThronesHomecoming Steps to convert the stream to a typed array The following are the steps to covert the stream to a typed array − ... Read More

What is a free function in C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

383 Views

The C/C++ library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc. Following is the declaration for free() function.void free(void *ptr)This function takes a pointer ptr. This is the pointer to a memory block previously allocated with malloc, calloc or realloc to be deallocated. If a null pointer is passed as argument, no action occurs.Example#include #include #include using namespace std; int main () {    char *str;    /* Initial memory allocation */    str = (char *) malloc(15);    strcpy(str, "tutorialspoint");    cout

Convert an int to ASCII character in C/C++

Nishu Kumari
Updated on 30-May-2025 18:01:36

37K+ Views

In C and C++, every character like 'A', 'b', '3', or '@' is stored as a number called its ASCII value. For example, 'A' is 65, and 'a' is 97. Given an integer like 97, we can convert it to its corresponding ASCII character which is 'a'. In this article, we will learn how to write a C and C++ program to convert an integer into its ASCII character. For example, we're given any integer from 0 to 127 (because the ASCII table contains 128 characters), and we need to convert it into its corresponding ASCII character: Input: 65 ... Read More

How to use Reference Parameters in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

254 Views

Here we will see how to pass reference of some variable in C++. Sometimes we call it as “Call by Reference”.The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in ... Read More

How to right align the text in a ComboBox in Java

Nancy Den
Updated on 30-Jul-2019 22:30:26

464 Views

To right align the text in a JComboBox, use the following:ComponentOrientation.RIGHT_TO_LEFTThe following is an example to right align the text in a ComboBox:Exampleimport java.awt.Component; import java.awt.ComponentOrientation; import javax.swing.DefaultListCellRenderer; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JList; public class SwingDemo extends JFrame {    public SwingDemo() {       JComboBox combo = new JComboBox();       combo.setRenderer(new MyListCellRenderer());       combo.addItem("One");       combo.addItem("Two");       combo.addItem("Three");       combo.addItem("Four");       combo.addItem("Five");       getContentPane().add(combo, "North");       setSize(600, 400);       setDefaultCloseOperation(EXIT_ON_CLOSE);    }    public static void main(String[] args) { ... Read More

Java program to check if none of the string in the list matches the condition

Nancy Den
Updated on 29-Sep-2024 02:47:46

863 Views

In this article, you will learn how to check if none of the strings in a list start with a specific letter using Java. This approach is useful for validating or filtering data. We will use the Stream API to evaluate the condition and return a boolean result based on whether any string starts with the given letter. Problem Statement Write a program in Java to check if none of the strings in the list matches the condition. Input pqr, stu, vwx, yza, vwxy Output No match for the starting letter as f? = true Steps to check if none of ... Read More

How to check if input is numeric in C++?

Nishu Kumari
Updated on 30-May-2025 18:07:17

6K+ Views

Numeric input means a value that contains only digits from 0 to 9, without any letters or special characters. In this article, we'll show you how to write a C++ program to check whether the input is numeric. Let's understand this with a few examples: //Example 1: Input: 12345 Output: Valid numeric input //Example 2: Input: 12a5 Output: Not a numeric input We will cover two common ways to check if the input is numeric or not in C++. Using std::getline with std::isdigit Check Using stringstream to Parse ... Read More

How to find the size of an int[] in C/C++?

Nishu Kumari
Updated on 30-May-2025 18:05:34

8K+ Views

In C and C++, arrays can store multiple values of the same data type. This problem is about finding how many elements are present in a statically declared int[] array (not pointers or dynamically allocated arrays). We'll learn how to find the size of an int[] in both C and C++. Let's understand this with an example: //Example 1 input: int numbers[] = {10, 20, 30, 40, 50}; This array has 5 elements. Output: Number of elements: 5 //Example 2: Input: int values[] = {1, 2, 3}; This array has 3 elements. Output: Number of elements: 3 ... Read More

Java Program to convert integer to String with Map

Alshifa Hasnain
Updated on 07-Jan-2025 18:55:45

722 Views

In this article, we will learn to convert integers to String with Map in Java. The Stream API has become an essential tool for processing collections of data in a more declarative and readable way. Instead of using traditional loops, streams allow developers to filter, map, and perform other operations on data with minimal code Problem Statement Given an integer, we need to convert it to its string representation where the integer is treated as a character (e.g., 5 becomes "5", 10 becomes "10"). We'll use a Map to define the integer-to-string mappings and retrieve the string equivalent for a ... Read More

Advertisements