Question

Write a C++ program that will read in image data from the file image.txt and illustrate that image as ASCII art. The Image
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main()
{
   vector<vector<int>> vImage;
   ifstream fin;
   ofstream fout;

   fin.open("image.txt");
   fout.open("output.txt");

   if (!fin.is_open())
       return 0;

   string row;
   //Reading the file into the vector
   while (std::getline(fin,row))
   {
       stringstream ss(row);
       int element;
       vector<int> rowVector;
       while (ss >> element)
       {
           rowVector.push_back(element);
       }
       vImage.push_back(rowVector);
   }

   //Adding the rule to display each pixel
   for (auto rowVector : vImage)
   {
       for (auto element : rowVector)
       {
           if (element >= 0 && element <= 14)
           {
               fout << " ";
               cout << " ";
           }
           else if (element >= 15 && element <= 29)
           {
               fout << "`";
               cout << "`";
           }
           else if (element >= 30 && element <= 44)
           {
               fout << ",";
               cout << ",";
           }
           else if (element >= 45 && element <= 59)
           {
               fout << "~";
               cout << "~";
           }
           else if (element >= 60 && element <= 74)
           {
               fout << "|";
               cout << "|";
           }
           else if (element >= 75 && element <= 89)
           {
               fout << "J";
               cout << "J";
           }
           else if (element >= 90 && element <= 104)
           {
               fout << "x";
               cout << "x";
           }
           else if (element >= 105 && element <= 119)
           {
               fout << "X";
               cout << "X";
           }
           else if (element >= 120 && element <= 134)
           {
               fout << "k";
               cout << "k";
           }
           else if (element >= 135 && element <= 149)
           {
               fout << "0";
               cout << "0";
           }
           else if (element >= 150 && element <= 174)
           {
               fout << "@";
               cout << "@";
           }
           else if (element >= 175 && element <= 255)
           {
               fout << "&";
               cout << "&";
           }
           cout << " ";
           fout << " ";
       }
       fout << "\n";
       cout << "\n";
   }


  
   return 0;
}

OUTPUT:

Microsoft Visual Studio Debug Console & J J @ к х х х JX @ @ & & k 2 2

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that will read in image data from the file "image.txt" and illustrate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This needs to be linux compatible in C++ code. Write a program that reads a file...

    This needs to be linux compatible in C++ code. Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167,...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • c# csci312: character counter - vector design a program that reads in an ascii text file...

    c# csci312: character counter - vector design a program that reads in an ascii text file (provided) ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provide... C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provided) and creates an output file that contains each unique ASCII...

  • written in c++ Write a program that reads a file consisting of students’ test scores in...

    written in c++ Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data:76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149,...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • C++ Write a program based on 8_lb that reads in the integer data from "input.txt" into...

    C++ Write a program based on 8_lb that reads in the integer data from "input.txt" into a vector. Please prompt for the file name and append the "txt". Then create another vector where each element is the original vector times 10. Create a variable total and sum up the numbers from the second vector. The total should be 17510. Then write the data from the original vector and the 10 times vector to a file. See output.txt for the format....

  • C++ Data Structure Write a program to read a list of students from a file and...

    C++ Data Structure Write a program to read a list of students from a file and create a list. The program should use a linked list for implementation. Each node in the linked list should have the student’s name, a pointer to the next student, and a pointer to a linked list of scores. There may be up to four scores for each student.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT