Posts

Showing posts from April, 2021

C - Linked List Implementation

C - Linked List Implementation CODE #include <stdio.h> #include <stdlib.h> struct Node { int data ; struct Node * next ; }; int main () { struct Node * head = NULL ; struct Node * currentNode ; currentNode = head ; for ( int i = 1 ; i <= 10 ; i ++) { struct Node * temp = ( struct Node *) malloc ( sizeof ( struct Node )); temp -> data = i ; temp -> next = NULL ; if ( currentNode == NULL ) currentNode = head = temp ; else { currentNode -> next = temp ; currentNode = temp ; } } //Print all the nodes in the list currentNode = head ; while ( currentNode ) { printf ( "%d " , currentNode -> data ); currentNode = currentNode -> next ; } return 0 ; //OUTPUT //1 2 3 4 5 6 7 8 9 10 } ...

C++ - Read and Write to a File

C++ - Read and Write to a File CODE #include <iostream> #include <fstream> using namespace std ; int main () { string fileName = "data.txt" ; //Write to a file ofstream f1 ( fileName ); f1 << "This is a test line. 1\n" ; f1 << "This is a test line. 2\n" ; f1 << "This is a test line. 3\n" ; f1 << "This is a test line. 4" ; f1 . close (); //Read the file line by line string line ; ifstream f2 ( fileName ); while ( getline ( f2 , line )) { cout << line << endl ; } f2 . close (); //OUTPUT //This is a test line. 1 //This is a test line. 2 //This is a test line. 3 //This is a test line. 4 return 0 ; }

C# - Read, Write, Modify data in a file

C# - Read, Write, Modify data in a file CODE using System ; using System . IO ; using System . Collections . Generic ; class Program { private const string fileName = "data.txt" ; public static void Main ( string [] args ) { Console . WriteLine ( Environment . CurrentDirectory ); Dictionary < string , double > items = ReadData (); AddData ( "Lunch Bag" , 100.2 , items ); DisplayData ( items ); ModifyData ( "Lunch Bag" , 200.2 , items ); DisplayData ( items ); //INPUT File //Pencil,2 //Book,25.5 //Box,30 //Lunch Bag,200.2 } private static Dictionary < string , double > ReadData () { Dictionary < string , double > items = new Dictionary < string , double >(); if (! File . Exists ( fileName )) throw new Exception ( "The inpu...

C# - Shift Operators

C# - Shift Operators CODE using System ; class Program { public static void Main ( string [] args ) { int number = 10 ; int leftShiftedNumber = 10 << 1 ; int rightShiftedNumber = 10 >> 1 ; //The left shifted number will be the double of the number //The right shifted number will be the half of the number Console . WriteLine ( "Number = {0}" , number ); Console . WriteLine ( "Left Shifted Number = {0}" , leftShiftedNumber ); Console . WriteLine ( "Right Shifted Number = {0}" , rightShiftedNumber ); } }

C# - Sorting - Insertion Sort

C# - Sorting - Insertion Sort CODE using System ; class Program { public static void Main ( string [] args ) { int [] numbers = { 12 , 11 , 13 , 5 , 6 }; Console . Write ( "Numbers: " ); PrintArray ( numbers ); Console . Write ( "\nSorted Numbers: " ); PrintArray ( Sort ( numbers )); } private static int [] Sort ( int [] numbers ) { for ( int i = 1 ; i < numbers . Length ; i ++) { int key = numbers [ i ]; int j = i - 1 ; //Move elements of numbers[0..i-1], that are greater than key to one position ahead of their current position while ( j >= 0 && numbers [ j ] > key ) { numbers [ j + 1 ] = numbers [ j ]; j = j - 1 ; } numbers [ j + 1 ] = key ; } retur...

C# - Find the factorial of a given number

