Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Homework HelpRSS Feeds

Program to sort that set of strings in ascending and descending order of their weights. The sorted output is to be displayed on the screen

Posted By: Easy Tutor     Category: C++ Programming     Views: 14834

Write a program to sort set of strings in ascending and descending order of their weights. The sorted output is to be displayed on the screen.

The first line of the input is an positive integer N (1=<N<=50) equal to the number of strings to be sorted. The next N lines contains N strings to be sorted of lengths greater than equal to 1 and less than equal to 26.

Your job is to display the strings in ascending and descending order of their weights. Print each string on a new line. Print a prompt e.g. “***** Ascending Order *****” before printing strings in ascending order and a prompt before printing strings in descending order.

Code for Program to sort that set of strings in ascending and descending order of their weights. The sorted output is to be displayed on the screen in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 int main( )
    {
       clrscr( );

       fstream File("CP-05.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Number_of_strings[85]={NULL};
       char Strings[50][50]={NULL};
       char Temp[50]={NULL};

       int number_of_strings=0;

       File.getline(Number_of_strings,80);

       number_of_strings=atoi(Number_of_strings);

       int i=0;
       int j=0;

       for(i=0;i<number_of_strings;i++)
      File.getline(Strings[i],26);

       for(i=0;i<(number_of_strings-1);i++)
      {
         for(j=0;j<(number_of_strings-1);j++)
        {
           if(strcmpi(Strings[j],Strings[j+1])>0)
              {
             strset(Temp,NULL);
             strcpy(Temp,Strings[j]);
             strset(Strings[j],NULL);
             strcpy(Strings[j],Strings[j+1]);
             strset(Strings[j+1],NULL);
             strcpy(Strings[j+1],Temp);
              }
        }
      }

       cout<<" *****  Ascending Order  *****"<<endl;

       for(i=0;i<number_of_strings;i++)
      cout<<Strings[i]<<endl;

       for(i=0;i<(number_of_strings-1);i++)
      {
         for(j=0;j<(number_of_strings-1);j++)
        {
           if(strcmpi(Strings[j],Strings[j+1])<0)
              {
             strset(Temp,NULL);
             strcpy(Temp,Strings[j]);
             strset(Strings[j],NULL);
             strcpy(Strings[j],Strings[j+1]);
             strset(Strings[j+1],NULL);
             strcpy(Strings[j+1],Temp);
              }
        }
      }

       cout<<endl<<" *****  Descending Order  *****"<<endl;

       for(i=0;i<number_of_strings;i++)
      cout<<Strings[i]<<endl;

       getch( );
       return 0;
    }

 
  
Share: 



Easy Tutor
Easy Tutor author of Program to sort that set of strings in ascending and descending order of their weights. The sorted output is to be displayed on the screen is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!