Question

In: Computer Science

#include #include #include #define WORDLENGTH 30 #define MAX 100 struct car { char model[WORDLENGTH]; int year;...

#include

#include

#include

#define WORDLENGTH 30

#define MAX 100

struct car

{

char model[WORDLENGTH];

int year;

int milage;

};

typedef struct car Car;

Car createCar(char model[], int year, int milage)

{

Car c;

strcpy(c.model, model);

c.year = year;

c.milage = milage;

return c;

}

void regCars(Car reg[], int *pNrOfCars)

{

char again[WORDLENGTH] = "yes", model[WORDLENGTH];

int year, milage;

while (strcmp(again, "yes") == 0)

{

printf("Ange model:");

scanf("%s%*c", model);

printf("Ange year:");

scanf("%d%*c", &year);

printf("Ange milage:");

scanf("%d%*c", &milage);

reg[*pNrOfCars] = createCar(model, year, milage);

(*pNrOfCars)++;

printf("Do you rigister another car? enter yes or no:\n");

scanf("%s%*c", again);

}

}

void printRegister(Car reg[], int nrOfCars)

{

int i = 0;

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

{

printf("%d. Car: %s, Year model: %d, Mil: %d\n", i + 1, reg[i].model, reg[i].year, reg[i].milage);

}

}

int main()

{

Car carRegister[MAX];

int nrOfCars = 0;

char r[100];

printf("register a car\n");

regCars(carRegister, &nrOfCars);

printf("Do you want to print the cars details or quit? entr print or quit:\n");

scanf("%s", r);

if (strcmp(r, "print") == 0)

printRegister(carRegister, nrOfCars);

else

exit(1);

return 0;

}

Add a menu item that is "Increase miles" where you should be able to select a car by entering its location in the register and then can increase the number of miles it has traveled. So to increase the fiat's miles above, enter 3.

Edit: this question is a following question to a previous one which was:

In question number one we should Add the function that prints all cars in the register in our car register program. The printout should look as follows:
1. Car: Volvo, Year model: 2011, Mil: 3000
2. Car: Saab, Year model: 2000, Mil: 5000
Car: Fiat, Model year: 1999, Mil: 40000


Then in a separate question we should Change the program so that it starts with a menu according to
below. Depending on what you choose, the correct function is called. Then the user gets another chance to choose until he chooses to quit.

• register cars

• Print all cars
• Exit

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define WORDLENGTH 30

#define MAX 100

struct car{
char model[WORDLENGTH];

int year;
int milage;

};
typedef struct car Car;

void printCar(Car c){
printf("Bil: %s, Arsmodell: %d, mil: %d\n",c.model,c.year,c.milage);

}

Car createCar(char model[],int year, int milage){

Car c;

strcpy(c.model,model);

c.year=year;

c.milage=milage;

return c;

}

void regCars(Car reg[],int *pNrOfCars){
char again[WORDLENGTH] = "ja", model[WORDLENGTH];

int year,milage;
while(strcmp(again,"ja")==0){

printf("Ange model:");
scanf("%s%*c",model);
printf("Ange year:");
scanf("%d%*c",&year);
printf("Ange milage:");

scanf("%d%*c",&milage);

reg[*pNrOfCars]=createCar(model,year,milage);

(*pNrOfCars)++;

printf("Vill du fortsatta? (ja/nej)");

scanf("%s%*c",again); }

}

void printRegister(Car reg[], int nrOfCars){}

int main(){
Car carRegister[MAX];
int nrOfCars=0;

regCars(carRegister,&nrOfCars);

printRegister(carRegister, nrOfCars);

return 0;

}

the answer was:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define WORDLENGTH 30

#define MAX 100

struct car{
char model[WORDLENGTH];

int year;
int milage;

};
typedef struct car Car;




Car createCar(char model[],int year, int milage){

Car c;

strcpy(c.model,model);

c.year=year;

c.milage=milage;

return c;

}

