Question

In: Computer Science

(MUST BE DONE IN C (NOT C++)) In this task, you will create a structure with...

(MUST BE DONE IN C (NOT C++))

In this task, you will create a structure with arrays. You will have to create your own structure. However, make sure to meet these guidelines:

- Give the structure whichever name you want.

- It must have at least 3 members.

- Two of the members must be arrays.

- Your members should be of at least two different data-types. In other words, your members cannot be integers only (or floats, or doubles…).

- Your structure cannot have the members as make, mileage, model or price.

Once you define your structure, go ahead and declare one structure of this type inside main. Then, initialize all its members by asking the user (there is no need of functions in this task). When done scanning the information from the user, print it at the end of main.

Solutions

Expert Solution

Code:

#include <stdio.h>
#include <conio.h>

//Defining a structure as per the given requirement
struct Book {
int bookId;
char bookName[20];
char bookAuthor[20];
int yearPublished;
};

void main() {
   int i, j, k, l;
   struct Book b1; //Declaring the structure
   clrscr();

   //Taking user inputs for the structure data members.
   printf("Enter the book id: ");
   scanf("%d", &b1.bookId);

   printf("\nEnter the book name: ");
   for(i = 0; i < 20; i++){
       scanf("%c", &b1.bookName[i]);

       if(i > 0 && b1.bookName[i] == '\n'){
           break;
       }
   }

   printf("\nEnter the author's name: ");
   for(j = 0; j < 20; j++){
       scanf("%c", &b1.bookAuthor[j]);
       if(j > 0 && b1.bookAuthor[j] == '\n'){
           break;

      }
   }

   printf("\nEnter the publication year: ");
   scanf("%d", &b1.yearPublished);


   //Printing the data members
   printf("\nBook id is: %d", b1.bookId);
   printf("\nBook name is: ");
   for(k = 1; k < 13 ; k++){

       printf("%c", b1.bookName[k]);
   }
   printf("\nBook author's name is: ");
   for(l = 0; l < 11; l++){
       printf("%c", b1.bookAuthor[l]);
   }
   printf("\nBook publication year is: %d", b1.yearPublished);
   getch();
}

Code screenshot:

Output:

Note: C has a very bad garbage collection mechanism. So, printing an unassigned index might give you a garbage value. So, I am printing only the assigned values array.

If you find any trouble in understanding, feel free to ask.


Related Solutions

MUST BE DONE IN C (NOT C++)) Here, we will create a structure that resembles a...
MUST BE DONE IN C (NOT C++)) Here, we will create a structure that resembles a university’s profile (you can pick any university name, just so long as the program runs properly). The structure must contain 5 members: - One member for number of undergraduate students - One member for number of graduate students - One member for number of classrooms - One member for the name of the university (an array) - One member for the term (fall, summer...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure you understood the concept of “the dot” in programming. Go ahead and declare an array of characters with a default length (whichever length you want, it's fine). Next, you want to ask the user for a phrase and store the characters in your array, but before you do that, ask the user for an estimated length of the array. If the length given is...
MUST BE DONE IN C (NOT C++) In this task, using a function, we will add...
MUST BE DONE IN C (NOT C++) In this task, using a function, we will add a range of values of an array. The range will be determined by the user. For example, if I have the following array … 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells me to add from the 3rd element to the 6th element, my program would add the values 8.9, 4.6, 7.8 and 995.1. To do so, please follow...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases in which the initial unit is Fahrenheit or something not recognizable. Your program should incorporate Fahrenheit to Celsius, Fahrenheit to Kelvin and unknown initial units (display an error message for this last one). You must use functions to calculate Fahrenheit degrees.
(MUST BE DONE IN C (NOT C++)) Instead of using two different variables, define a structure...
(MUST BE DONE IN C (NOT C++)) Instead of using two different variables, define a structure with two members; one representing the feet and the other one representing the inches. You will also use three functions; one to initialize a structure, another one to check the validity of its values and one last one to print them out. First, go ahead and define your structure. Next, declare a structure of this type inside main. Then, call your first function (this...
Calculations must be done in Excel – You must create your own spreadsheet do not copy...
Calculations must be done in Excel – You must create your own spreadsheet do not copy and paste someone else’s. Polycorp Limited Steel Division is considering a proposal to purchase a new machine to manufacture a new product for a potential three year contract. The new machine will cost $1.9 million. The machine has an estimated life of three years for accounting and taxation purposes. Installation will cost a further $120,000. The contract will not continue beyond three years and...
Project 2 Calculations must be done in Excel – You must create your own spreadsheet (do...
Project 2 Calculations must be done in Excel – You must create your own spreadsheet (do not copy and paste someone else’s). This question should be done using Method 1 as outlined in lecture 6 (i.e. Tax Effects, then Cash Flows then NPV) As the financial advisor to All Star Manufacturing you are evaluating the following new investment in a manufacturing project: - The project has a useful life of 12 years. Land costs $6m and is estimated to have...
MUST BE DONE IN C (NOT C++) In this program we will calculate the average of...
MUST BE DONE IN C (NOT C++) In this program we will calculate the average of x students’ grades (grades will be stored in an array). To do so, please follow these guidelines: - Your program should ask the user for the number of students that are in the class. This number should help you declare your array. - Use the function seen in class to scan the grades of the array. In other words, we will populate the array...
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values...
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything
(MUST BE DONE IN C (NOT C++)) For this program, remember to use feet and inches....
(MUST BE DONE IN C (NOT C++)) For this program, remember to use feet and inches. First, ask the user for the name of students they have in their class. Then, using a loop, you will ask for each student’s height. However, you will have to use two separate variables, one for feet and one for inches. Then, you will have to call two functions. The first function will check if the values entered are valid (check if number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT