Question
Write the program in C


The following statistics for cities of the Vege country are stored in a file: Name: a string of characters less than 15 characters long. Population: an integer value. Area: square miles of type double Write a program that: asks and gets the name of the input file from the user. Read the contents of the file into an array length 10 of structures -ask the user for the name of a city (less than 15 characters long) and store it in the proper length string. Search the array of structures for a match and if found output the name of the city and its population density to the screen. A name match should ignore the case; ex: tomato and ToMAto are a match. Ask the user for an output file name Write the name of each city and its population density to the output file, on separate lines Each output record has to be similar in format to input.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

struct City cityList[10: struct City ci; //if file in not valid if (inPointNULL) fprintf (stderr, \nFile error, please input//crate the file pointer by opeing the file FILE *outPointi outPoint-fopen (output, w) for (1-0; i<10;i++) fprintf (outPoincitylist.txt X main.cpp* Vegas 10000 12.3 NewYork 20000 16.5 London 30000 17.4 Berlin 200 2.3 Delhi 50000 20 Sydney 10000 20

Copyable Content:

Executable Code:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

//The city structure

struct City

{

char Name[15];

int Population;

double Area;

};

// Driver program

int main ()

{

int num=0;

//To take the input file

char input[20];

printf("Enter the file name: ");

scanf("%s", input);

//crate the file pointer by opeing the file

FILE *inPoint;

inPoint=fopen (input, "r");

//crate the array of structure

struct City cityList[10];

struct City ci;

//if file in not valid

if (inPoint == NULL)

{

fprintf(stderr, "\nFile error, please input correct file\n");

exit (1);

}

//Read and store the file contents into the structre

char buff[15];

fscanf(inPoint, "%s", buff);

printf("1 : %s\n", buff );

while(num<10)

{

strcpy(cityList[num].Name,buff);

fscanf(inPoint, "%d", &cityList[num].Population);

fscanf(inPoint, "%lf", &cityList[num].Area);

printf ("%d : %s %d %lf\n", num, cityList[num].Name, cityList[num].Population, cityList[num].Area);

fscanf(inPoint, "%s", buff);

num++;

}

char buff1[15];

//read the city from user

printf("Enter the city to search: ");

scanf("%s", buff1);

int i;

for(i=0; i<strlen(buff1); i++)

{

buff1[i]=tolower(buff1[i]);

}

char tem[15];

int j=0;

for(i=0; i<10;i++)

{

strcpy(tem,cityList[i].Name);

for(j=0; j<strlen(tem); j++)

{

tem[j]=tolower(tem[j]);

}

if(strcmp(tem, buff1)==0)

{

printf("The city %s", cityList[i].Name);

printf("has a population density of %lf\n\n", cityList[i].Population/cityList[i].Area);

}

}

char output[20];

printf("Enter the output file name: ");

scanf("%s", output);

//crate the file pointer by opeing the file

FILE *outPoint;

outPoint=fopen (output, "w");

for(i=0; i<10;i++)

{

fprintf(outPoint, cityList[i].Name);

double t= cityList[i].Population/(double)cityList[i].Area;

fprintf(outPoint, "%lf\n", t);

}

fclose(outPoint);

return 0;

}

Input File example:

Vegas 10000 12.3

NewYork 20000 16.5

London 30000 17.4

Berlin 2000 2.3

Delhi 50000 20

Sydney 10000 20

Paris 30000 15.4

Mumbai 20000 23

chennai 50000 20

Bres 10000 20

Add a comment
Know the answer?
Add Answer to:
Write the program in C The following statistics for cities of the Vege country are stored...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to write a loop that examines the names of the cities stored in the...

    I need to write a loop that examines the names of the cities stored in the array. Write code that tests for a match Write code that, when appropriate, prints the message: Not a city in Illinois _______________________________________________________________ ' IllinoisCities.vb - This program prints a message for invalid cities in Illinois. ' Input: Interactive ' Output: Error message or nothing Option Explicit On Option Strict On Module IllinoisCities    Sub Main()       ' Declare variables.         Dim city As String                 ...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of...

    ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of a file. The program encoded sentences in the file and writes the encoded sentences to the output file. Enter the file name: messages.txt Output: encoded words are written to file: messages.txt.swt The program reads the content of the file and encodes a sentence by switching every alphabetical letter (lower case or upper case) with alphabetical position i, with the letter with alphabetical position 25...

  • 8. (15 marks) Write a complete C program, which uses an array of structures and two...

    8. (15 marks) Write a complete C program, which uses an array of structures and two user defined functions. The program deals with a library database. The program will ask the user to input required information about each book and store it in a structure in a user defined function called get info. The second user defined function display_info will display database information on the screen. The output should look as follows: Book Title Book ld 123456 C Programming 10...

  • *In Python please***** This program will display some statistics, a table and a histogram of a...

    *In Python please***** This program will display some statistics, a table and a histogram of a set of cities and the population of each city. You will ask the user for all of the information. Using what you learned about incremental development, consider the following approach to create your program: Prompt the user for information about the table. First, ask for the title of this data set by prompting the user for a title for data, and then output the...

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT