Question

In: Computer Science

exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6...

exampleInput.txt

1 2 3

0 2 3 4

0 1 3 5

0 1 2 6

1 5 6 8

2 4 6 7

3 4 5 9 10

5 8 9

4 7 9

6 7 8

6

How can I detect when 'cin' starts reading from a new line. The amount of numbers in each row is unknown. I need them in type 'int' to use the data.

Solutions

Expert Solution

Here is how to count the number of integers in each line and print

=====================================================================

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;

int main(int argc, char* argv[]){
  
   if(argc!=2){
       cout<<"No command line arguments provided. Program will terminate ...";
       return 1;
   }
  
   ifstream infile(argv[1]);
   if(!infile.is_open() || infile.bad()){
       cout<<"Error: could not open file: "<<argv[1]<<endl;
       cout<<"Program will terminate now ...\n";
       return 1;
   }
   string line;
   int lineNum = 1;
   while(getline(infile,line,'\n')){
       stringstream ss(line);
       cout<<"Line >\""<<line<<lineNum++;
       int numCount = 0;
       int num;
       while(ss>>num){
           numCount++;
       }
       cout<<"\" contains "<<numCount<<" number(s).\n";
      
   }
   infile.close();
  


   return 0;
}

==================================================================


Related Solutions

x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6 13 7 33 8 42 9 66 10 77 11 105 12 103 13 110 14 105 15 84 16 70 17 51 18 40 19 27 20 27 21 15 22 5 23 7 24 2 25 2 26 1 27 0 28 0 29 0 30 0 (7) On the Histogram worksheet, calculate all frequencies of the distribution using the table shown....
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1...
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1 0 2 11 5 11 27 4 0 6 Create Standard Deviation Chart (Normal Distribution Curve)
ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5...
ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5 8 7 6 5 7 7 6 7 8 8 8 9 7 8 10 12 11 Test the significance of the correlation coefficient. Then use math test scores (X) to predict physics test scores (Y).  Do the following: Create a scatterplot of X and Y. Write the regression equation and interpret the regression coefficients (i.e., intercept and slope). Predict the physics score for each....
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Find mean, median, mode, variance, standard deviation, coefficient of variation, range, 70th percentile, 3rdquartile of the data and skewness and define what each of these statistics measure. For example, mean is a measure of the central tendency, what about the rest? Use Chebyshev’s rule to find...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Split the dataset in two equal parts. You have 30 datavalues. If you split the data in two equal parts each part will contain 15 data values.  Call the first part Y and second part X.Draw scatter plot of the 2 datasets, X being on the horizontal...
0 0 2 0 5 3 1 12 0 0 0 1 6 0 1 1...
0 0 2 0 5 3 1 12 0 0 0 1 6 0 1 1 2 8 1 3 1 6 2 4 0 16 17 0 8 0 3 0 0 1 2 5 2 0 2 1 5 0 7 0 1 0 0 1 0 0 3 1 9 4 1 3 0 1 1 1 0 7 1 9 2 0 1 1 1 1 7 2 7 1 2 =============================== (a) What type of...
tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3...
tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3 5 4 7 9 7 0 0 4 5 6 9 9 8 1 3 5 6 8 9 9 0 1 2 3 5 9 The table represent a random sample of 31 test scores taken from a large lecture class. Find the following [round to 2 decimal points X. XX] a) [2 pts] Find the 5 number summary [L, Q1, Q2, Q3,...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5 3 0 2 5 4 1 6 9 5 3 1 2 10 6 1 1 2 1 19 6 6 6 7 0 4 1 1 1 0 1 9 2 2 2 1 16 10 10 5 2 3 1 4 4 4 3 6 2 8 5 2 7 1 6 4 0 3 1 1 1 Background: A group of...
X 1 3 5 3 4 4 Y 2 5 4 3 4 6 A: Plot...
X 1 3 5 3 4 4 Y 2 5 4 3 4 6 A: Plot the date B: find the line of best fit C: determine ŷ AT x=3 D: Find r and r^2 E: explain r and r^2
Find the distances: A) Between ?1=〈2+2?,−1+?,−3?〉and ?2=〈4,−5−3?,1+4?〉 . B) Between the planes 2?−?+5?=0 and 2?−?+5?=5 ....
Find the distances: A) Between ?1=〈2+2?,−1+?,−3?〉and ?2=〈4,−5−3?,1+4?〉 . B) Between the planes 2?−?+5?=0 and 2?−?+5?=5 . C) From the point (1,2,3) to the line ?=〈−?,4−?,1+4?〉 .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT