|
4. Reynolds number is calculated using formula (D*v*rho)/mu Where D = Diameter, V= velocity, rho = density mu = viscosity Write a program that will accept all values in appropriate units (Don't worry about unit conversion) If number is < 2100, display Laminar flow, If it's between 2100 and 4000 display 'Transient flow' and if more than '4000', display 'Turbulent Flow' (If, else, then...)
5. Modify the above program such that it will ask for 'Do you want to calculate again (y/n), if you say 'y', it'll again ask the parameters. If 'n', it'll exit. (Do while loop) While running the program give value mu = 0. See what happens. Does it give 'DIVIDE BY ZERO' error? Does it give 'Segmentation fault..core dump?'. How to handle this situation. Is there something built in the language itself? (Exception Handling) using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lrn2csharp4_5
{
class Program
{
static void Main( string[ ] args )
{
char answer = 'y';
Console.WriteLine( "Today we are going to calculate Reynolds Number (D*v*rho)/mu" );
while ( answer.Equals( 'y' ) )
{
try
{
calcReynolds( );
}
catch ( System.DivideByZeroException dbze )
{
Console.WriteLine( dbze.Message );
}
Console.Write( "Do you want to calculate again (y/n): " );
try
{
answer = Convert.ToChar( Console.ReadLine( ) );
}
catch ( System.FormatException )
{
answer = 'n';
}
}
}
static void calcReynolds( )
{
Console.Write( "Enter Diameter: " );
double d = Convert.ToDouble( Console.ReadLine( ) );
Console.Write( "Enter Velocity: " );
double v = Convert.ToDouble( Console.ReadLine( ) );
Console.Write( "Enter Density: " );
double rho = Convert.ToDouble( Console.ReadLine( ) );
Console.Write( "Enter Viscosity: " );
double mu = Convert.ToDouble( Console.ReadLine( ) );
// C# actually handles divide by zero well and wont throw an exception automatically, so do it manually
if ( mu.Equals( 0 ) )
throw new System.DivideByZeroException( "Cannot divide by zero. Viscosity was zero. You erred." );
double reynolds = ( d * v * rho ) / mu;
if ( reynolds < 2100 )
Console.WriteLine( "Laminar flow (" + reynolds + ")" );
else if ( reynolds >= 2100 && reynolds <= 4000 )
Console.WriteLine( "Transient flow (" + reynolds + ")" );
else
Console.WriteLine( "Turbulent flow (" + reynolds + ")" );
}
}
}
posted by dharh 3:01 AM Nov 29th, 2009 via idt
3. Accepting series of numbers, strings from keyboard and sorting them ascending, descending order.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lrn2csharp3
{
class Program
{
static void Main( string[ ] args )
{
Console.Write( "Please enter the number of items you wish to enter: " );
int num_item = Convert.ToInt32( Console.ReadLine( ) );
int[ ] nums = new int[ num_item ];
for ( int i = 0; i < num_item; i++ )
{
Console.Write( "Item " + i + ": " );
nums[ i ] = Convert.ToInt32( Console.ReadLine( ) );
}
qsort( nums );
foreach ( int num in nums ) Console.Write( num.ToString( ) + " " );
Console.WriteLine( "" );
nums = reverse( nums );
foreach ( int num in nums ) Console.Write( num.ToString( ) + " " );
Console.WriteLine( "" );
}
static void swap<T>( ref T lhs, ref T rhs )
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
static void qsort( int[ ] nums )
{
qs( nums, 0, nums.Length - 1 );
}
static void qs( int[ ] items, int left, int right )
{
int i, j;
int x;
i = left;
j = right;
x = items[ ( left + right ) / 2 ];
do
{
while ( ( items[ i ] < x ) && ( i < right ) ) i++;
while ( ( x < items[ j ] ) && ( j > left ) ) j--;
if ( i <= j )
{
swap<int>( ref items[ i ], ref items[ j ] );
i++;
j--;
}
} while ( i <= j );
if ( left < j ) qs( items, left, j );
if ( i < right ) qs( items, i, right );
}
static int[ ] reverse( int[ ] items )
{
int[ ] reversed = new int[ items.Length ];
int j = 0;
for ( int i = items.Length - 1; i >= 0; i-- )
{
reversed[ j ] = items[ i ];
j++;
}
return reversed;
}
}
}
posted by dharh 1:40 AM Nov 29th, 2009 via idt
2. Fibonacci series, swapping two variables, finding maximum/minimum among a list of numbers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lrn2csharp2
{
class Program
{
static void Main( string[ ] args )
{
int fibs = 21;
int[ ] fib_series = new int[ fibs ];
Console.WriteLine( "Outputing Fib 0 through " + fibs );
for ( int i = 0; i < fib_series.Length; i++ )
{
fib_series[ i ] = fibonacci( i );
}
printArray( fib_series );
Console.WriteLine( "" );
Console.WriteLine( "Max in Fib Series: " + max( fib_series ) );
Console.WriteLine( "Min in Fib Series: " + min( fib_series ) );
Console.WriteLine( "Swapping front with end: " );
swap<int>( ref fib_series[ 0 ], ref fib_series[ fib_series.Length - 1 ] );
printArray( fib_series );
Console.WriteLine( "" );
}
static void swap<T>( ref T lhs, ref T rhs )
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
public static int fibonacci( int n )
{
if ( n <= 1 )
{
return n;
}
else
{
return fibonacci( n - 1 ) + fibonacci( n - 2 );
}
}
public static int max( int[ ] n )
{
int max = n[ 0 ];
for ( int _i = 0; _i < n.Length; _i++ )
{
if ( n[ _i ] > max ) max = n[ _i ];
}
return max;
}
public static int min( int[ ] n )
{
int min = n[ 0 ];
for ( int _i = 0; _i < n.Length; _i++ )
{
if ( n[ _i ] < min ) min = n[ _i ];
}
return min;
}
public static void printArray( int[ ] n )
{
for ( int _i = 0; _i < n.Length; _i++ )
{
Console.Write( n[ _i ] + " " );
}
}
}
}
posted by dharh 12:47 AM Nov 29th, 2009 via idt
It's been a while since I've made an actual post, but I felt the urge. I do alot of programming (many different languages), so after I read this interview I thought i'd share it.
posted by dharh 1:05 PM Aug 7th, 2009 via idt
Looks like the twitter integration has worked pretty well.
posted by dharh 3:46 PM May 27th, 2009 via idt
Yes I'm still around. I got lazy and busy at the same time, so no updates since the last one till now. I was bored and have nothing to do till christmas stuff starts tomorrow so I started messing around on the web. Read an article "Twitter, Flickr, Facebook Make Blogs Look So 2004" aka "The Blog is Dead". And so it is! Though not for the reasons the article explains, mine is dead right now... well... I already mentioned that. Anyway, I made a twitter account and hopefully January I will force myself to add my twitter feed to my feed eater here at IDT.
Don't hold your breath though. I wouldn't want any blood on my hands. posted by dharh 6:32 PM Dec 24th, 2008 via idt
Using session variables instead of cookies?! Awesome.
posted by dharh 9:01 AM May 13th, 2008 via idt
« Previous 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Next » |
airships america attention batteries blogs books browser c++ computers copyright CSharp dharh disaster DIY DRM drugs economy energy environment FCC gaming government history HTML humor idt internet interview java javascript linkjack linux MLP moving music nature neThing neTodo networking news opensource philosophy podcasts politics poverty programming projects radio religion science sick simple software space sparce tagging technology twitter unbirthday video wiki
|

- a little order in the chaos where the mind dwells



