Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include<limits>
- using namespace std;
- struct Book {
- char title[50];
- char author[50];
- int isbn;
- int publishingYear;
- };
- int main() {
- // Create a program which sorts an array of books based on publishing year, descending
- struct Book books[3];
- int n = 2;
- for(int i = 0; i < n; i++) {
- cout << "Enter title: ";
- cin.getline(books[i].title, 50);
- cout << "\nEnter author: ";
- cin.getline(books[i].author, 50);
- cout << "\nEnter publishing year: ";
- cin >> books[i].publishingYear;
- cin.ignore(numeric_limits<streamsize>::max(),'\n');
- cout << "\nEnter isbn: ";
- cin >> books[i].isbn;
- cin.ignore(numeric_limits<streamsize>::max(),'\n');
- }
- cout << "===============================\n";
- cout << "Books before sorting: \n\n";
- cout << "Author\tTitle\tISBN\t#Publishing Year\n";
- for(int i = 0; i < n;i++) {
- cout <<books[i].author <<"\t" << books[i].title << "\t" << books[i].isbn << "\t" << books[i].publishingYear;
- cout << endl;
- }
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < n-1;j++) {
- if(books[j].publishingYear < books[j+1].publishingYear) {
- struct Book aux = books[j];
- books[j] = books[j+1];
- books[j+1] = aux;
- }
- }
- }
- cout << "===============================\n\n";
- cout << "Books after sorting: \n\n";
- cout << "Author\tTitle\tISBN\t#Publishing Year\n";
- for(int i = 0; i < n;i++) {
- cout <<books[i].author <<"\t" << books[i].title << "\t" << books[i].isbn << "\t" << books[i].publishingYear;
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement