for the colum and row, how to fill the space with examcode automatically, below, I must put one by one..

/*-------------------
Two dimensional array
-------------------*/

#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>


ifstream infile("STA83EXM.txt");


void main()
{


	const int row = 139;
	const int column = 139;
	int matrix[row] [column];

//**************creates Matrix****************
	int num;
		for(int i = 0; i<=row-1; i++)
		{
			for(int j = 0; j<=column-1; j++)
			{
				infile>>num;
				matrix[i][j]=num;
			}
		}
	

//***************Prints out Matrix*********************


	cout<<"\n\n"<<endl;
	cout<<"\t\tColumn:"<<setw(3)<<"1"<<setw(5)<<"2"<<setw(5)<<"3"<<setw(5)<<"4"<<setw(5)<<"5"<<setw(5)<<"6"<<endl;
	cout<<"\t\t--------------------------------------"<<endl;
	cout<<"\t\tRow 1|\t "<<setw(2)<<matrix[0][0]<<setw(5)<<matrix[0][1]<<setw(5)<<matrix[0][2]<<setw(5)<<
		matrix[0][3]<<setw(5)<<matrix[0][4]<<setw(5)<<matrix[0][5]<<endl;
	cout<<"\t\t    2|\t "<<matrix[1][0]<<setw(5)<<matrix[1][1]<<setw(5)<<matrix[1][2]<<setw(5)<<
		matrix[1][3]<<setw(5)<<matrix[1][4]<<setw(5)<<matrix[1][5]<<endl;
	cout<<"\t\t    3|\t "<<matrix[2][0]<<setw(5)<<matrix[2][1]<<setw(5)<<matrix[2][2]<<setw(5)<<
		matrix[2][3]<<setw(5)<<matrix[2][4]<<setw(5)<<matrix[2][5]<<endl<<endl;
	


return;
}

Dani AI

Generated

The goal in this thread is to print a 139x139 data table with the row and column labels 1..139 automatically. is reading the data from a file; asked where the examcode belongs; and correctly noted that it is usually simpler to print labels during output rather than stuffing them into the numeric matrix itself.

A practical approach: keep the matrix as numeric data only (one value per cell), and generate the header row and left-hand labels when you print. That avoids changing the file format or enlarging the stored matrix. Use a loop to print column numbers across the top, then for each row print the row number followed by that row's values formatted with a fixed width (std::setw) so columns line up.

Example printing routine (modern C++):

#include <iostream>
#include <iomanip>
#include <vector>

void print_matrix_with_labels(const std::vector<std::vector<int>>& m) {
    if (m.empty()) return;
    int rows = m.size(), cols = m[0].size();
    const int w = 6;
    std::cout << std::setw(w) << "";
    for (int c = 0; c < cols; ++c) std::cout << std::setw(w) << (c + 1);
    std::cout << '\n';
    for (int r = 0; r < rows; ++r) {
        std::cout << std::setw(3) << (r + 1) << ' ';
        for (int c = 0; c < cols; ++c) std::cout << std::setw(w) << m[r][c];
        std::cout << '\n';
    }
}

Notes and cautions: printing 139 columns to a console will likely wrap; consider outputting to a file (CSV) or printing a subset/paged view. Prefer standard C++ headers and std::vector over compiler-specific VLAs. See std::vector and std::setw for details.

Recommended Answers

All 6 Replies

question is not very clear ... where is the examcode? where r u putting it?

Dear Chandra,

I put it in file ..ifstream infile("STA83EXM.txt");
I want to put in the column and row a number from 1 to 139 which it was an examcode.Then the next things to fill in the others column.

ok. so u'll enter the examcode in say [0][0] and corresponding marks in [0][1] or something like that ...? and what do you mean by "fill the space with examcode automatically" ?

Like this, i have a matrix 139 * 139.

i/j 1 2 3 4 5 6 7 8 9 10
1
2
3
4
5
6
7
8
9

i want to like above..to fill the colum and row with 1 until 139.

I wanna do like this ..but how to put 1 until 139 at the row?like this..

1 2 3 4 5 6 71
2
3
4
5


Can c++ draw a line for table?

/*-------------------
Two dimensional array
-------------------*/

#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>

ifstream infile("STA83EXM.txt");

void main()
{
	const int row = 139;
	const int column = 139;
	int matrix[row] [column];

//**************creates Matrix****************
	int num;
		for(int i = 0; i<=row-1; i++)
		{
			for(int j = 0; j<=column-1; j++)
			{
				infile>>num;
				matrix[i][j]=num;
			}
		}
	
//***************Prints out Matrix*********************

{
	for(int i = 1; i < 140; i++)
	{
		cout << i << endl;
	}


return;
}

}

Do you mean you want the first row and column of numbers in the table will represent the labels of the row and column respectively? If so and the table is table of ints or a table of strings, then the labels can be incorprated into the table by hardcoding it in. If the table is to hold values other than ints or strings, and you want ints or strings as the labels, then you may need to come up with a compound type if you want the labels incorporated into the table or you may want to display the labels using a separate output statements from the output statement of the table contents.

If the table is to contain 139 values per row plus the label, then there needs to be 140 rows and 140 columns.

As a point of clarity which looks easier to understand:
i <= row-1
or
i < row
the statements are equivalent.

To display the array in tabular form you will need a nested array, or similar construct. Your output statement in post #6 just displays a single row of ints.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.