Help with Structures in C!! Do the TODO Parts in the files please! There is 3 di
ID: 3845679 • Letter: H
Question
Help with Structures in C!!
Do the TODO Parts in the files please! There is 3 different files Car.c, Car.h, and main.c
Please Copy and paste your output so I know its correct!
Thanks!
HERE IS THE CODE:
/**
* Including C libraries
*/
#include <stdio.h>
#include <stdlib.h>
/**
* Including function prototypes
*/
#include “Car.h”
/**
* Definition of constants
*/
#define INDEX_OF_DOM 6
#define CURRENT_YEAR 2017
#define SUPER_NEW_YEAR 2
#define NEW_YEAR 6
/**
* Prints brand, model and the date
* of manufacture of Car to stdout.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* none.
*/
void print( Car argCar ) {
//TODO
}
/**
* Prints The logo of Car struct to stdout
* and adds a space at end too.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* none.
*/
void printLogo( Car argCar ) {
//Getting the first letter of the brand and the model
//Hint: think string as array of characters.
//TODO
}
/**
* Calculates the age of Car by subtracting
* the current year ( 2017 ) from the Car’s
* year of manufacture.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* The age of Car passed in as argument.
*/
int calculateAge( Car argCar ) {
//Getting the year of manufacture only
//Hint: think string as array of characters.
//TODO
}
/**
* Checks if Car is super new, new or old
* depending on whether it was manufactured
* within 2 years, 6 years or earlier
* Argument:
* argCar -- Car to read information from.
*
* Return:
* 2 if Car is super new. 1 if new.
* 0 if old.
*/
int isNew( Car argCar );
//TODO
}
/**
* Addind C Standard library
*/
#include <stdio.h>
#include <stdlib.h>
/**
* Adding local header files
*/
#include “Car.h”
/**
* Definition of constants
*/
#define NUM_ARGS 4
/**
* Main method to make a Car struct, populate it and
* call helper methods to print Car information.
*
* Arguments:
* argc -- Holds the number of command line
* arguments passed.
* argv -- Holds each command line argument as
* char string.
*
* Error:
* If we don't pass in 3 command line arguments, write an error to stderr
* and terminate the program
*
* Return:
* EXIT_FAILURE upon hitting an error. EXIT_SUCCESS on successful program run.
*/
int main( int argc, char* argv[] ) {
//Checks if we pass in 3 arguments
if( /* TODO */ ) {
fprintf( stderr, "Program only accepts 3 command line arguments. " );
return EXIT_FAILURE;
}
//Make and populate Car struct from command line arguments
Car myCar = //TODO
//Print Car’s logo followed by Car’s information
//TODO: logo first
printf( "Information: " );
//then print Car’s information:
//Print brand and model followed by whether the Car is a super new, new or old.
//TODO
return EXIT_SUCCESS;
}
#ifndef CAR_H
#define CAR_H
//defining string of char as string
typedef char* string;
/**
* Making struct Car with 3 members.
* brand, model and date of manufacture.
*
* Assume valid brand, make and date of manufacture
* when making a struct Car
*/
struct Car {
//TODO
};
//defining struct Car as Car
typedef struct Car Car;
/**
* Prints brand, model and the date
* of manufacture of Car to stdout.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* none.
*/
void print( Car argCar );
/**
* Prints The logo of Car struct to stdout
* and adds a space at end too.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* none.
*/
void printLogo( Car argCar );
/**
* Calculates the age of Car by subtracting
* the current year ( 2017 ) from the Car’s
* year of manufacture.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* The age of Car passed in as argument.
*/
int calculateAge( Car argCar );
/**
* Checks if Car is super new, new or old
* depending on whether it was manufactured
* within 2 years, 6 years or earlier
* Argument:
* argCar -- Car to read information from.
*
* Return:
* 2 if Car is super new. 1 if new.
* 0 otherwise.
*/
int isNew( Car argCar );
#endif //CAR_H
Car struct is simple. You will have 3 members that will hold 3 different facts about a car: Brand, Model and Date of manufacture (format: mm/dd/yyyy). Then you will manipulate the Car struct inside your main method to fill the struct members off of command line arguments passed in then print information about the said Car Here are the steps that your main should take: 1. Check if3 arguments have been passed in. If less or more than 3 arguments have been passed in, then print an error into stderr (already provided) and exit the program. Otherwise, go to step 2. 2. Make a Car struct called myCar and populate its members with the arguments passed in through command line. 3. Print the car's logo (it is formed from the first letter of the brand and first letter from the model, separated and terminated by using a function from Car.c 4. Print the car's information (brand, model and date of manufacture) using a function from Car.c 5. Print the car's full name (brand followed by model) followed by a statement declaring whether this car is old, new or super new. (A car is super new if it was manufactured anytime in the past 2 years, it is new if it was manufactured anytime in the past 6 years and is old if it was manufactured anytime before that). See the sample outputs below for better understanding of the requirements.Explanation / Answer
Here are the completed files with output .Please don't forget to rate the answer if it helped. Thank you very much.
Car.h
#ifndef CAR_H
#define CAR_H
//defining string of char as string
typedef char* string;
/**
* Making struct Car with 3 members.
* brand, model and date of manufacture.
*
* Assume valid brand, make and date of manufacture
* when making a struct Car
*/
struct Car {
string brand;
string model;
string date;
};
//defining struct Car as Car
typedef struct Car Car;
/**
* Prints brand, model and the date
* of manufacture of Car to stdout.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* none.
*/
void print( Car argCar );
/**
* Prints The logo of Car struct to stdout
* and adds a space at end too.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* none.
*/
void printLogo( Car argCar );
/**
* Calculates the age of Car by subtracting
* the current year ( 2017 ) from the Car’s
* year of manufacture.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* The age of Car passed in as argument.
*/
int calculateAge( Car argCar );
/**
* Checks if Car is super new, new or old
* depending on whether it was manufactured
* within 2 years, 6 years or earlier
* Argument:
* argCar -- Car to read information from.
*
* Return:
* 2 if Car is super new. 1 if new.
* 0 otherwise.
*/
int isNew( Car argCar );
#endif //CAR_H
Car.c
/**
* Including C libraries
*/
#include <stdio.h>
#include <stdlib.h>
/**
* Including function prototypes
*/
#include "Car.h"
/**
* Definition of constants
*/
#define INDEX_OF_DOM 6
#define CURRENT_YEAR 2017
#define SUPER_NEW_YEAR 2
#define NEW_YEAR 6
/**
* Prints brand, model and the date
* of manufacture of Car to stdout.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* none.
*/
void print( Car argCar ) {
printf("Brand: %s", argCar.brand);
printf(" Model: %s", argCar.model);
printf(" Date of manufacture: %s",argCar.date);
}
/**
* Prints The logo of Car struct to stdout
* and adds a space at end too.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* none.
*/
void printLogo( Car argCar ) {
//Getting the first letter of the brand and the model
//Hint: think string as array of characters.
printf("%c.%c. ",argCar.brand[0],argCar.model[0]);
}
/**
* Calculates the age of Car by subtracting
* the current year ( 2017 ) from the Car’s
* year of manufacture.
*
* Argument:
* argCar -- Car to read information from.
*
* Return:
* The age of Car passed in as argument.
*/
int calculateAge( Car argCar ) {
//Getting the year of manufacture only
//Hint: think string as array of characters.
int year, diff;
// convert the year part starting at INDEX_OF_DOM into an int
year = atoi (argCar.date + INDEX_OF_DOM);
diff = CURRENT_YEAR - year;
return diff;
}
/**
* Checks if Car is super new, new or old
* depending on whether it was manufactured
* within 2 years, 6 years or earlier
* Argument:
* argCar -- Car to read information from.
*
* Return:
* 2 if Car is super new. 1 if new.
* 0 if old.
*/
int isNew( Car argCar ){
int age = calculateAge(argCar);
if(age <= SUPER_NEW_YEAR)
return 2;
else if(age <= NEW_YEAR)
return 1;
else
return 0;
}
main.c
/**
* Addind C Standard library
*/
#include <stdio.h>
#include <stdlib.h>
/**
* Adding local header files
*/
#include "Car.h"
/**
* Definition of constants
*/
#define NUM_ARGS 4
/**
* Main method to make a Car struct, populate it and
* call helper methods to print Car information.
*
* Arguments:
* argc -- Holds the number of command line
* arguments passed.
* argv -- Holds each command line argument as
* char string.
*
* Error:
* If we don't pass in 3 command line arguments, write an error to stderr
* and terminate the program
*
* Return:
* EXIT_FAILURE upon hitting an error. EXIT_SUCCESS on successful program run.
*/
int main( int argc, char* argv[] ) {
//Checks if we pass in 3 arguments
if( argc != NUM_ARGS ) {
fprintf( stderr, "Program only accepts 3 command line arguments. " );
return EXIT_FAILURE;
}
//Make and populate Car struct from command line arguments
Car myCar = {argv[1], argv[2], argv[3]};
//Print Car’s logo followed by Car’s information
printLogo(myCar);
printf( "Information: " );
//then print Car’s information:
//Print brand and model followed by whether the Car is a super new, new or old.
print(myCar);
int status = isNew(myCar);
if(status == 2)
printf(" %s %s is super new! ",myCar.brand, myCar.model);
else if(status == 1)
printf(" %s %s is new! ",myCar.brand, myCar.model);
else
printf(" %s %s is old! ",myCar.brand, myCar.model);
return EXIT_SUCCESS;
}
output
amoeba-2:cars raji$ gcc Car.c main.c
amoeba-2:cars raji$ ./a.out Chevrolet Cruze 11/11/2015
C.C. Information:
Brand: Chevrolet
Model: Cruze
Date of manufacture: 11/11/2015
Chevrolet Cruze is super new!
amoeba-2:cars raji$ ./a.out Honda Civic 11/01/2011
H.C. Information:
Brand: Honda
Model: Civic
Date of manufacture: 11/01/2011
Honda Civic is new!
amoeba-2:cars raji$ ./a.out Ford Focu 03/01/2006
F.F. Information:
Brand: Ford
Model: Focu
Date of manufacture: 03/01/2006
Ford Focu is old!
amoeba-2:cars raji$ ./a.out Nissan 03/01/2006
Program only accepts 3 command line arguments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.