Programming Assignment Using Strings and Files - Computer Programming w/ C For t
ID: 3763154 • Letter: P
Question
Programming Assignment Using Strings and Files - Computer Programming w/ C
For this assignment your program will read in data from a file and store it in an array or arrays for lookup. The data file contains the top 200 boy and girl baby names for 2014. The data file is tab delimited which makes it feasible to use fscanf() for input rather than having to parse the input yourself.
You should design the program to handle files with any number of baby names. DO NOT hard code the number 200. You need to count the number of lines in the file.
The program will present a menu to the user with the options:
1. Search Boy Names
2. Search Girl Names
3. Display the Boy Names 4. Display the Girl Names 5. Quit
The program will continue until option 5 is selected.
The user will be asked to enter a name and then search the appropriate name type (boy or girl names) for a match. If the name is found the program will display on the screen:
<input name> is in the top <name count> <girl or boy> names for 2014 with the rank of <rank>.
For example:
Mary is in the top 200 girl names for 2014 with the rank of 152.
If no match is found the program will display to the screen:
<input name> is not in the top <name count> <girl or boy> names for 2014.
The output for displaying the boy names will look like:
Top <name count> Boy Names for 2014 Rank Name
1 Boy Name
2 Boy Name
3 Boy Name
until you reach the end.
The output for displaying the girl names will look like:
Top <name count> Girl Names for 2014 Rank Name
### Girl Name
### Girl Name
until you reach the end.
The program may need to convert the input to all uppercase or lowercase characters for comparison. This also applies to the names to be searched. Or you can convert to lowercase or uppercase at the time of comparison (this would be slightly less efficient, but for our purposes it won’t be a problem).
You will need to create a function(s) to search the tables and a function(s) to display the names on the screen.
Advice: Get one piece of functionality working at a time then add another. Example: Write the code to open, read, and close the file; store the data in arrays; produce the menu structure; get user input...you get the picture. Though not necessarily in that order.
Here is the code I have so far. I just need help writting the last two functions that read and display the names.
#include <stdio.h>
#include <string.h>
void DisplayBoysGirls(char [][20],int, char *);
void SearchBoysGirls(char [][20],int, char *);
int ReadFile(char [][20],char [][20]);
main()
{
char boys[300][20],girls[300][20];
int retCode, choice, NameCount;
NameCount = ReadFile(boys,girls);
do
{
printf(" 1. Search Boy Names ");
printf(" 2. Search Girl Names ");
printf(" 3. Display Boy Names ");
printf(" 4. Display Girl Names ");
printf(" 5. Quit ");
scanf("%d",&choice);
switch (choice)
{
case 1: SearchBoysGirls(boys,NameCount,"boy");
break;
case 2: SearchBoysGirls(girls,NameCount,"girl");
break;
case 3: DisplayBoysGirls(boys,NameCount,"BOY");
break;
case 4: DisplayBoysGirls(girls,NameCount,"GIRL");
break;
}
}
while (choice != 5);
}
int ReadFile(char B[][20],char G[][20])
{
FILE *inFile;
int i,NameLen,NameCount=0;
char rank[4],boy[20], girl[20];
inFile=fopen("boygirlnames2014.txt","r");
if (inFile == NULL)
{
printf("File cannot be opened.");
fclose(inFile);
return 1;
}
while (fscanf(inFile, "%s%s%s", rank,B[NameCount],G[NameCount])!= EOF)
{
NameLen=strlen(B[NameCount]);
for(i=0;i<NameLen;i++)
B[NameCount][i]=toupper(B[NameCount][i]);
NameLen=strlen(G[NameCount]);
for(i=0;i<NameLen;i++)
G[NameCount][i]=toupper(G[NameCount][i]);
NameCount++;
}
fclose(inFile);
return NameCount;
}
void SearchBoysGirls (char BG[][20],int VarieCount, char *BoyGirl);
{
printf("Enter a name of a boy or a girl:")
scanf
}
void DisplayBoysGirls (char BG[][20],int VarieCount, char *BoyGirl);
{
if
else
printf("<input name> is not in the top <name count> <girl or boy> names for 2014.",);
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
void DisplayBoysGirls(char [][20],int, char *);
void SearchBoysGirls(char [][20],int, char *);
int ReadFile(char [][20],char [][20]);
main()
{
char boys[300][20],girls[300][20];
int retCode, choice, NameCount;
NameCount = ReadFile(boys,girls);
printf("NameCount = %d ", NameCount);
do
{
printf(" 1. Search Boy Names ");
printf(" 2. Search Girl Names ");
printf(" 3. Display Boy Names ");
printf(" 4. Display Girl Names ");
printf(" 5. Quit ");
scanf("%d",&choice);
switch (choice)
{
case 1: SearchBoysGirls(boys,NameCount,"boy");
break;
case 2: SearchBoysGirls(girls,NameCount,"girl");
break;
case 3: DisplayBoysGirls(boys,NameCount,"BOY");
break;
case 4: DisplayBoysGirls(girls,NameCount,"GIRL");
break;
}
}
while (choice != 5);
}
int ReadFile(char B[][20],char G[][20])
{
FILE *inFile;
int i,NameLen,NameCount=0;
char rank[4],boy[20], girl[20];
inFile=fopen("boygirl.txt","r");
if (inFile == NULL)
{
printf("File cannot be opened.");
fclose(inFile);
return 1;
}
while (fscanf(inFile, "%s%s%s", rank,B[NameCount],G[NameCount])!= EOF)
{
NameLen=strlen(B[NameCount]);
for(i=0;i<NameLen;i++)
B[NameCount][i]=toupper(B[NameCount][i]);
NameLen=strlen(G[NameCount]);
for(i=0;i<NameLen;i++)
G[NameCount][i]=toupper(G[NameCount][i]);
NameCount++;
}
fclose(inFile);
return NameCount;
}
void SearchBoysGirls (char BG[][20],int VarieCount, char *BoyGirl)
{
char name[20];
int NameLen;
int flag = 0, i, k = 0;
if (BoyGirl == "boy") {
printf("Enter a name of a boy: ");
scanf("%s", name);
while (name[k]) {
name[k] = toupper(name[k]);
k++;
}
for (i = 0; i < VarieCount; i++) {
if (strcmp(name, BG[i]) == 0) {
printf("%s is in the top %d boy names for 2014 with the rank of %d ", name, VarieCount, i+1);
flag = 1;
break;
}
}
if (flag == 0) {
printf("%s is not in the top %d boy names for 2014. ", name, VarieCount);
}
}
else {
printf("Enter a name of a girl: ");
scanf("%s", name);
while (name[k]) {
name[k] = toupper(name[k]);
k++;
}
for (i = 0; i < VarieCount; i++) {
if (strcmp(name, BG[i]) == 0) {
printf("%s is in the top %d girl names for 2014 with the rank of %d ", name, VarieCount, i+1);
flag = 1;
break;
}
}
if (flag == 0) {
printf("%s is not in the top %d girl names for 2014. ", name, VarieCount);
}
}
}
void DisplayBoysGirls (char BG[][20],int VarieCount, char *BoyGirl)
{
if (BoyGirl == "BOY") {
printf("Top %d Boy Names for 2014 Rank Name ", VarieCount);
int i;
for (i = 0; i < VarieCount; i++) {
printf("%d %s ", i+1, BG[i]);
}
}
else {
printf("Top %d Girl Names for 2014 Rank Name ", VarieCount);
int i;
for (i = 0; i < VarieCount; i++) {
printf("%d %s ", i+1, BG[i]);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.