Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

C/C++ Address zero used for the null pointer

cpp

#include <stdio.h>
int main() {
   int *p= NULL;//initialize the pointer as null.
   printf("The value of pointer is %u",p);
   return 0;
}

Copy one array from another in Java

public class Demo {
	public static void main(String[] args) {
		String[] arr = new String[] { "P","Q", "R", "S", "T"};
		System.out.println("Initial array...");
		for (int i = 0; i<arr.length; i++)
			System.out.println(arr[i]);
			int len = arr.length;
			String[] arr2 = new String[len];
			System.arraycopy(arr, 0, arr2, 0, arr.length);
			System.out.println("New array...copied");
			for (int i = 0; i<arr2.length; i++)
				System.out.println(arr2[i]);
	}
}

Java Program to convert an Array to a List

import java.util.Arrays;
import java.util.List;
public class Demo {
	public static void main(String[] args) {
		Integer[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
		System.out.println("Array...");
		for (Integer res: arr) {
			System.out.println(res);
		}
		System.out.println("List...");
		List<Integer>list = Arrays.asList(arr);
		for (Integer res: list) {
			System.out.println(res);
		}
	}
}

2D vector in C++ with user defined size

cpp

#include <iostream>
#include <vector> //header file for 2D vector in C++
using namespace std;
int main() {
   vector<vector<int> > v{ { 4,5, 3, 10 }, // initializing 2D vector with values.
   { 2, 7, 11 },
   { 3, 2, 1, 12 } };
   cout<<"the 2D vector is:"<<endl;
   for (int i = 0; i < v.size(); i++) { // printing the 2D vector.
      for (int j = 0; j < v[i].size(); j++)
      cout << v[i][j] << " ";
      cout << endl;
   }
   return 0;
}

Java Program to sort integers in unsorted array

import java.util.Arrays;
public class Demo {
	public static void main(String[] args) {
		int[] arr = { 10, 14, 28, 11, 7, 16, 30, 50, 25, 18};
		System.out.println("The unsorted integer array = "+Arrays.toString(arr));
		int[] res = arr;
		Arrays.sort(res);
		System.out.println("The sorted integer array = "+Arrays.toString(res));
	}
}

Java Program to sort a subset of array elements

import java.util.Arrays;
public class Demo {
	public static void main(String[] args) {
		String[] strArr = new String[] { "r", "p", "v","y", "s", "q" };
		Arrays.sort(strArr, 2, 6);
		System.out.println("Sorted subset of array elements from index 2 to 6...");
		for (int a = 0; a<strArr.length; a++) {
			System.out.println(strArr[a]);
		}
	}
}

Program to calculate the time of sorting an array

import java.util.Arrays;
import java.util.Date;
public class Demo {
	public static void main(String[] args) {
		int[] arr = new int[1000];
		for (int i = 0; i<arr.length; i++) {
			arr[i] = (int) (i + 20);
		}
		Date past = new Date();
		Arrays.sort(arr);
		Date future = new Date();
		System.out.println("Time (milliseconds) = " + (future.getTime() - past.getTime()));
	}
}

tellp() in file handling with C++

cpp

#include <iostream>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
   fstream newfile;
   newfile.open("tpoint.txt", ios::out); //open a file to perform write operation using file object
   newfile << "Tutorials Point"; //inserting data
   cout << "The present position of the pointer in the file: "
   << newfile.tellp() << endl; //position of the pointer in the file object
   newfile.close(); //close file object.
}

Populate a 2d array with random alphabetic values in Java

import java.util.Arrays;
import java.util.Random;
public class Demo {
	public static void main(String[] args) {
		char arr[][] = new char[3][3];
		Random randNum = new Random();
		for (int i = 0; i< 3; i++) {
			for (int j = 0; j< 3; j++) {
				int x = randNum.nextInt(3);
				switch (x) {
					case 0: {
						arr[i][j] = 'p';
						break;
					}
					case 1: {
						arr[i][j] = 'q';
						break;
					}
					case 2: {
						arr[i][j] = 'r';
						break;
					}
					case 3: {
						arr[i][j] = 's';
						break;
					}
					case 4: {
						arr[i][j] = 't';
						break;
					}
					case 5: {
						arr[i][j] = 'u';
						break;
					}
					case 6: {
						arr[i][j] = 'v';
						break;
					}
					case 7: {
						arr[i][j] = 'w';
						break;
					}
				}
			}
		}
		System.out.println("Random alphabets...");
		System.out.println(Arrays.deepToString(arr));
	}
}

Java Program to output fixed number of array elements in a line

public class Demo {
	public static void main(String[] args) {
		int[] list = new int[50];
		for (int i = 0; i<list.length; i++) {
			list[i] = (int) (i + 20);
		}
		int output = 0;
		for (int i = 0; i<list.length; i++) {
			if (output == 5) {
				System.out.println();
				output = 0;
			}
			System.out.print(list[i] + ", ");
			output++;
		}
	}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.