Answer to Question #200259 in C++ for Osama Ahmed

Question #200259

Write a program to arrange fruits names in ascending order. Input will be given by user. Use array to perform the given task. 


1
Expert's answer
2021-05-29T03:36:19-0400
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;


int main()
{
        
        int i, j;
        int n;
        cout<<"Enter number of fruits: ";
        cin>>n;
        char name[n][30], temp[20];
        cout<<"\nEnter fruit names : "<<endl;
        for(i=0; i<n; i++)
        {
                cout<<" ";
                cin>>name[i];
        }
        for(i=1; i<n; i++)
        {
                for(j=1; j<n; j++)
                {
                        if(strcmp(name[j-1], name[j])>0)
                        {
                                strcpy(temp, name[j-1]);
                                strcpy(name[j-1], name[j]);
                                strcpy(name[j], temp);
                        }
                }
        }
        cout<<"\nFruit Names Sorted in Alphabetical Order : "<<endl;
        for(i=0; i<n; i++)
        {
                cout<<name[i]<<endl;
        }
        return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog