0% found this document useful (0 votes)
9 views10 pages

Dawit Getnet

The document contains a series of C# programming assignments focused on array manipulation, including finding element frequency, reversing an array, identifying the largest element, and calculating the sum of array elements. It also explains exception handling in C#, detailing how exceptions disrupt program flow and can be managed using try-catch blocks, along with examples of custom exceptions and throwing exceptions. The content is structured as a tutorial for students in a Rapid Application Development course at Mattu University.

Uploaded by

chak.m.beyena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Dawit Getnet

The document contains a series of C# programming assignments focused on array manipulation, including finding element frequency, reversing an array, identifying the largest element, and calculating the sum of array elements. It also explains exception handling in C#, detailing how exceptions disrupt program flow and can be managed using try-catch blocks, along with examples of custom exceptions and throwing exceptions. The content is structured as a tutorial for students in a Rapid Application Development course at Mattu University.

Uploaded by

chak.m.beyena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

MATTU UNIVERSITY

College of Business and Economics


Department of Management Information System
Course name: Rapid Application Development
with C#
Course Code: MISY4052
Individual Assignment
Name: Dawit Getnet
Id No: 1615

Submitted to: Mr.tirate k


1. Write C# Program to find the frequency of each element of an
array?
using System;

public class Frequency


{
public static void Main()
{

int[] arr = new int[] { 1, 8, 8, 3, 2, 2, 2, 5, 1 };


int[] fr = new int[arr.Length];
int visited = -1;

for (int i = 0; i < arr.Length; i++)


{
int count = 1;
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] == arr[j])
{
count++;
fr[j] = visited;
}
}
if (fr[i] != visited)
fr[i] = count;
}
Console.WriteLine("---------------------");
Console.WriteLine(" Element | Frequency");
Console.WriteLine("---------------------");
for (int i = 0; i < fr.Length; i++)
{
if (fr[i] != visited)
Console.WriteLine(" " + arr[i] + " | " + fr[i]);
}
Console.WriteLine("---------------------");
Console.ReadLine();
}
}

Out put

1
2. Write C# Program to print the elements of an array in reverse
order?
using System;

public class ReverseArray

