Computer Science, asked by kishoreji3346, 1 year ago

Write c++ programs arrange the list of names using sorting technique

Answers

Answered by manjusreem97
0

Answer:

Explanation:

#include <bits/stdc++.h>

using namespace std;

//function to print the array

void print(vector<string> names){

printf("printing ........\n");

for(int i=0;i<names.size();i++)

 cout<<names[i]<<endl;

printf("\n");

}

bool mycomp(string a, string b){

//returns 1 if string a is alphabetically

//less than string b

//quite similar to strcmp operation

return a<b;

}

vector<string> alphabaticallySort(vector<string> a){

int n=a.size();

//mycomp function is the defined function which

//sorts the strings in alphabatical order

sort(a.begin(),a.end(),mycomp);

return a;

}

int main()

{  

int n;

printf("enter number of names to be added: ");

scanf("%d",&n);

//creating a vector of strings

//vector to store strings(names)

vector<string> names;

string name;

printf("enter names: \n");

//taking input

for(int i=0;i<n;i++){

 cin>>name;

 //insert names into the vector

 names.push_back(name);

}

printf("\nbefore sorting\n");

print(names);

//function to sort names alphabetically

names=alphabaticallySort(names);

printf("after alphabetical sorting\n");

print(names);

return 0;

}

Answered by rishabhagrawal0
0

Answer:

SHOWN HERE:

Explanation:

#include<iostream>

#include<string.h>

#include<stdio.h>

using namespace std;

int main()

{

       char str[5][20], t[20];

       int i, j;

       cout<<"\n Enter Any Five Names : \n\n";

       for(i=0; i<5; i++)

       {

               cout<<" ";

               cin>>str[i];

       }

       for(i=1; i<5; i++)

       {

               for(j=1; j<5; j++)

               {

                       if(strcmp(str[j-1], str[j])>0)

                       {

                               strcpy(t, str[j-1]);

                               strcpy(str[j-1], str[j]);

                               strcpy(str[j], t);

                       }

               }

       }

       cout<<"\n Names Sorted in Alphabetical Order : \n\n";

       for(i=0; i<5; i++)

       {

               cout<<" ";

               cout<<str[i]<<"\n";

       }

       return 0;

}

Attachments:
Similar questions