C# - Find the factorial of a given number CODE using System ; class Program { public static void Main ( string [] args ) { int number = 10 ; int factorial = 1 ; for ( int i = 1 ; i <= number ; i ++) { factorial = factorial * i ; } Console . WriteLine ( "The factorial of {0} is {1}." , number , factorial ); //OUTPUT //The factorial of 10 is 3628800. } }

C# - Matrix - Find the determinant of a matrix

C# - Matrix - Find the determinant of a matrix CODE using System ; class Program { public static void Main ( string [] args ) { int [,] matrix = { { 2 , 6 , 8 }, { 10 , 12 , 14 }, { 16 , 18 , 20 } }; int rows = matrix . GetLength ( 0 ); int cols = matrix . GetLength ( 1 ); if (( rows == 2 || rows == 3 ) && rows == cols ) { int determinant ; if ( rows == 2 ) determinant = GetDeterminantOf2by2Matrix ( matrix ); else { int determinantInnerMatrix1 = GetDeterminantOf2by2Matrix ( new int [,] { { matrix [ 1 , 1 ], matrix [ 1 , 2 ] }, { matrix [ 2 , 1 ], matrix [ 2 , 2 ] } }); int determinantInnerMatrix2 = GetDeterminantOf2by2Matrix ( new int [,] { { matrix [ 1 , 0 ], matrix [ 1 , 2 ] }, { matrix [ 2 , 0 ], matrix [ 2 , 2 ] } }); ...

C# - Print different Star Patterns

C# - Print different Star Patterns CODE using System ; class Program { public static void Main ( string [] args ) { int numberOfLines = 5 ; //Prints the below pattern //* //** //*** //**** //***** for ( int i = 1 ; i <= numberOfLines ; i ++) { for ( int j = 1 ; j <= i ; j ++) { Console . Write ( "*" ); } Console . WriteLine ( "" ); } Console . WriteLine ( "" ); //Prints the below pattern //***** //**** //*** //** //* for ( int i = 1 ; i <= numberOfLines ; i ++) { for ( int j = numberOfLines ; j >= i ; j --) { Console . Write ( "*" ); } Console . WriteLine ( "" ); ...

C# - Matrix - Find product of 2 matrix

C# - Matrix - Find product of 2 matrix CODE using System ; class Program { public static void Main ( string [] args ) { //The number of columns of Matrix a should be same as the number of rows of Matrix b int [,] matrixA = { { 1 , 4 , 2 }, { 2 , 5 , 1 } }; int [,] matrixB = { { 3 , 4 , 2 }, { 3 , 5 , 7 }, { 1 , 2 , 1 } }; Console . WriteLine ( "Matrix A" ); printMatrix ( matrixA ); Console . WriteLine ( "\nMatrix B" ); printMatrix ( matrixB ); if ( matrixA . GetLength ( 1 ) == matrixB . GetLength ( 0 )) { int [,] productMatrix = new int [ matrixA . GetLength ( 0 ), matrixB . GetLength ( 1 )]; for ( int i = 0 ; i < matrixA . GetLength ( 0 ); i ++) { for ( int j = 0 ; j < matrixB . GetLength ( 1 ); j ++) { ...

C# - Find 1st (first) derivative of a polynomial equation

C# - Find 1st (first) derivative of a polynomial equation CODE using System ; class Program { public static void Main ( string [] args ) { //Polynomial Equation: 3x^3 + 4x^2 +0x + 1 //If one of the x term is 0, provide it as 0 in the array double [] polynomialCoefficients = new double [] { 3 , 4 , 0 , 1 }; Console . Write ( "Polynomial Equation: " ); displayPolynomialEquation ( polynomialCoefficients ); double [] firstDerivative = new double [ polynomialCoefficients . Length - 1 ]; for ( int i = 0 , power = polynomialCoefficients . Length - 1 ; i < firstDerivative . Length ; i ++, power --) { firstDerivative [ i ] = power * polynomialCoefficients [ i ]; } Console . Write ( "First Derivative: " ); displayPolynomialEquation ( firstDerivative ); //OUTPUT //Polynomial Equatio...