Question

Write a C++ program that creates a ragged table (i.e. a table with rows of different...

Write a C++ program that creates a ragged table (i.e. a table with rows of different length) using a vector of vector of integers. Your program should prompt the user for a file name, open the file, read the content of the file and store it in a vector or vectors, and finally print the content of the vector of vectors.

Sample Input:

Enter the file name: data.dat

A sample file might contain:

6
5 2 4 5 3 2
3 1 2 6
0
4 9 3 1 7
1 5
2 3 6

The first number in the file is a whole integer (i.e. 0, 1, 2, …) representing the number of rows of values on the remaining lines in the file. For example, the first 6 in the example shown above indicates there are 6 lines of numbers after the first line. Each row of data (second line in the sample shown above) begins with a whole number representing the number of items on the line. For example, first row of data begins with a 5 indicating there are 5 data values on the line (i.e. 2 4 5 3 2). After you have read and stored the values in a vector of vectors, write a pair of nested loops to print the vector of vectors.

When you print the vector of vector for the example given above you should display:

2 4 5 3 2
1 2 6

9 3 1 7
5
3 6

Notice a zero length row is displayed as a blank line (i.e. endl)

Make sure your solution can handle a file containing the following:

3
0
0
0

as well as a file containing:

0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.
data.dat
----
6
5 2 4 5 3 2
3 1 2 6
0
4 9 3 1 7
1 5
2 3 6


main.cpp
-----
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main(){
   string filename;
   vector< vector <int> > table;
   int numRows, numValues, num;
  

   cout << "Enter the file name: ";
   cin >> filename;
  
   ifstream infile(filename.c_str());
   if(infile.fail()){
       cout << "ERROR: could not read input file " << filename << endl;
       return 0;
   }
  
   infile >> numRows;
   for(int i = 1; i <= numRows; i++){
       vector<int> v;
       infile >> numValues;
       for(int j = 1; j <= numValues; j++){
           infile >> num;
           v.push_back(num);
       }
       table.push_back(v);
   }
  
   infile.close();
  
   //print the data
   for(int i = 0; i < table.size(); i++){
       for(int j = 0; j < table[i].size(); j++)
           cout << table[i][j] << " ";
       cout << endl;
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that creates a ragged table (i.e. a table with rows of different...
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
  • Write a C++ program that will read in image data from the file "image.txt" and illustrate...

    Write a C++ program that will read in image data from the file "image.txt" and illustrate that image as ASCII art. The Image file will contain several lines, representing horizontal rows in the image, and each line will have several integers in the range 0-255. representing povels, separated by spaces. You should read this image data into a vector vector in Once you've read in the phel data, go through the image and print out each piel in the image...

  • DESCRIPTION Complete the program using Java to read in values from a text file. The values...

    DESCRIPTION Complete the program using Java to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows)...

  • Please write a C++ Program for the following problem >> Vector: Calculation the sum of each...

    Please write a C++ Program for the following problem >> Vector: Calculation the sum of each two adjacent Numbers.... >>【Description】 Read integer numbers from keyboard and store them into a vector. Calculation the sum of each two adjacent Numbers, store the result into another vector. Then output the result inversely. For example, if the input is 1 2 3 4 5 then the output is 9 7 5 3 【 Input】 There are 5 test cases. For each case, there...

  • Java Objective: The goal of this assignment is to practice 2-dimensional ragged arrays. Background: Within a...

    Java Objective: The goal of this assignment is to practice 2-dimensional ragged arrays. Background: Within a healthy, balanced diet, a grownup needs 2,250 calories a day You will write a program to track calorie intake of a person. Assignment: Calorie intake data from a person is provided in a text file named input.txt. There are arbitrary number of double values on each line, separated by spaces. The numbers represent the number of calories consumed for meals and/or snacks on a...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp...

    Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp takes a single argument, which is the path to a file containing a square (k × k) matrix M and a non-negative exponent n. It computes M and prints the result Note that the size of the matrix is not known statically. You ust use malloc to allocate space for the matrix once you obtain its size from the input file. Tocompute M". it...

  • Using Java. Write a program that creates a “triangular” two-dimensional array A of 10 rows. The...

    Using Java. Write a program that creates a “triangular” two-dimensional array A of 10 rows. The first row has length 1, the second row has length 2, the third row has length 3, and so on. Then initialize the array using nested for loops so that the value of A[i][j] is i+j. Finally, print out the array in a nice triangular form.

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

  • Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e....

    Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e. a two-dimensional array), number of rows and number of columns as input and prints the matrix after rotating it 90 degrees clockwise. Example: void rotate-matrix (int* matrix, int row, int column) Inputs: row 4, column-6 and the pointer to the matrix below: 2 3 6 5 8 0 Output:

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