Thursday, April 3, 2014

Old Questions of Fundamentals of Computer Programming (CSc-102), B.Sc. CSIT, First Semester

Wednesday, April 2, 2014

How to find second greatest number in an array? C++ Program

Source Code:

//second greates among array
#include<iostream>
using namespace std;
int great (int a[],int n)
{
     int i,greatest;
     greatest=a[0];
     for (i=0;i<n;i++)
     {
         if(a[i]>greatest)
greatest=a[i];
     }
     return greatest;
}
int second (int a[],int n)
{
    int i,second,g;
    g=great(a,n);
    if(a[0]==g)
second=a[1];
    else
        second=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]>second&&a[i]<g)
        {
            second=a[i];
        }
    }
    return second;
}
int main()
{
    int n,a[40],j;
    cout<<"How many numbers? ";
    cin>>n;
    cout<<"Enter "<<n<<" numbers\n";
    for(j=0;j<n;j++)
        cin>>a[j];
    cout<<"The greatest number is "<<great(a,n)<<endl;
    cout<<"The second greatest number is "<<second(a,n)<<endl;
    system("pause");
    return 0;
}

Output:


Insertion Sort in C++

Source Code:

//insertion sort
#include<iostream>
#include<stdlib.h>
using namespace std;
class insertion
{
    private:
        int n,key;
        int stack[100],temp;        
    public:
        void getinput()
        {
            cout<<"How many nuumbers in array? ";
            cin>>n;
            cout<<"Enter "<<n<<" numbers: ";
            for (int i=0;i<n;i++)
                cin>>stack[i];
        }
        void ascending()
        {
            for (int i=1;i<n;i++) 
            {
                while(i>0 && stack[i]<stack[i-1])
                {
                    temp=stack[i];
                    stack[i]=stack[i-1];
                    stack[i-1]=temp;
                    i--;
                }
            } 
        }
        void descending()
        {
            for (int i=1;i<n;i++) 
            {
                while(i>0&&stack[i]>stack[i-1])
                {
                    temp=stack[i];
                    stack[i]=stack[i-1];
                    stack[i-1]=temp;
                    i--;
                }
            } 
        }
        void showoutput()
        {
            cout<<"\nThe sorted numbers are: ";
            for(int j=0;j<n;j++)
                cout<<stack[j]<<"\t";
            cout<<"\n-----------------------------------------------                    --------------\n";
        }         
}; 
int main()
{
    insertion q;
    int choice;
    cout<<"\n**************************\n******INSERTION SORT******\n**************************\n";
    while (1)
    {
        cout<<"\n1-> Sort in ascending order\n";
        cout<<"2-> Sort in descending order\n";
        cout<<"3-> Exit\n";
        cout<<"\n\nEnter your choice(1, 2 or 3): ";
        cin>>choice;
        switch(choice)
        {
            case(1):
                q.getinput();
                q.ascending();
                q.showoutput();
                break;
            case(2):
                q.getinput();
                q.descending();
                q.showoutput();                
                break;
            case(3):
                exit(1);
        }   
    } 
    system("pause");
    return 0;
}



Output: