Skip to main content

DSL

16: Problem statement:

Write C++ program to store first year percentage of students in array. Write function for sorting array of floating point numbers in ascending order using
a) Selection Sort
b) Bubble sort and display top five scores.


Program:


#include<stdio.h>

void selection(float[],int);
void bubble(float[],int);

void main()
{
    int n,i,op;
    float a[30];

    do
    {
        printf("\n 1)Bubble sort \n 2)Selection sort \n 3)Quit");
        printf("\n Enter your choice :");
        scanf("%d", &op);
        if(op==1)
        {
            printf("\n Enter no. of elements :");
            scanf("%d",&n);
            printf("\n Enter array elements :");
            for(i=0;i<n;i++)
                scanf("%f",&a[i]);
            bubble(a,n);
            printf("\n Sorted array is :");
            for(i=n-1;i>=n-5;i--)
                printf("%7.2f",a[i]);
        }
        if(op==2)
        {
            printf("\n Enter no. of elements :");
                        scanf("%d",&n);
                        printf("\n Enter array elements :");
                        for(i=0;i<n;i++)
                            scanf("%f",&a[i]);
                        selection(a,n);
                        printf("\n Sorted array is :");
                        for(i=n-1;i>=n-5;i--)
                            printf("%7.2f",a[i]);
        }
    }while(op!=3);
}


void bubble(float a[], int n)
{
    int i,j;
    float temp;
    for(i=1; i<n;i++)
        for(j=0;j<n-i;j++)
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;

            }
}


void selection(float a[],int n)
{
    int i,j,k;
    float temp;

    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++)
            if(a[j]<a[k])
                k=j;
        if(k !=i)
        {
            temp=a[i];
            a[i]=a[k];
            a[k]=temp;

        }
    }
}


Comments

Popular posts from this blog

DSL

9: Program Setatement A palindrome is a string of character that‘s the same forward and backward. Typically, punctuation, capitalization, and spaces are ignored. For example, ‖Poor Dan is in a droop‖ is a palindrome, as can be seen by examining the characters ―poor danisina droop‖ and observing that they are the same forward and backward. One way to check for a palindrome is to reverse the characters in the string and then compare with them the original-in a palindrome, the sequence will be identical. Write C++ program with functions- 1. to check whether given string is palindrome or not that uses a stack to determine whether a string is a palindrome. 2. to remove spaces and punctuation in string, convert all the Characters to lowercase, and then call above Palindrome checking function to check for a palindrome 3. to print string in reverse order using stack Program: #include<iostream> #include<string.h> #define MAX 100 using namespace std;  struct stack ...

OOP

6.Problem statement Develop an object oriented program in C++ to create a database of student  information system containing the following information: Name, Roll number, Class, division, Date of Birth, Blood group, Contact address, telephone number, driving license no. etc Construct the database with suitable member functions for initializing and destroying the data viz constructor, default constructor, Copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete. Program #include<iostream> #include<stdio.h> #include<cstring> using namespace std; class personal; static int count; class person {       char *name;       int *rollno,*classno;       char *bloodgroup,*div;                   public:person()              {     ...

DSL

8: Problem statement: Write C++ program for storing binary number using doubly linked lists. Write functions- a) to compute 1‘s and 2‘s complement b) add two binary numbers #include <iostream> #include<vector> using namespace std; struct node { int data; node* next; node* prev; }; node* Create(int x) { node* temp=new node; temp->data=x; temp->next=NULL; temp->prev=NULL; return temp; } void Insert(node** head,int x) { node *temp=Create(x); if(*head==NULL) { *head=temp; } else { (*head)->prev=temp; temp->next=*head; *head=temp; } } void print(node* head) { node* temp=head; cout<<"\ndata: "; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; } cout<<endl; } int sum(int a,int b,int c) { return a^b^c; } int carry(int a,int b,int c) { int x=a*b; int y=b*c; int z=c*a; int s=sum(x,y,z); return s; } ...