Showing posts with label sorting. Show all posts
Showing posts with label sorting. Show all posts

C and C++ Programs for String Sorting

Here i am writing programs in c and c++ languages to sort a given number of strings. Sorting is carried out based on the ASCII values of the letters in the strings. The string comparing function strcmp() is used in the program. These programs sort a given set of strings in ascending order.To change it into descending order just replace > with < in comparison line. The comparison line in the code is marked by comment.

C Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{

C Program to Sort a Matrix Column wise using Pointers

This is a c program to sort a mxn matrix column wise in ascending order using pointers. To sort column wise in descending order, just replace the '>' symbol in the comparing line of code into '<'. That part of the code is marked with a comment 'comparison'.

#include<stdio.h>

main()

{

int a[7][7],i,j,k,m,n,temp;

//reading
printf("enter number of rows of matrix\n");
scanf("%i",&m);
printf("enter number of columns of matrix\n");
scanf("%i",&n);
printf("enter the elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
//displaying
printf("the matrix you entered:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",*(*(a+i)+j));
}
}