Visual Programming Assienment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Department Of Computer Science

Assignment Cover Letter

Student Name

Roll No

Course Name Visual Programming

Assignment # 02

Session 2016-2020

Date

Instructor
Q1: Write a method that returns the last digit of given integer as an
English word. Examples: 512 is "two", 1024 is "four", 12309 is "nine".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace assignment
{
class LastDigitOfANumberAsWord
{
static string[ ] digitsAsWord = new string[] { "Zero", "One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine" };

static int FindingLastDigitOfANumber(int number)


{
int lastDigit;
lastDigit = number % 10;
return lastDigit;
}

static void PrintingLastDigitAsWord(int lastDigit)


{
Console.WriteLine(digitsAsWord[lastDigit]);
}

static void Main()


{
Console.Write("Enter a number: ");
int sayNumber = int.Parse(Console.ReadLine());
if (sayNumber < 0)
{
sayNumber *= -1;
}

int lastDigit;
lastDigit = FindingLastDigitOfANumber(sayNumber);
PrintingLastDigitAsWord(lastDigit);
Console.ReadKey();
}
}
}
Q2: Write a method that counts how many times given number appears
in given array. Write a test program to check if the method is working
correctly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace assignment
{
class NumberAppearsInArray
{

static void Main(string[] args)


{

Console.Write("Length of array: ");

int length = int.Parse(Console.ReadLine());

FillArray(length);

static void FillArray(int length)


{

int[] arr = new int[length];

for (int i = 0; i < length; i++)


{

Console.Write("Fill array[{0}]: ", i + 1);

arr[i] = int.Parse(Console.ReadLine());
}

Console.Write("Number to find: ");

int number = int.Parse(Console.ReadLine());

NumberAppears(arr, number);

public static void NumberAppears(int[] arr, int number)


{

int count = 0;

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


{

if (arr[i] == number)
{

count++;

Console.WriteLine("{0} appears in array {1} times.", number, count);


Console.ReadKey();

}
Q3: Write a method that returns the index of the first element in array
that is bigger than its neighbors, or -1, if there’s no such element. Use
the method from the previous exercise.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace assignment
{
class Program
{

static void CompareNumber(int pos, int[] arr)


{

if (pos == 0)
{

if (arr[0] < arr[1]) Console.WriteLine("{0} is smaller than it's right.", arr[0]);

else if (arr[0] > arr[1]) Console.WriteLine("{0} is bigger than it's right.", arr[0]);

else Console.WriteLine("{0} is equal to it's right.", arr[0]);

else if (pos == arr.Length - 1)


{

if (arr[arr.Length - 1] < arr[arr.Length - 2]) Console.WriteLine("{0} is smaller than it's


left.", arr[pos]);

else if (arr[arr.Length - 1] > arr[arr.Length - 2]) Console.WriteLine("{0} is bigger than


it's left.", arr[pos]);

else Console.WriteLine("{0} is equal to it's left.", arr[pos]);

else
{

if (arr[pos] < arr[pos - 1])


{

if (arr[pos] < arr[pos + 1]) Console.WriteLine("{0} is smaller than it's neighbours.",


arr[pos]);
else if (arr[pos] == arr[pos + 1]) Console.WriteLine("{0} is smaller than it's left and
equal to it's right.", arr[pos]);

else Console.WriteLine("{0} is smaller than it's left and bigger than it's right.",
arr[pos]);

else if (arr[pos] == arr[pos - 1])


{

if (arr[pos] < arr[pos + 1]) Console.WriteLine("{0} is euqal to it's left and smaller
than it's right.", arr[pos]);

else if (arr[pos] == arr[pos + 1]) Console.WriteLine("{0} is equal to it's neighbours.",


arr[pos]);

else Console.WriteLine("{0} is equal to it's left and bigger than it's right.", arr[pos]);

else
{

if (arr[pos] < arr[pos + 1]) Console.WriteLine("{0} is bigger than it's left and smaller
than it's right.", arr[pos]);

else if (arr[pos] == arr[pos + 1]) Console.WriteLine("{0} is bigger than it's left and
equal to it's right.", arr[pos]);

else Console.WriteLine("{0} is bigger than it's neighbours.", arr[pos]);

static void Main(string[] args)


{

Console.Write("Enter array length: ");

int length = Int32.Parse(Console.ReadLine());

int[] arr = new int[length];


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

Console.Write("Enter {0} element: ", i);

arr[i] = Int32.Parse(Console.ReadLine());

Console.Write("Enter position in array: ");

int pos = Int32.Parse(Console.ReadLine());

CompareNumber(pos, arr);
Console.ReadLine();

}
}

Q4: Difference between Class and Structure in C#?


A class is a user-defined blueprint or prototype from which objects are created.
Basically, a class combines the fields and methods(member function which defines
actions) into a single unit.
A structure is a collection of variables of different data types under a single unit. It is
almost similar to a class because both are user-defined data types and both hold a
bunch of different data types.

Class Structure
Classes are of reference types. Structs are of value types.

All the reference types are allocated on heap All the value types are allocated on stack
memory. memory.

Allocation of large reference type is cheaper Allocation and de-allocation is cheaper in value
than allocation of large value type. type as compare to reference type.

Class has limitless features. Struct has limited features.

Class is generally used in large programs. Struct are used in small programs.

Classes can contain constructor or destructor. Structure does not contain constructor or
destructor.

Classes used new keyword for creating Struct can create an instance, without new
instances. keyword.

A Class can inherit from another class. A Struct is not allowed to inherit from another
struct or class.

The data member of a class can be protected. The data member of struct can’t be protected.

Function member of the class can be virtual or Function member of the struct cannot be virtual
abstract. or abstract.

Two variable of class can contain the reference Each variable in struct contains its own copy of
of the same object and any operation on one data(except in ref and out parameter variable)
variable can affect another variable. and any operation on one variable can not
effect another variable.

You might also like