{
public static void Main()

{
int[] arr = new int[] { 5, 6, 7, 8, 9, 10 };

Console.WriteLine("Original array: ");


{
Console.Write(arr[i] + " ");

Console.WriteLine();

Console.WriteLine("Array in reverse order: ");

for (int i = arr.Length - 1; i >= 0; i--)


{

Console.Write(arr[i] + " ");

}
Console.ReadLine();
}
}

Out put

2
3. Write C# Program to print the largest element present in an array?
using System;

public class LargestElement

{
public static void Main()
{

int[] arr = new int[] { 23, 76, 19, 86, 65 };

int max = arr[0];

for (int i = 0; i < arr.Length; i++)


{
if (arr[i] > max)

max = arr[i];
}
Console.WriteLine("Largest element present in given array: " + max);

Console.ReadLine();
}
}

Out put

3
4. Write C# Program to print the sum of all the elements of an array?
using System;

public class SumOfArray

{
public static void Main()

{
int[] arr = new int[] { 10, 5, 25, 5, 15 };

int sum = 0;
for (int i = 0; i < arr.Length; i++)

{
sum = sum + arr[i];
}
Console.WriteLine("Sum of all the elements of an array: " + sum);

Console.ReadLine();
}

Out put

4
Explain

Explain Exception in C#

Exceptions in C# are used to handle runtime errors in a program. When an exception occurs, the
normal flow of the program is disrupted, and the control is transferred to the nearest exception
handler. This mechanism helps in maintaining the stability and reliability of the program by
preventing it from crashing due to unexpected errors. In C#, exceptions are represented by
objects that are derived from the System. Developers can create custom exception classes by
deriving from the Exception class to handle specific types of errors in their applications. This
allows for better organization and management of different types of exceptions that may occur
during the execution of the program. Handling exceptions in C# is done using try-catch blocks.
The code that might throw an exception is placed inside the try block, and the catch block is used
to handle the exception if it occurs. This way, developers can gracefully recover from errors and
take appropriate actions, such as logging the error, displaying a message to the user, or
performing cleanup operations before the program terminates.

An exception in C# represents an error condition that arises during the execution of a program.
Exceptions can occur due to various reasons, such as incorrect input, resource unavailability, or
logical errors in the code. The .NET runtime throwing exceptions is a way of informing the
programmer about these errors.

When an exception is thrown, the default behavior is to terminate the program, and the content of
the stack trace is displayed to the console. However, a programmer can catch exceptions and
handle them according to their requirements.

In C#, there are built-in exception classes that represent specific error conditions. Some common
exceptions are Null Reference Exception, Index out of Range Exception, and Divide By Zero
Exception. Custom exceptions can also be created by inheriting from the Exception class.

When an exception is thrown, the program starts looking for a suitable catch block that can
handle the exception. The first catch block that can handle the type of exception thrown is used
to execute the error handling code. If no suitable catch block is found, the program will
terminate. This mechanism allows for the differentiation of various error conditions and provides
a way to handle them appropriately.

Explain Exceptions Handling with example

Exception Handling in C# is a process to handle runtime errors. We perform


exception handling so that normal flow of the application can be maintained

5
even after runtime errors. In C#, exception is an event or object which is
thrown at runtime. All exceptions the derived from System. It is a runtime
error which can be handled. If we don't handle the exception, it prints
exception message and terminates the program.

Exception handling in C# is a way to deal with runtime errors that occur


during the execution of a program. When an exception is thrown, the flow of
the program is disrupted and the normal execution is stopped.

To handle exceptions, developers can use try-catch blocks. The try block
contains the code that might throw an exception, while the catch block is
used to handle the exception if it occurs.

Exceptions Handling with example


using System;
namespace ErrorHandlingApplication
{
class DivNumbers
{
int result;
DivNumbers()
{
result = 0;
}
public void division(int num1, int num2)
{
try
{
result = num1 / num2;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception caught: {0}", e);
}
finally
{
Console.WriteLine("Result: {0}", result);
}
}
static void Main(string[] args)
{
DivNumbers d = new DivNumbers();
d.division(25, 0);
Console.ReadKey();
}
}
}

6
Define custom exceptions with example
Custom exceptions in programming, often used in object-oriented languages like Java or
Python, are exceptions that you define yourself to handle specific error conditions in your code.
By creating custom exceptions, you can provide more meaningful error messages and improve
the readability and maintainability of your code. They allow you to differentiate between
different types of errors and handle them appropriately within your program.

Custom exceptions in C# are user-defined exceptions that allow developers to create their own
exception types to handle specific error conditions in their code. By creating custom exceptions,
developers can provide more detailed information about the error and handle it in a more
specific way. This can help improve the overall error handling and debugging process in C#
applications. Let me know if you need more information on this topic!

A Custom Exception in C# is an exception defined by the developer to handle specific error


conditions that are unique to the application. They are created by deriving a new class from the
base Exception class or one of its derived classes.

Custom exceptions provide a way to signal unique error conditions and react to them in a
structured manner.

C# includes the built-in exception types such as Null Reference Exception, Memory Overflow
Exception, etc. However, you often like to raise an exception when the business rule of your
application gets violated. So, for this, you can create a custom exception class by deriving the
Application Exception class.

Custom exceptions with example

using System;
public class ExExample
{
public static void Main(string[] args)
{
int a = 10;
int b = 0;
int x = a/b;
Console.WriteLine("Rest of the code");
Console.ReadLine();
}

7
}

Define Throwing exceptions with example


In C#, throwing exceptions is a mechanism used to indicate that an abnormal condition has
occurred during the execution of a program. When you throw an exception, you're essentially
saying, "Something went wrong, and I can't continue as expected." This can be due to various
reasons, such as invalid input, a missing file, or a network error.

To throw an exception in C#, you use the throw keyword followed by an instance of an
exception class. For example:

Throw new Exception ("Something went wrong");

This will create and throw a new instance of the Exception class with the specified error
message. You can also throw specific types of exceptions, such as Argument Exception or File
Not Found Exception, depending on the nature of the error.

Throwing exceptions is a fundamental concept in programming where errors or exceptional


conditions are reported during the execution of a program. When an exceptional situation
occurs, an exception is "thrown" by the program to indicate that something unexpected has
happened. This allows the program to handle the error gracefully by either trying to recover
from the exception or terminating the program in a controlled manner. By using exceptions,
developers can improve the reliability and robustness of their code by separating the error-
handling logic from the regular program flow. Additionally, exceptions provide a mechanism for
propagating errors up the call stack, allowing higher-level code to handle exceptions thrown by
lower-level code.

8
Throwing exceptions with example
using System;
public class DivZero
{
static public void Main ()
{
// Set an integer equal to 0
int IntVal1 = 0;
// and another not equal to zero
int IntVal2 = 57;
try
{
Console.WriteLine ("{0} / {1} = {2}", IntVal2, IntVal1, IntResult (IntVal2,
IntVal1) / IntResult (IntVal2, IntVal1));
}
catch (DivideByZeroException e)
{
Console.WriteLine (e.Message);
}
// Set a double equal to 0
double dVal1 = 0.0;
double dVal2 = 57.3;
try
{
Console.WriteLine ("{0} / {1} = {2}", dVal2, dVal1, DoubleResult (dVal2, dVal1));
}
catch (DivideByZeroException e)
{
Console.WriteLine (e.Message);
}
}
static public int IntResult (int num, int denom)
{
return (num / denom);
}
static public double DoubleResult (double num, double denom)
{
return (num / denom);
}
}
}

You might also like