How to arrange the number properly?

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

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

ifstream infile("STA83EXM.txt");

void main()
{
	const int row = 140;
	const int column = 140;
	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 << "i,j"<<"\t";
	for(int i = 1; i < 140; i++)
	{
	cout<<i<<"\t";
	}
    
	for(int j= 1; j < 140; j++)
	{
		cout<<j<< "\n";
	}
return;
}

}

Dani AI

Generated

To : if the intent is to display the 140x140 integers from STA83EXM.txt as a readable grid, the printing logic needs changing. Common mistakes in this thread: printing headers in a separate loop and not printing the matrix elements inside a nested row/column loop, and off-by-one indexing (C++ arrays use 0..139 for a 140-size array). 's comments about fractional digits are not relevant here; 's suggestion about sorting only applies if the goal is to reorder values — for that use std::sort, not bubble sort.

A straightforward, modern approach — check the file opens, read into a 2D container, verify enough numbers were read, then print row headers and each row with a newline at the end:

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

int main() {
    const size_t R = 140, C = 140;
    std::ifstream infile("STA83EXM.txt");
    if (!infile) {
        std::cerr << "Cannot open STA83EXM.txt\n";
        return 1;
    }

    std::vector<std::vector<int>> m(R, std::vector<int>(C));
    for (size_t i = 0; i < R; ++i) {
        for (size_t j = 0; j < C; ++j) {
            if (!(infile >> m[i][j])) {
                std::cerr << "Insufficient or bad data at [" << i << "][" << j << "]\n";
                return 1;
            }
        }
    }

    std::cout << "    ";
    for (size_t col = 0; col < C; ++col) std::cout << std::setw(4) << col << ' ';
    std::cout << '\n';

    for (size_t row = 0; row < R; ++row) {
        std::cout << std::setw(3) << row << ' ';
        for (size_t col = 0; col < C; ++col)
            std::cout << std::setw(4) << m[row][col] << ' ';
        std::cout << '\n';
    }
    return 0;
}

If the requirement is to sort values instead of printing them in matrix order, flatten the 2D array into a single vector and call std::sort(flat.begin(), flat.end()). Practical tips: print a small sample first to confirm parsing, count numbers read versus expected, redirect output to a file for very large grids, and prefer standard headers (<iostream>, <fstream>) and int main() with a return value.

Recommended Answers

All 6 Replies

How to arrange the number properly?

One digit after another. If there is a portion that's below 1, add a '.' and that portion. If negative, '-' in front of all the digits.

I dont understand...what mean by below 1?

0.24, 0.5, 0.997

the number is 1 until 139

no number below 1 and negative.

How to arrange the number properly?

Congrats: Taking this and other threads into account, you've just scored an A+ in "The art of asking vague questions"!

Perhaps you mean sorting a list a numbers? If so:bubblesort

the number is 1 until 139

no number below 1 and negative.

OK, then I update my quote with this new information:

One digit after another.

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.