C Program Sorting Addresses --------------------------------------- Description:
ID: 3697690 • Letter: C
Question
C Program Sorting Addresses
---------------------------------------
Description: Input consists of names and addresses in the below format: There should be NO USER PROMPTS, all input will come from a file using I/O redirection.
FirstName LastName
2222 Address Lane
My City, State
90210
Chegg Senior
5895 This Street
Programming, NY
92310-4532
-------------------------------------
The program is supposed to take this, store it in a struct, then after all input is received it should output all addresses in zip code order from lowest to heighest using PURE POINTER NOTATION, All memory must be DYNAMICALLY ALLOCATED as data is input. there is no Minimum or Maximum to how many addresses that can be entered. Addresses should be output in the same style as they are input with a space between each address.
Note that a zip code entered in this format (90210-1234) should also be accepted.
---------------------------------------
Rules/Notes:
-Must use Dynamic Memory Allocation
-Must use Pure Pointer Notation
-Do NOT Delete duplicate addresses
-No Global Variables
-Sort from lowest Zip to Highest
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person
{
char name[10];
int zcode ;
Char stname[20];
};
typedef struct person NAME;
NAME stud[10], temp[10];
void main()
{
int no,i;
void sort(int N); /* Function declaration */
clrscr();
fflush(stdin);
printf("Enter the number of students in the list ");
scanf("%d",&no);
for(i = 0; i < no; i++)
{
printf(" Enter the name of person %d : ", i+1);
fflush(stdin);
gets(stud[i].name);
printf("Enter the zip code of %d : ", i+1);
scanf("%d",&stud[i].zcode);
temp[i] = stud[i];
}
printf(" ***************************** ");
printf (" Names before sorting ");
/* Print the list of names before sorting */
for(i=0;i<no;i++)
{
printf("%-10s %3d ",temp[i].name,temp[i].zcode);
}
sort(no); /* Function call */
printf(" ***************************** ");
printf (" Names after sorting ");
printf(" ***************************** ");
/* Display the sorted names */
for(i=0;i<no;i++)
{
printf("%-10s %3d ",stud[i].name,stud[i].zcode);
}
printf(" ***************************** ");
} /* End of main() */
/* Function to sort the given names */
void sort(int N)
{
int i,j;
NAME temp;
for(i = 0; i < N-1;i++)
{
for(j = i+1; j < N; j++)
{
if(strcmp(stud[i].name,stud[j].name) > 0 )
{
temp = stud[i];
stud[i] = stud[j];
stud[j] = temp;
}
}
}
} /* end of sort() */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.