Change this code to sort AND serach them by zip code ZIP cod
Change this code to sort AND serach them by zip code. ZIP code MUST remain \"char\" data type!
code:
#include <stdio.h>
 #include <stdlib.h>
struct file {
 char name[20];
 char address[30];
 char city[20];
 char st[3];
 char zip[6];
 int age;
 };
 int main()
 {
 int i, j, flag = 1, jump = 20, tgt, frst, last, mid;
 char more = \'Y\';
 struct file temp;
 struct file data[] = {
 { \"John J. Doe\", \"202 Harvey Rd\", \"College Station\", \"TX\", \"77450\", 29},
 { \"Tama Martin\", \"202 College Main\", \"Houston\", \"TX\", \"72002\", 10},
 { \"Dick Simmons\", \"Box 1010\", \"El Paso\", \"TX\", \"72081\", 55},
 { \"Udo Pooch\", \"1004 Windwood\", \"Houston\", \"AR\", \"02400\", 80},
 { \"Aarron Stein\", \"1000 Spicewood\", \"New York\", \"NY\", \"23932\", 90},
 { \"Steve jobs\", \"2082 RainTree\", \"College Station\", \"TX\", \"30020\", 45},
 { \"Robert Ricks\", \"8378 University DR.\", \"Bryan\", \"TX\", \"72001\", 25},
 { \"Richard Ticks\", \"2498 C. Main\", \"Los Angeles\", \"CA\", \"20020\", 18},
 { \"Tim Safferd\", \"3303 George Bush Dr.\", \"Bryan\", \"TX\", \"20200\", 12},
 { \"Rickard Thamos\", \"7438 RoseMerry\", \"Salt Lake\", \"UT\", \"83298\", 73},
 { \"Mary Hart\", \"3998 Hallow St\", \"Anthem\", \"CA\", \"00329\", 30},
 { \"Todd Hoffer\", \"2939 univ. Blvd\", \"Huntsville\", \"AL\", \"23890\", 63},
 { \"Shannon Shoumaker\", \"2300 Decon\", \"Dallas\", \"TX\", \"73999\", 24},
 { \"Mike Meeks\", \"Box 2839\", \"Paris\", \"TX\", \"72990\", 43},
 { \"Mikey Monique\", \"Box 3899\", \"Kansas City\", \"MO\", \"38982\", 65},
 { \"Joe Montana\", \"10000 Hardwood\", \"San Francisco\", \"CA\", \"83999\", 33},
 { \"Tom Landry\", \"48923 Software\", \"Seattle\", \"WN\", \"43223\", 13},
 { \"Larry Bird\", \"3298 Hilow Ln\", \"Kauffnan\", \"AK\", \"84920\", 39},
 { \"Denis Cohen\", \"Box 2399\", \"Las Cruis\", \"NM\", \"73289\", 28},
 { \"Fay Maybree\", \"399 Harvey Rd\", \"Wisconsin\", \"MO\", \"48299\", 32},
 };
printf (\"\ \ Sort by age in increasing order. (BUBBLE SORT)\ \");
for (i=0;i<20;i++)
 for (j=0;j<20-i;j++)
 if (data[j].age>data[j+1].age) {
 temp = data[j];
 data[j] = data[j+1];
 data[j+1] = temp;
 }
 for (i=0;i<20;i++)
 printf(\"\\t%20s\\t%.5s\\t%7d\ \", data[i].name, data[i].zip, data[i].age);
 printf (\"\ Hit any key to continue . . .\");
 system(\"pause\");
printf (\"\ \ Sort by age in decreasing order. (SHELL SORT)\ \");
 for (jump = jump / 2; jump > 0; flag = 1, jump = jump / 2) {
 while (flag == 1) {
 flag = 0;
 for (i=0;i<20-jump;i++)
 if (data[i].age<data[i+jump].age) {
 temp = data[i];
 data[i] = data[i+jump];
 data[i+jump] = temp;
 flag = 1;
 }
 }
 }
 for (i = 0;i < 20; i++)
 printf (\"\\t%-20s\\t%7.5s\\t%6d\ \", data[i].name, data[i].zip, data[i].age);
 printf(\"\ Hit any key to continue . . . \");
system(\"pause\");
while (more == \'y\' || more == \'Y\') {
 printf (\"\ \ Please input an age to search : \"); scanf (\"%d\", &tgt);
 frst = 0; last = 19; flag = 1;
 while (frst <= last && flag ==1) {
 mid = (frst+last)/2;
 if (data[mid].age == tgt) {
 printf(\"\ \ \\t\\tI am lucky, data at position %d\", mid +1);
 printf(\"\ \\t%s, %s, %s, %.2s %.5s age = %d\", data[mid].name, data[mid].address, data[mid].city, data[mid].st, data[mid].zip, data[mid].age);
 flag = 0;
 }
 else
 if (data[mid].age > tgt)
 frst = mid + 1;
 else
 last = mid -1;
 }
 if (flag == 1)
 printf(\"\ \ \\t\\tSorry! Your input is not found in this array . . .\");
 printf(\"\ \ \\t\\tDo more (Y/N) ? \");
 scanf(\"%s\", &more);
 }
 return 0;
 }
Solution
#include <stdio.h>
 #include <stdlib.h>
 struct file {
 char name[20];
 char address[30];
 char city[20];
 char st[3];
 char zip[6];
 int age;
 };
int main()
 {
 int i, j, flag = 1, jump = 20, tgt, frst, last, mid;
 char more = \'Y\';
 struct file temp;
 struct file data[] = {
 { \"John J. Doe\", \"202 Harvey Rd\", \"College Station\", \"TX\", \"77450\", 29},
 { \"Tama Martin\", \"202 College Main\", \"Houston\", \"TX\", \"72002\", 10},
 { \"Dick Simmons\", \"Box 1010\", \"El Paso\", \"TX\", \"72081\", 55},
 { \"Udo Pooch\", \"1004 Windwood\", \"Houston\", \"AR\", \"02400\", 80},
 { \"Aarron Stein\", \"1000 Spicewood\", \"New York\", \"NY\", \"23932\", 90},
 { \"Steve jobs\", \"2082 RainTree\", \"College Station\", \"TX\", \"30020\", 45},
 { \"Robert Ricks\", \"8378 University DR.\", \"Bryan\", \"TX\", \"72001\", 25},
 { \"Richard Ticks\", \"2498 C. Main\", \"Los Angeles\", \"CA\", \"20020\", 18},
 { \"Tim Safferd\", \"3303 George Bush Dr.\", \"Bryan\", \"TX\", \"20200\", 12},
 { \"Rickard Thamos\", \"7438 RoseMerry\", \"Salt Lake\", \"UT\", \"83298\", 73},
 { \"Mary Hart\", \"3998 Hallow St\", \"Anthem\", \"CA\", \"00329\", 30},
 { \"Todd Hoffer\", \"2939 univ. Blvd\", \"Huntsville\", \"AL\", \"23890\", 63},
 { \"Shannon Shoumaker\", \"2300 Decon\", \"Dallas\", \"TX\", \"73999\", 24},
 { \"Mike Meeks\", \"Box 2839\", \"Paris\", \"TX\", \"72990\", 43},
 { \"Mikey Monique\", \"Box 3899\", \"Kansas City\", \"MO\", \"38982\", 65},
 { \"Joe Montana\", \"10000 Hardwood\", \"San Francisco\", \"CA\", \"83999\", 33},
 { \"Tom Landry\", \"48923 Software\", \"Seattle\", \"WN\", \"43223\", 13},
 { \"Larry Bird\", \"3298 Hilow Ln\", \"Kauffnan\", \"AK\", \"84920\", 39},
 { \"Denis Cohen\", \"Box 2399\", \"Las Cruis\", \"NM\", \"73289\", 28},
 { \"Fay Maybree\", \"399 Harvey Rd\", \"Wisconsin\", \"MO\", \"48299\", 32},
 };
printf (\"\ \ Sort by ZIP CODE in increasing order. (BUBBLE SORT)\ \");
for (i=0;i<20;i++)
 for (j=0;j<20-i;j++)
 if (atoi(data[j].zip) > atoi(data[j+1].zip))
 {
 temp = data[j];
 data[j] = data[j+1];
 data[j+1] = temp;
 }
 for (i=0;i<20;i++)
 printf(\"\\t%20s\\t%.5s\\t%7d\ \", data[i].name, data[i].zip, data[i].age);
 printf (\"\ Hit any key to continue . . .\");
 system(\"pause\");
 printf (\"\ \ Sort by ZIP CODE in decreasing order. (SHELL SORT)\ \");
 for (jump = jump / 2; jump > 0; flag = 1, jump = jump / 2) {
 while (flag == 1) {
 flag = 0;
 for (i=0;i<20-jump;i++)
 if (atoi(data[i].zip)<atoi(data[i+jump].zip)) {
 temp = data[i];
 data[i] = data[i+jump];
 data[i+jump] = temp;
 flag = 1;
 }
 }
 }
for (i = 0;i < 20; i++)
 printf (\"\\t%-20s\\t%7.5s\\t%6d\ \", data[i].name, data[i].zip, data[i].age);
 printf(\"\ Hit any key to continue . . . \");
 system(\"pause\");
 while (more == \'y\' || more == \'Y\') {
 printf (\"\ \ Please input an ZIP CODE to search : \"); scanf (\"%d\", &tgt);
 frst = 0; last = 19; flag = 1;
 while (frst <= last && flag ==1) {
 mid = (frst+last)/2;
 if (atoi(data[mid].zip) == tgt) {
 printf(\"\ \ \\t\\tI am lucky, data at position %d\", mid +1);
 printf(\"\ \\t%s, %s, %s, %.2s %.5s age = %d\", data[mid].name, data[mid].address, data[mid].city, data[mid].st, data[mid].zip, data[mid].age);
 flag = 0;
 }
 else
 if (atoi(data[mid].zip) > tgt)
 frst = mid + 1;
 else
 last = mid -1;
 }
 if (flag == 1)
 printf(\"\ \ \\t\\tSorry! Your input is not found in this array . . .\");
 printf(\"\ \ \\t\\tDo more (Y/N) ? \");
 scanf(\"%s\", &more);
 }
 return 0;
 }
---------------------------------------------------------------------------------
output sample :-
Sort by ZIP CODE in increasing order. (BUBBLE SORT)
 Mary Hart 00329 30
 Udo Pooch 02400 80
 Richard Ticks 20020 18
 Tim Safferd 20200 12
 Todd Hoffer 23890 63
 Aarron Stein 23932 90
 Steve jobs 30020 45
 Mikey Monique 38982 65
 Tom Landry 43223 13
 Fay Maybree 48299 32
 Robert Ricks 72001 25
 Tama Martin 72002 10
 Dick Simmons 72081 55
 Mike Meeks 72990 43
 Denis Cohen 73289 28
 Shannon Shoumaker 73999 24
 John J. Doe 77450 29
 Rickard Thamos 83298 73
 Joe Montana 83999 33
 Larry Bird 84920 39
Hit any key to continue . . .Press any key to continue . . .
 Sort by ZIP CODE in decreasing order. (SHELL SORT)
 Larry Bird 84920 39
 Joe Montana 83999 33
 Rickard Thamos 83298 73
 John J. Doe 77450 29
 Shannon Shoumaker 73999 24
 Denis Cohen 73289 28
 Mike Meeks 72990 43
 Dick Simmons 72081 55
 Tama Martin 72002 10
 Robert Ricks 72001 25
 Fay Maybree 48299 32
 Tom Landry 43223 13
 Mikey Monique 38982 65
 Steve jobs 30020 45
 Aarron Stein 23932 90
 Todd Hoffer 23890 63
 Tim Safferd 20200 12
 Richard Ticks 20020 18
 Udo Pooch 02400 80
 Mary Hart 00329 30
Hit any key to continue . . . Press any key to continue . . .
 Please input an ZIP CODE to search :
 00329
 I am lucky, data at position 20
 Mary Hart, 3998 Hallow St, Anthem, CA 00329 age = 30
Do more (Y/N) ? y
 Please input an ZIP CODE to search : 77450
 I am lucky, data at position 4
 John J. Doe, 202 Harvey Rd, College Station, TX 77450 age = 29
Do more (Y/N) ?
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.