void regCars(Car reg[],int *pNrOfCars){
char again[WORDLENGTH] = "yes", model[WORDLENGTH];

int year,milage;
while(strcmp(again,"yes")==0){

printf("Ange model:");
scanf("%s%*c",model);
printf("Ange year:");
scanf("%d%*c",&year);
printf("Ange milage:");

scanf("%d%*c",&milage);

reg[*pNrOfCars]=createCar(model,year,milage);

(*pNrOfCars)++;

printf("Do you rigister another car? enter yes or no:\n");

scanf("%s%*c",again); }

}

void printRegister(Car reg[], int nrOfCars){
    int i=0;
    for(i=0;i<nrOfCars;i++)
    {
        printf("%d. Car: %s, Year model: %d, Mil: %d\n",i+1,reg[i].model,reg[i].year,reg[i].milage);
    }
}

int main(){
Car carRegister[MAX];
int nrOfCars=0;
char r[100];
printf("register a car\n");
regCars(carRegister,&nrOfCars);
printf("Do you want to print the cars details or quit? entr print or quit:\n");
scanf("%s",r);
if(strcmp(r,"print")==0)
printRegister(carRegister, nrOfCars);
else 
exit(1);
return 0;

}

Solutions

Expert Solution

Updated program with an 'Increase miles' option is below:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WORDLENGTH 30
#define MAX 100

struct car{
char model[WORDLENGTH];
int year;
int milage;
};
typedef struct car Car;

Car createCar(char model[],int year, int milage){
Car c;
strcpy(c.model,model);
c.year=year;
c.milage=milage;
return c;
}

void regCars(Car reg[],int *pNrOfCars){
char again[WORDLENGTH] = "yes", model[WORDLENGTH];
int year,milage;
while(strcmp(again,"yes")==0){
printf("Ange model:");
scanf("%s%*c",model);
printf("Ange year:");
scanf("%d%*c",&year);
printf("Ange milage:");
scanf("%d%*c",&milage);
reg[*pNrOfCars]=createCar(model,year,milage);
(*pNrOfCars)++;
printf("Do you rigister another car? enter yes or no:\n");
scanf("%s%*c",again);
}
}

void printRegister(Car reg[], int nrOfCars){
int i=0;
for(i=0;i<nrOfCars;i++)
printf("%d. Car: %s, Year model: %d, Mil: %d\n",i+1,reg[i].model,reg[i].year,reg[i].milage);
}

void increaseMiles (Car reg[], int nrOfCars) {
int carno, newmiles;
  
printf ("Enter car no to increase miles:");
scanf ("%d%*c", &carno);
printf ("Enter new no of miles for this car:");
scanf ("%d%*c", &newmiles);
  
if ((carno > 0) && (carno <= nrOfCars) )
reg[carno-1].milage = newmiles;

}

int main(){
Car carRegister[MAX];
int nrOfCars=0;
char r[100];
printf("register a car\n");
regCars(carRegister,&nrOfCars);
while (1== 1) {
printf("Do you want to print cars details, increase miles or quit? enter print, miles or quit:\n");
scanf("%s",r);
if(strcmp(r,"print")==0)
printRegister(carRegister, nrOfCars);
else if(strcmp(r,"miles")==0)
increaseMiles (carRegister, nrOfCars);
else
break;
}
return 0;
}


Related Solutions

Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i]...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1],...
int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 == -1){ perror("Reading problem "); exit(2); } close(fd2); exit(0); } Could anyone fix the two issues in the while loop, so the ouput can...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
Program #include<iostream> using namespace std; struct bookRecord //structure definition {             //structure members definition char title[30];...
Program #include<iostream> using namespace std; struct bookRecord //structure definition {             //structure members definition char title[30];             char main_author[30];             int year_publish;             float price; }; int main() {             struct bookRecord book1; // declare structure variable;             //Get data from user             cout<<"Enter book title: ";             cin>>book1.title;             cout<<"Enter main author name: ";             cin>>book1.main_author;                     cout<<"Enter year publish: ";             cin>>book1.year_publish;             cout<<"Enter book price RM: ";             cin>>book1.price;             //Print the information the screen output            ...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT