Question

In: Computer Science

How do I write a C++ program to call a frequency table from a csv file,...

How do I write a C++ program to call a frequency table from a csv file, using vector?

Data given is in a csv file. Below is part of the sample data.

Student ID English Math Science
100000100 80 90 90
100000110 70 60 70
100000120 80 100 90
100000130 60 60 60
100000140 90 80 80

Solutions

Expert Solution

#include <iostream>

#include <fstream>

#include <vector>

#include <iterator>

#include <string>

#include <algorithm>

#include <boost/algorithm/string.hpp>

/*

First, we will create a class to read frequency table from a csv file.

*/

class FileReader

{

std::string fileName;

std::string delimeter;

public:

FileReader(std::string filename, std::string delm = ",") :

fileName(filename), delimeter(delm)

{ }

// now we will create a Function to fetch frequency from a CSV File

std::vector<std::vector<std::string> > getData();

};

/*

* now we have to Parse through csv file line by line and returns the frequency data

* in vector of vector of strings.

*/

std::vector<std::vector<std::string> > FileReader::getData()

{

std::ifstream file(fileName);

std::vector<std::vector<std::string> > dataList;

std::string line = "";

// Iterate through each line and split the content using delimeter

while (getline(file, line))

{

std::vector<std::string> vec;

boost::algorithm::split(vec, line, boost::is_any_of(delimeter));

dataList.push_back(vec);

}

// Close the File

file.close();

return dataList;

}

int main()

{

// Creating an object of CSVWriter

FileReader reader("example.csv");

// Get the data from CSV File

std::vector<std::vector<std::string> > dataList = reader.getData();

// Print the content of row by row on screen

for(std::vector<std::string> vec : dataList)

{

for(std::string data : vec)

{

std::cout<<data << " , ";

}

std::cout<<std::endl;

}

return 0;

}


Related Solutions

I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
Write a C program using system call I/O to a) open an existing text file passed...
Write a C program using system call I/O to a) open an existing text file passed to your program as a command line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated content of the file
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
how do i find sample standard deviation from the range frequency table?
how do i find sample standard deviation from the range frequency table?
Write a C program named as listLetterFreq.c that lists the frequency of the letters from the...
Write a C program named as listLetterFreq.c that lists the frequency of the letters from the input via ignoring the case sensitivity. For example, sample outputs could be like below. Please input a string: This is a list of courses. CSC 1010 - COMPUTERS & APPLICATION Here is the letter frequency: Letter a or A appears 3 times Letter b or B appears 0 times Letter c or C appears 5 times Letter d or D appears 0 times Letter...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT