Server Side Programming Articles - Page 1704 of 2650

Working with Array and Vectors using STL in C++

sudhir sharma
Updated on 17-Jul-2020 10:56:51

396 Views

Array and vectors are very important data structures in competitive programming for solving problems. And the STL (Standard Template Library) in c++ programming provide some functions to perform operations of arrays and vectors.Let’s see some of these functions in action, Finding sum, Min and Max of the array/vector − In STL there are function that helps you to find the sum, max, and min of the array/vector. Functions with there function, Finding sumaccumulate(startIndex, endIndex, initialSum)Greatest element of the array/vecto*max_element(startIndex, endIndex)Smallest element of array/vector*min_element(startIndex, endIndex)Program to perform operations on array −Example Live Demo#include using namespace std; int main(){    int array[] ... Read More

Write a bash script to print a particular line from a file in C

sudhir sharma
Updated on 17-Jul-2020 10:50:06

363 Views

In this program, we are given a file name text.txt. Our task is to print a particular line from the file.For this there are multiple methods in bash script, they are awk, sed, head.Syntax$> awk ‘{if(NR==LINE_NUMBER) print $0}’ filename $> sed -n LINE_NUMBERp filename $head -n LineNumber filename | tail - n + LINE_NUMBERCode to print a specific line in bash programming from file text.txt.Using awk$> awk ‘{if(NR==5) print $0}’ text.txtUsing sed$>sed -n 5p text.txtUsing head$head -n 5 filename | tail - n + 5

Write a C program that displays contents of a given file like 'more' utility in Linux

sudhir sharma
Updated on 17-Jul-2020 10:48:40

269 Views

Here, we will write a C program that will display the contents of a file page by page as displayed in Linux using the more command.This program will show a specific number of lines on the screen first and then wait for the user to hit enter to move to the next page i.e. next set of n lines.For displaying the content of the file like this we will open the file and print its contents. And maintain a counter for new lines in the file. When this counter reaches n, we will read a key pressed by the user ... Read More

Write a program to print ‘Tutorials Point’ without using a semicolon in C

sudhir sharma
Updated on 15-Jul-2020 06:40:59

243 Views

In this problem, we have to write a program that will print ‘Tutorials Point ’ without using a semicolon.We all know that to end a statement in c semicolon is necessary. And print statement will be executed when a semicolon is added at the end.So, for printing ‘Tutorials point’ without a semicolon, we will have to first learn about the printf method in c. in actually returns an integer which is the count of total number of characters that are required to be printed.Syntaxint printf(constant char *format, ...)The method can accept n number of arguments. The first will be the ... Read More

Write a C program to print ‘ABCD’ repeatedly without using loop, recursion and any control structure

sudhir sharma
Updated on 15-Jul-2020 06:31:02

251 Views

In this problem, we have to write a program in c that will print a string ‘ABCD’ repeatedly without using loop, recursion and any control structure.So, we will have to call or run the same block of code infinite time but without using loop, recursion or control structure which are the most common methods to perform the task. For this, we will run the same program multiple times instead of looping. This will perform our task within the given constraints. The system() method can be employed inside the code that will call the program infinite times.We will pass the file ... Read More

Write a function that counts the number of times a given int occurs in a Linked List in C++

sudhir sharma
Updated on 15-Jul-2020 06:26:14

468 Views

In this problem, we are given a linked list. Our task is to create a function that will be able to count the number of times a given number occurs in the linked list.Let’s take an example to understand the problem, InputLinked list = 10-> 50 -> 10 -> 20 -> 100 -> 10, int = 10Output3Explaination − the number 10 occurs 3 times in the linked list.The solution to this problem is simple, just traverse the linked list and increment a counter the current node value is equal to the given number.The looping over the nodes of the linked ... Read More

Write a function that generates one of 3 numbers according to given probabilities in C++

sudhir sharma
Updated on 15-Jul-2020 06:23:43

370 Views

In this problem, we have to create a function that will generate three numbers based on the given probability.For this, we will use the built-in random number generator function which is rand(a, b) which generates random numbers within the range [a, b] with equal probability.Our task is to return only three numbers A, B, C which have the probability of occurrence as P(A), P(B), P(C) respectively and according to definition of probability P(A) + P(B) + P(C) = 1.To create our function using rand(a, b). we will use its feature that the probability of occurrence of any number from a ... Read More

Write a function to delete a Linked List in C++ Programming

sudhir sharma
Updated on 15-Jul-2020 06:21:31

695 Views

Here, we will create a function that will delete all elements of a linked list one by one.In c/c++, there is no specific function to perform this task but in java, the automatic garbage collection is done to ease deleting linked list.Now, let’s see the implementation of this program, Example Live Demo#include using namespace std; class Node{    public:    int data;    Node* next; }; void deleteLinkedList(Node** head_ref){    Node* current = *head_ref;    Node* next;    while (current != NULL){       coutnext = (*head_ref);    (*head_ref) = new_node; } int main(){    Node* head = NULL; ... Read More

Preconditions - Java

AmitDiwan
Updated on 14-Jul-2020 07:08:28

277 Views

Precondition to check if the list passed as parameter is empty or not. Let us see an example −Examplepublic void my_fun(List myList){    if (myList == null){       throw new IllegalArgumentException("List is null");    }    if (myList.isEmpty()){       throw new IllegalArgumentException("List is empty");    }    my_fun(myList); }A void function named ‘my_fun’ is defined that takes a list of objects as its parameters. If the list is null, it prints the relevant message. If the list has no elements in it, a specific message is displayed. The function is called by passing the list as ... Read More

Java program to print matrix in Z form

AmitDiwan
Updated on 29-Oct-2024 18:51:04

731 Views

In this article, we will learn to print a matrix in a "Z" pattern in Java. It starts from the top-left corner, moves horizontally across the top row, then diagonally down through the center, and finally across the bottom row. This pattern creates a "Z" shape with selected elements from the matrix. Problem Statement Write a program in Java to print a matrix in Z form. Input my_arr[][] = { { 34, 67, 89, 0}, { 0, 1, 0, 1 }, { 56, 99, 102, 21 }, {78, 61, 40, 99}} Output The matrix is34 67 89 0 0 99 ... Read More

Advertisements