Hi i need to write program that sort number in ascending order with 2 D array from inputfie to an outpute file i m real behind i tried this code but it does show any thing this assignment is due tomorrow please help
//This program is sorting intergers from inpute file in ascending order
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
int main ()
{
//Declare the variables
ifstream infile;
ofstream outfile;
int const Row = 10 ;
int const Column = 10;
Does it hang? Does it crash? Do you get any error messages?
It looks to me as though your for loop at line 41 is incorrect. You initialise counter to 1, and then you subtract 1 from it each time through the loop. That means that it goes to 0 the next time, then -1 and so on.
This means that (counter < temp) will always be true, until counter reaches the largest possible negative number an int can hold, at which point it will become a huge positive number (I think...) and cause the loop to exit.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
usingnamespace std;
int main()
{
constint ROWS(10), COLS(10);
int myarray[ROWS][COLS];
ifstream ifs;
ifs.open("P6_F12_data.txt");
if (ifs.fail())
cout << "failed to read input file" << endl;
else
{
// read data into array:
int row(0);
string line;
while (getline(ifs, line) && row < ROWS)
{
istringstream iss(line);
int n, col(0);
while (iss >> n && col < COLS)
myarray[row][col++] = n;
++row;
}
ifs.seekg(0, ios::beg);
ifs.clear();
ifs.close();
// do sort:
int* beg = reinterpret_cast<int*>(myarray);
int* end = beg + ROWS * COLS;
sort(beg, end);
// write sorted data out to new file:
ofstream ofs;
ofs.open("out.txt");
if (ofs.fail())
cout << "failed to open out file" << endl;
else
{
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
ofs << myarray[r][c];
ofs << endl;
}
ofs.flush();
ofs.close();
}
}
return 0;
}