I am sorry for the confusing but here is what i would to be done in C++.
What to expect as output:
1- Report how many lines in the file.
2- How many columns in each row.
3- How many different classes in the file (last column)
4- You should have at least one function in your solution.
5.1 | 0.222222 | 3.5 | 0.625 | 1.4 | 0.067797 | 0.2 | 0.041667 | setosa |
4.9 | 0.166667 | 3 | 0.416667 | 1.4 | 0.067797 | 0.2 | 0.041667 | setosa |
4.7 | 0.111111 | 3.2 | 0.5 | 1.3 | 0.050847 | 0.2 | 0.041667 | setosa |
4.6 | 0.083333 | 3.1 | 0.458333 | 1.5 | 0.084746 | 0.2 | 0.041667 | setosa |
5 | 0.194444 | 3.6 | 0.666667 | 1.4 | 0.067797 | 0.2 | 0.041667 | setosa |
5.4 | 0.305556 | 3.9 | 0.791667 | 1.7 | 0.118644 | 0.4 | 0.125 | setosa |
6.4 | 0.583333 | 2.7 | 0.291667 | 5.3 | 0.728814 | 1.9 | 0.75 | virginica |
6.8 | 0.694444 | 3 | 0.416667 | 5.5 | 0.762712 | 2.1 | 0.833333 | virginica |
5.7 | 0.388889 | 2.5 | 0.208333 | 5 | 0.677966 | 2 | 0.791667 | virginica |
5.8 | 0.416667 | 2.8 | 0.333333 | 5.1 | 0.694915 | 2.4 | 0.958333 | virginica |
6.4 | 0.583333 | 3.2 | 0.5 | 5.3 | 0.728814 | 2.3 | 0.916667 | virginica |
6.5 | 0.611111 | 3 | 0.416667 | 5.5 | 0.762712 | 1.8 | 0.708333 | virginica |
7.7 | 0.944444 | 3.8 | 0.75 | 6.7 | 0.966102 | 2.2 | 0.875 | virginica |
4.6 | 0.083333 | 3.4 | 0.583333 | 1.4 | 0.067797 | 0.3 | 0.083333 | setosa |
5 | 0.194444 | 3.4 | 0.583333 | 1.5 | 0.084746 | 0.2 | 0.041667 | setosa |
4.4 | 0.027778 | 2.9 | 0.375 | 1.4 | 0.067797 | 0.2 | 0.041667 | setosa |
4.9 | 0.166667 | 3.1 | 0.458333 | 1.5 | 0.084746 | 0.1 | 0.01 | setosa |
6.4 | 0.583333 | 2.7 | 0.291667 | 5.3 | 0.728814 | 1.9 | 0.75 | virginica |
6.8 | 0.694444 | 3 | 0.416667 | 5.5 | 0.762712 | 2.1 | 0.833333 | virginica |
5.7 | 0.388889 | 2.5 | 0.208333 | 5 | 0.677966 | 2 | 0.791667 | virginica |
5.8 | 0.416667 | 2.8 | 0.333333 | 5.1 | 0.694915 | 2.4 | 0.958333 | virginica |
Here is the file.
The program is below. Save it file in count_lines.cpp
#include <iostream>
#include <fstream>
using namespace std;
// returns number of words in a str
unsigned count_words(char *str) {
int state = 0;
unsigned wc = 0; // word count
// Scan all characters
while (*str)
{
// If next character is a separator, set the flag state
if (*str == ' ' || *str == '\n' || *str == '\t')
state = 0;
// If next character is not a word separator increment word
count
else if (state == 0){
state = 1;
++wc;
}
// check next character
++str;
}
return wc;
}
void find_column() {
ifstream fin;
fin.open("file.txt");
char str[80];
int count=0;
while(!fin.eof())
{
fin.getline(str,80);
break;
}
fin.close();
cout << "Column count "<<
count_words(str);
}
void count_lines(){
ifstream fin;
fin.open("file.txt");
char str[100];
int count=0;
while(!fin.eof())
{
fin.getline(str,100);
count++;
}
cout<<"Number of lines in the file is:
"<<count<<endl;
fin.close();
}
int main() {
count_lines();
find_column();
return 0;
}
Output :
Number of lines in the file is 21
Column count
9
Get Answers For Free
Most questions answered within 1 hours.