Showing posts with label 2. Show all posts
Showing posts with label 2. Show all posts

Wednesday, October 15, 2014

Implement Insertion Sort Algorithms and test it with input of array of length 15 {9,13,15,11,14,1,8, 6,2,7,4,5,10,3,12}.

Implement Insertion Sort Algorithms and test it with input of array of length 15 {9,13,15,11,14,1,8, 6,2,7,4,5,10,3,12}.

CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Labtask4_1
{
    public class Insertion_Sort
    {
        int[] A = { 9, 13, 15, 11, 14, 1, 8, 6, 2, 7, 4, 5, 10, 3, 12 };
        public void Show()
        {
            for (int i = 0; i < 15; i++)
            {
                Console.Write("{0},", A[i]);
            }
            Console.WriteLine();
        }
        public void Sort()
        {
            for (int i = 0; i < 15; i++)
            {
                int num = A[i];
                int j = i - 1;
                while (j >= 0 && A[j] > num)
                {
                    A[j + 1] = A[j];
                    j--;
                }
                A[j + 1] = num;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Insertion_Sort A = new Insertion_Sort();
            A.Show();
            Console.WriteLine("*****AFTER SORTING THE ARRAY*****\n");
            A.Sort();
            A.Show();
            Console.ReadLine();
        }
    }

}

Wednesday, September 17, 2014

Objective:
Write a program to find.
XA + YB
A. B
Test the program using A= (16, -6, 7), B= (4, 2,-3), X= 2, Y= -5



#include<iostream>
using namespace std;
class Vectors
{
public:
       void GetVectorAB(int x, int y, int A[3], int B[3])
       {
              int XA[3];
              int YB[3];
              int ANS[3]={0};
       int i;
       for(i=0; i<3; i++)
       {
       XA[i]=x*A[i];
       }
       for(i=0; i<3; i++)
       {
       YB[i]=y*B[i];
       }
       for(i=0; i<3; i++)
       {
       ANS[i]=XA[i]+YB[i];
       }

       cout<<"XA+YB=(";
       for(i=0; i<3; i++)
       {
              cout<<ANS[i]<<" ";
       }
       cout<<")"<<endl;
       }
      

       void GetVectorAdotB(int A[3], int B[3])
       {
       int dot[3];
       int ab=0;
       int i;
       for(i=0; i<3; i++)
       {
       dot[i]=A[i]*B[i];
       }

       for(i=0; i<3; i++)
       {
              ab=ab+dot[i];
       }

       cout<<"A.B = "<<ab<<endl;
      
       }
};

void main()
{
       int n;
       Abc:
cout<<"What expression you want to do???"<<endl;
cout<<"1:     XA+YB???"<<endl;
cout<<"2:     A.B????"<<endl;
cin>>n;

if(n==1)
{
       int a,b;
       int A[3]={0};
       int B[3]={0};
       cout<<"Enter vector A"<<endl;
       for(int i=0; i<3; i++)
       {
       cin>>A[i];
       }

       cout<<"Enter vector B"<<endl;
       for(int i=0; i<3; i++)
       {
       cin>>B[i];
       }
       cout<<"Enter a constant value for X"<<endl;
       cin>>a;
       cout<<"Enter a constant value for Y"<<endl;
       cin>>b;
       Vectors v1;
       v1.GetVectorAB(a,b,A,B);
}
else
       if(n==2)
       {
              int A[3];
              int B[3];
              cout<<"Enter vector A"<<endl;
              for(int i=0; i<3; i++)
              {
              cin>>A[i];
              }
              cout<<"Enter vector B"<<endl;
              for(int i=0; i<3; i++)
              {
              cin>>B[i];
              }
              Vectors v2;
              v2.GetVectorAdotB(A,B);
       }
       else
       {
       cout<<"Enter a valid number\n\n";
       goto Abc;
       }
}