sorting names

January 4, 2011 at 16:58:54
Specs: Windows XP
In c programming , suppose we enter nams of n students and their marks.we have to display the names in alphabetcal order.This is performed by sorting. But how do we display the equivalent marks of the students whose names are arranged in alphabetical order

See More: sorting names

Report •


#1
January 4, 2011 at 21:08:22
One way might be to create an array of structs and sort it using the name field... the marks will follow. If you can't use structured data, perhaps putz around with a bunch of arrays.

Post your code and someone will help you out.


Report •

#2
January 5, 2011 at 09:55:51
below is the program for performing sorting of height , weight and alphabetical order.In this program when i perform height sort order i get the corresponding name of the student and weight by sorting their variables too. how can i get the corresponding name for sorted height without sorting the names. please help me....
as soon as possible.
CODING:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,y;
char a[10][20],x;
float w[20],h[20],s;
clrscr();
printf("\nenter the number of students:");
scanf("%d",&n);
printf("\nenter the student name, height, weight");
for(i=0;i<n;i++)
{
printf("\nstudent[%d]:",i+1);
scanf("%s",a[i]);
printf("height[%d]:",i+1);
scanf("%f",&h[i]);
printf("weight[%d]:",i+1);
scanf("%f",&w[i]);
}
printf("\nstudents according to height in ascending order");
for(i=1;i<n;i++)
{
for(j=i;j>=1;j--)
{
if(h[j]<h[j-1])
{
s=h[j];
h[j]=h[j-1];
h[j-1]=s;
strcpy(x,a[j]);
strcpy(a[j],a[j-1]);
strcpy(a[j-1],x);
y=w[j];
w[j]=w[j-1];
w[j-1]=y;
}
}
}
for(i=0;i<n;i++)
{
printf("\nstudent=%s\t",a[i]);
printf("weight=%.2f\t",w[i]);
printf("height=%.2f\n",h[i]);
}
printf("\nstudents according to weight in ascending order");
for(i=1;i<n;i++)
{
for(j=i;j>=1;j--)
{
if(w[j]<w[j-1])
{
s=w[j];
w[j]=w[j-1];
w[j-1]=s;
strcpy(x,a[j]);
strcpy(a[j],a[j-1]);
strcpy(a[j-1],x);
y=h[j];
h[j]=h[j-1];
h[j-1]=y;
}
}
}
for(i=0;i<n;i++)
{
printf("\nstudent:%s\t",a[i]);
printf("height:%.2f\t",h[i]);
printf("weight:%.2f\n",w[i]);
}
printf("\nstudents according to alphabetical order");
for(i=1;i<n;i++)
{
for(j=i;j>=1;j--)
{
if(a[j]<a[j-1])
{
strcpy(x,a[j]);
strcpy(a[j],a[j-1]);
strcpy(a[j-1],x);
s=w[j];
w[j]=w[j-1];
w[j-1]=s;

y=h[j];
h[j]=h[j-1];
h[j-1]=y;
}
}
}
for(i=0;i<n;i++)
{
printf("\nstudent:%s\t",a[i]);
printf("height:%.2f\t",h[i]);
printf("weight:%.2f\n",w[i]);
}
getch();
}


Report •

#3
January 5, 2011 at 11:18:35
You've got a few issues to think about, I think

function main should return an int.

scanf is a dangerous function to use... especially for numeric input. You might try creating a temp char array, read in your data into it, and then use something like sscanf to convert the appropriate fields.

you're asking your user to enter the number of students, yet you've hard coded an upper bound for your arrays without checking whether or not "n" is greater than this value.

If your arrays are capable of storing 20 data items, then your char array is wrong, it should be char a[20][20]; char a[max number of students][max length of each name]

You might want to use meaningful variable names; for example, float weight[20], char name[20][20], etc.

you were trying to copy up to 19 chars into the variable "x"; however, its declaration is only 1 byte.

I guess you wanted to sort names in ascending order as well? Use strcmp. I modified your code a bit, but if you remove the initializers and comment block, hopefully, it will run.

#include<stdio.h>
#include<conio.h>

int main(void) {

	int i,j, n=5, y;
	char a[10][20] = { "amy", "fred", "bill", "will", "marry" }, x[20];
	float w[20] = { 90, 110, 120, 78, 94 }, h[20] = { 70, 83, 74, 82, 66 },s;

	clrscr();
/*
	printf("\nenter the number of students:");
	scanf("%d",&n);

	printf("\nenter the student name, height, weight");

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

		printf("\nstudent[%d]:",i+1);
		scanf("%s",a[i]);
		printf("height[%d]:",i+1);
		scanf("%f",&h[i]);
		printf("weight[%d]:",i+1);
		scanf("%f",&w[i]);
	}
*/

	printf("\nstudents according to height in ascending order\n");

	for(i=1;i<n;i++) {

		for(j=i;j>=1;j--) {
			if(h[j]<h[j-1]) {
				s=h[j];
				h[j]=h[j-1];
				h[j-1]=s;
				strcpy(x,a[j]);
				strcpy(a[j],a[j-1]);
				strcpy(a[j-1],x);
				y=w[j];
				w[j]=w[j-1];
				w[j-1]=y;
			}
		}
	}

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

		printf("\nstudent=%s\t",a[i]);
		printf("weight=%.2f\t",w[i]);
		printf("height=%.2f",h[i]);
	}

	printf("\n\nstudents according to weight in ascending order\n");

	for(i=1;i<n;i++) {
		for(j=i;j>=1;j--)	{
			if(w[j]<w[j-1]) {
				s=w[j];
				w[j]=w[j-1];
				w[j-1]=s;
				strcpy(x,a[j]);
				strcpy(a[j],a[j-1]);
				strcpy(a[j-1],x);
				y=h[j];
				h[j]=h[j-1];
				h[j-1]=y;
			}
		}
	}

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

		printf("\nstudent:%s\t",a[i]);
		printf("height:%.2f\t",h[i]);
		printf("weight:%.2f",w[i]);
	}


	printf("\n\nstudents according to alphabetical order\n");

	for(i=1;i<n;i++) {
		for(j=i;j>=1;j--) {
			if(strcmp(a[j], a[j-1]) < 0) {
				strcpy(x,a[j]);
				strcpy(a[j],a[j-1]);
				strcpy(a[j-1],x);
				s=w[j];
				w[j]=w[j-1];
				w[j-1]=s;

				y=h[j];
				h[j]=h[j-1];
				h[j-1]=y;
			}
		}
	}


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

		printf("\nstudent:%s\t",a[i]);
		printf("height:%.2f\t",h[i]);
		printf("weight:%.2f",w[i]);
	}

   getch();

   return 0;
}

HTH


Report •

Related Solutions


Ask Question