The library optimezedr should determine if a collection of books is in ascending or desending order.Your solution must read books from a text file and produce 2 additional text files one in ascending order and one in Descending order. then your solution will indicate if your original text file in in ascending or descending order or not sorted .You can only use the following c++ libraries:iostrem,fstream and string. Could someone please help me with this thank you.

Dani AI

Generated

Following 's prompt about where the OP was stuck, and building on 's pairwise idea, the snippet below gives a complete, compile-ready solution for that uses only the allowed headers (<iostream>, <fstream>, <string>). It reads the input file (default "books.txt" or a filename passed on the command line), determines whether the original list is non-decreasing (ascending), non-increasing (descending), or not sorted, and writes two output files ("ascending.txt" and "descending.txt"). The comparison is trimmed and made case-insensitive (ASCII only) so human-friendly ordering is used; the original lines are preserved in the outputs.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string trim(const string& s) {
    size_t a = s.find_first_not_of(" \t\r\n");
    if (a == string::npos) return "";
    size_t b = s.find_last_not_of(" \t\r\n");
    return s.substr(a, b - a + 1);
}

int main(int argc, char* argv[]) {
    const char* inName = (argc > 1) ? argv[1] : "books.txt";
    const char* ascName = "ascending.txt";
    const char* descName = "descending.txt";

    ifstream in(inName);
    if (!in) {
        cerr << "Cannot open input file: " << inName << '\n';
        return 1;
    }

    size_t n = 0;
    string line;
    while (getline(in, line)) ++n;
    in.clear();
    in.seekg(0);

    string* raw = new string[n];
    string* comp = new string[n];

    size_t idx = 0;
    while (getline(in, line) && idx < n) {
        raw[idx] = line;
        comp[idx] = trim(line);
        for (size_t k = 0; k < comp[idx].size(); ++k) {
            char c = comp[idx][k];
            if (c >= 'A' && c <= 'Z') comp[idx][k] = char(c - 'A' + 'a');
        }
        ++idx;
    }
    in.close();

    if (n == 0) {
        cout << "Input file is empty\n";
        delete[] raw; delete[] comp;
        return 0;
    }

    bool asc = true, desc = true;
    for (size_t i = 1; i < n; ++i) {
        if (comp[i] < comp[i-1]) asc = false;
        if (comp[i] > comp[i-1]) desc = false;
    }

    string* sorted = new string[n];
    string* compSorted = new string[n];
    for (size_t i = 0; i < n; ++i) { sorted[i] = raw[i]; compSorted[i] = comp[i]; }

    for (size_t i = 1; i < n; ++i) {
        string key = sorted[i];
        string keyComp = compSorted[i];
        size_t j = i;
        while (j > 0 && compSorted[j-1] > keyComp) {
            sorted[j] = sorted[j-1];
            compSorted[j] = compSorted[j-1];
            --j;
        }
        sorted[j] = key;
        compSorted[j] = keyComp;
    }

    ofstream outAsc(ascName);
    for (size_t i = 0; i < n; ++i) outAsc << sorted[i] << '\n';
    outAsc.close();

    ofstream outDesc(descName);
    for (size_t i = n; i > 0; ) { --i; outDesc << sorted[i] << '\n'; }
    outDesc.close();

    if (asc && !desc) cout << inName << " is sorted ascending\n";
    else if (!asc && desc) cout << inName << " is sorted descending\n";
    else if (asc && desc) cout << inName << " is constant (all entries equal)\n";
    else cout << inName << " is NOT sorted\n";

    delete[] raw; delete[] comp; delete[] sorted; delete[] compSorted;
    return 0;
}

Notes and troubleshooting:

  • The program treats "ascending" as non-decreasing and "descending" as non-increasing; equal neighbors count as allowed. If strict ordering is required, change the checks to strict comparisons.
  • Trimming handles trailing spaces and CR/LF; the case-insensitive step uses a simple ASCII-only lowercase conversion (diacritics are not handled).
  • Insertion sort is used for simplicity (O(n^2)). That is fine for homework-sized files; for large files, an external merge or (if allowed) using std::vector + std::sort is recommended.
  • Filenames are near the top of main; change them if different output names are required.

Recommended Answers

All 2 Replies

Perhaps if you tell us where you are stuck we can offer some suggestions. What have you done so far?

commented: nothing don't know how to was hoping someone would help with the whole code +0

Let me suggest a dumb solution. Read from the original list. For each row or record, compare the title, name or whatever, to that of the previous record. If Current > Previous then increment a counter I'll call Ascending else increment a counter I'll call Descending. When your done, if the value of either Ascending or Descending is very high, that tells you the order of the original list. If both counters are fairly high, the original list is not sorted. There are probably much more elegant ways to do this, so I'll call this a quick and dirty solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.