Latest Activity In Study Groups

Join Your Study Groups

VU Past Papers, MCQs and More

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.


Problem Statement:

 

In this assignment, you have to write a generic code to sort a dynamic array in ascending order which should work for all (short, int, long, float, double and char) data types.

 

Key points:

 

  • You have to write three template functions in your program which are:
  1. input()
  2. sort()
  3. display()
  • Template function input()  will take two argument: first is template type pointer of dynamic array and other is int type variable will used to save number of values in dynamic array. 

 

  • Template function sort()  will take two arguments: first is template type pointer of dynamic array and other is int type variable will used help to identify number of values in dynamic array what function will sort in ascending order. 

 

  • Template function display()  will take two arguments first is template type pointer of dynamic array and other is int type variable will used for number of iteration to display all values in array.

 

  • In main() function, you are required to create three dynamic arrays of int, float and char type and pass it to functions input(), sort() and display().

Discuss plzzzz

Views: 6994

Attachments:

Replies to This Discussion

G theek ha but aap variable names khud change kro. Or well-defined variable declare kro is mein meiney just idea k liye naam likhey hein. aap apney hisab se declare kro like integer array ko intarr k bjaaye Iarray F_array. Like this...

#include<iostream>
#include<conio.h>
using namespace std;


template< typename T>
void input(T* arr, int x)
{
  for(int i=0; i<x; i++)
  {       int j;
          cout"Enter:";
          cin>>arr[i];
          coutendl;
  } 
}

    template<typename T>
    void sort(T arr[], int n)
   {
     int min;
      T tem;     
      for (int i=0;i<n-1;i++)
      {
         min = i;
         for (int j=i+1;j<n;j++)
         {
             if (arr[j] < arr[min])
             {
                min = j;         
             }
         }
         if (min != i)
         {
          
            tem = arr[i];
            arr[i] = arr[min];
            arr[min] = tem;         
         } 
      }
    }

                  template< typename T>
                  void display(T* arr, int n)
                  {
                      for (int i=0;i<n;i++)
                        {
                                 cout arr[i] "\t ";   
                        }
                  }
                
    int main()
{
  
    int n;
  
  
    int intarr[5];
    cout"Enter The number of Element:\n";
    cin>>n;
    cout"Enter integer values...\n";
    input(intarr, n);
    sort(intarr, 5);
    cout"After Sorting\n";
    display(intarr, n);
    cout endl;
  
    float floarr[5];
     coutendl"Enter floating values...\n";
    input(floarr, n);
    sort(floarr, n);
    cout"After Sorting\n";
    display(floarr, n);   
    cout endl;
  
    char chararr[5];
     coutendl"Enter character values...\n";
    input(chararr, n);
    sort(chararr, n);
    cout"After Sorting\n";
    display(chararr, n);
    cout endl;
      system("pause");
}

 

CS304 assignment solution 

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
template <class t>
class initial
{
t a[25];
public:
void input(int);
void sort(int);
void display(int);
};
template <class t>
void initial <t>::input(int n)
{
int i;
for(i=0; i<n;i++){
cout"Enter:";
cin>>a[i];
cout"\n";
}
}
template <class t>
void initial <t>:: display(int n)
{
cout"\nAfter Sorting\n";
for(int i=0;i<n;i++)
couta[i]"\t";
cout"\n";
}
template <class t>
void initial <t>::sort(int n)
{
int i,j;
t thr;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
thr=a[i];
a[i]=a[j];
a[j]=thr;
}
}
}
}

int main()
{
int t;
cout"Enter The number of Element:"endl;
cin>>t;
{
cout"Enter integer values..."endl;
initial<int> b1;
b1.input(t);
b1.sort(t);
b1.display(t);
};
{
cout"\nEnter floating Values..."endl;
initial<float> b2;
b2.input(t);
b2.sort(t);
b2.display(t); 
}; 

cout"\nEnter character Values..."endl;
initial<char> b3; 
b3.input(t);
b3.sort(t);
b3.display(t);
};

system("pause");
}

Dear Tariq Malik 

do you have any idea for other three data types how they include in this assignment 

the data types are short, long and double

RSS

© 2021   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service