Question

Create a typedef called timeType and a struct called timeType_struct with 3 components: an integer hr,...

Create a typedef called timeType and a struct called timeType_struct with 3 components: an integer hr, an integer min, and an integer sec


Create a typedef called tourType and a struct called tourType struct with 3 components: a 50 character city name, an integer distance, and a travel time of type timeType.

Write a void function that takes a pointer parameter to a tourType struct as input and prints it.

Write a value-returning function that prompts for all the data in a tourType and returns a value of tourType.

Create a 3 element array of tourType

Use your function to read the data into structs in the array.

Once the array is populated, find the tour with the shortest distance and use your function to print it.

For Example:

Enter tour data for 3 cities...
Enter a city name: Chicago
Enter an integer distance: 300
Enter travel time as HH:MM:SS 06:00:01
Enter a city name: Pittsburgh
Enter an integer distance: 600
Enter travel time as HH:MM:SS 10:30:00
Enter a city name: Miami
Enter an integer distance: 500
Enter travel time as HH:MM:SS 08:00:00

Shortest Tour
-------------
City name: Chicago
Distance: 300
Travel time: 06:00:01
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
struct timeType_struct {
        int hr;
        int min;
        int sec;
};
typedef struct timeType_struct timeType;
struct tourType {
        char city[50];
        int distance;
        timeType time;
};
typedef struct tourType tourType;
void printTour(tourType *);
tourType getData();
tourType tours[3];
int main() {
        int i=0, min;
        printf("Enter tour data for 3 cities...\n");
        for (i = 0;i < 3;i++) {
                tours[i] = getData();
        }
        min = tours[0].city;
        for (i = 0;i < 3;i++) {
                if (tours[i].distance < min)
                        min = tours[i].distance;
        }
        printf("\n");
        for (i = 0;i < 3; i++) {
                if (tours[i].distance == min) {
                        printTour(&tours[i]);
                }
        }
        getch();
        return 0;
}
void printTour(tourType* x) {
        printf("Shortest Tour\n---------------\n");
        printf("City name: %s\n", (*x).city);
        printf("Distance: %d\n", (*x).distance);
        printf("Travel time: %02d:%02d:%02d", (*x).time.hr, (*x).time.min, (*x).time.sec);
}
tourType getData() {
        tourType x;
        char a[5], b[5], c[5];          //Will be used for time input
        printf("Enter a city name: ");
        scanf("%s", &x.city);
        printf("Enter an integer distance: ");
        scanf("%d", &x.distance);
        printf("Enter travel time as HH:MM:SS ");
        scanf("%[^:]:%[^:]:%[^\n]", a, b, c);                   //store hr, min and sec as char array first
        /*
        %[^:]  -- It means read until : is found and store in first char array
        :  -- read and ignore :
        %[^:]  -- It means read until : is found and store in second char array
        :  -- read and ignore :
        %[^\n]  -- It means read until new line is found and store in third char array
        */
        x.time.hr = atoi(a);
        x.time.min = atoi(b);
        x.time.sec = atoi(c);
        return x;
}
Add a comment
Know the answer?
Add Answer to:
Create a typedef called timeType and a struct called timeType_struct with 3 components: an integer hr,...
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
  • C++: Array of contact info Design a contact struct, that takes your phone struct and address...

    C++: Array of contact info Design a contact struct, that takes your phone struct and address struct along with a c string for a name to create a record of contact information. ( C++: Design a Struct Design two structs address and phone Address has the following attributes: street address, city name, state code, zip code. phone has 3 numbers: area code, prefix and suffix test these by writing a program that creates one of each and fills them with...

  • Please need help, programming in C - Part A You will need to create a struct...

    Please need help, programming in C - Part A You will need to create a struct called Team that contains a string buffer for the team name. After you've defined 8 teams, you will place pointers to all 8 into an array called leaguel], defined the following way: Team leaguel8]. This must be an aray of pointers to teams, not an array of Teams Write a function called game) that takes pointers to two teams, then randomly and numerically determines...

  • I need to create the following struct in C++ id (integer) - identifies the element texture...

    I need to create the following struct in C++ id (integer) - identifies the element texture (string) - specifies the appearance of the element (rabbit, fox, etc.) color (integer) health (double) Dimensions – length, width, and height. All three values are doubles (these variables will be also stored in struct "dimension" ) Hostile (Boolean) – determines if the animal is hostile (1) or peaceful (0) I need a function (outside the struct) that will read the data and inport from...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • C programming. 1.Create a program that does the following - Creates three pointers, a character pointer...

    C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...

  • Create a class called Student. This class will hold the first name, last name, and test...

    Create a class called Student. This class will hold the first name, last name, and test grades for a student. Use separate files to create the class (.h and .cpp) Private Variables Two strings for the first and last name A float pointer to hold the starting address for an array of grades An integer for the number of grades Constructor This is to be a default constructor It takes as input the first and last name, and the number...

  • Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours...

    Activities Define a struct called Unit containing the following fields I Unit code Unit credit hours Unit semester of study List of prerequisites Number of prerequisites List of post requisites Number of post requisites Inside your main function declare an array of type Unit called units with size 20 elements. This array will contain the information related to all the units in our diploma program. Each of the elements of this array correspond with one our diploma units. II III...

  • Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define...

    Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct { char name[MAX_SIZE1]; int scores[MAX_SIZE2]; } 3. The program accepts from the comand line an integer n (<= 30) as the number of students in the class. You may assume that the input will always be valid. 4. Dynamically allocate memory for an array of...

  • Process Create a City structure. The structure will hold the name of a city and its temperature. ...

    using C Process Create a City structure. The structure will hold the name of a city and its temperature. In the main function, read in a series of city names and their temperature expressed in Fahrenheit. Store the data in an array of City structures. Stop reading when either: The user enters the word "quit" for a city name, or The size of the array is about to be exceeded. Create a function that determines the highest temperature of the...

  • matlab please Problem 1. PART A Create a cell array called ca that will contain the following: [Physics] [14][1 4 -...

    matlab please Problem 1. PART A Create a cell array called ca that will contain the following: [Physics] [14][1 4 -3 8] [ITim] [Burnett]] [23] [Jessica] [Wul] [3 9 17] [Chemistry] [8 7 2 91 [Literature] [IJavier] [Lopez] [54] As shown above, the cell array ca should contain within the cells of each row: a department name (eg. Physics), integer code (e.g., 14), an array of integers (e.g., [1 4-3 8]), and two sub-cells containing a person's first and last...

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