So this program should compute a row of result matrix. But my code doesn't do what it supposed to.
#include <iostream>
#include <fstream>
#include <pthread.h>
#include <ctime>
#include <regex>
using namespace std;
//matrix A and B, result will be store as matrix C
int A[10000][10000], B[10000][10000], C[10000][10000];
int rowFirst, columnFirst, rowSecond, columnSecond;
int numThread;
struct threadLine {
int tid;
int lineNum;
int rowFirst;
int rowSecond;
int columnFirst;
int columnSecond;
};
//function to multiply matrix A and B
void* matrix (void* lineNumber)
{
int i, j, k;
int line = (long)lineNumber;
// Multiplying matrix A and B and store it in matrix C.
// per row
//for(i = 0; i < line; ++i)
//{
for(j = 0; j < columnSecond; ++j)
{
C[i][j] = 0;
for(k = 0; k < columnFirst; ++k)
{
C[i][j] += A[line][k] * B[k][j];
}
//C[i][j] = temp;
}
//}
return 0;
}
int main()
{
//read the file called matrix
ifstream read("matrix.txt");
// if it can read
if (read)
{
read >> rowFirst >> columnFirst; //read the first matrix
//thread ids
pthread_t tid[rowFirst];
struct threadLine tl[rowFirst];
for (int i = 0; i < rowFirst; i++)
{
for(int j = 0; j < columnFirst; j++)
{
read >>A[i][j]; //store it in array A
}
}
read >> rowSecond >> columnSecond; //read the second matrix
if (columnFirst != rowSecond) //column of first matrix must be the same as the second one
{
cout << "column of first matrix must have the same value as row of second matrix" << endl;
return -1;
}
else
{
for (int i = 0; i < rowSecond; i++)
{
for(int j = 0; j < columnSecond; j++)
{
read >>B[i][j]; //store it in array B
}
}
//print input data
cout << "Matrix A: "; //print array A
for (int i = 0; i < rowFirst; i++)
{
for(int j = 0; j < columnFirst; j++)
{
cout << A[i][j] << ' ';
}
cout << endl;
}
cout << "Matrix B: "; //print array B
for (int i = 0; i < rowSecond; i++)
{
for(int j = 0; j < columnSecond; j++)
{
cout << B[i][j] << ' ';
}
cout << endl;
}
cout << endl;
int timeStart = clock(); //start time counting
//loop for creating threads based on number of row (calculate per row)
for (int x = 0; x < rowFirst; x++)
{
int idSuccess = pthread_create(&tid[x], NULL, matrix, (void *) x);
cout << "Created worker thread " << tid[x] << " for row " << x << endl;
//check to see if thread is created sucessfully
if (idSuccess)
{
cout << "Thread creation failed : " << strerror(idSuccess);
return idSuccess;
}
}
cout << endl;
//wait for every threads complete
for (int x = 0; x < rowFirst; x++)
{
pthread_join(tid[x], NULL);
}
int timeStop = clock(); //stop time counting
cout << "Matrix C = A x B: "; //display result
for (int i = 0; i < rowFirst; i++)
{
for(int j = 0; j < columnSecond; j++)
{
cout << C[i][j] << " ";
if(j == columnSecond - 1)
{
cout << endl;
}
}
}
cout << endl;
//print out time
double timeVal = (timeStop -
timeStart)/double(CLOCKS_PER_SEC)*1000;
cout << "Total execution time using " << numThread << " threads is " << timeVal << " ms."<< endl;
}
}
else if(!read)
{
cout << "Unable to read file" << endl;
}
}
I was expecting the output would be something like this:
Matrix C = A x B: 3 17 10 11
2 -10 -6 -7
16 11 5 -5
4 52 30 27
Instead I got this:
Matrix C = A x B: 4 52 30 27
0 0 0 0
0 0 0 0
0 0 0 0
It only computes the first row. I'm not sure what's the cause of this.
Related
I am trying to create a program that swaps the row that contains the min number with the row that contains the max number in a n x m twodimensional array (c++)
#include <iostream>
using namespace std;
int main()
{
int i, j, n, m, imin, imax, jnm, jnv;
cin >> n >> m;
int k[n][m];
int max = 0;
int min = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << "a[" << i << "][" << j << "]" << endl;
cin >> k[i][j];
}
}
cout << endl
<< endl;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << k[i][j];
}
cout << endl;
}
cout << endl
<< endl;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (k[i][j] > max) {
max = k[i][j];
imax = i;
}
}
cout << endl;
}
min = max;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (k[i][j] < min) {
min = k[i][j];
imin = i;
}
}
cout << endl;
}
cout << endl
<< endl;
if (imax == imin) {
cout << endl
<< "Min & Max are in the same row!" << endl;
}
else {
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (i == imax) {
k[i][j] = k[imin][j];
}
else if (i == imin) {
k[i][j] = k[imax][j];
}
cout << k[i][j];
}
cout << endl;
}
}
return 0;
}
I know the code isn't the cleanest and most professionally written, and that isn't important, as I'm currently preparing for a coding competition where the only thing that matters is functionality of the program.
When I execute this program, it usually swaps one row but the other is still the same.
You could use function swapif you want to swap. At the moment you assignments are wrong.
So, simply write:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int i, j, n, m, imin=0, imax=0;
cin >> n >> m;
vector<vector<int>> k(n, vector<int>(m, 0));
int max = 0;
int min = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << "a[" << i << "][" << j << "]" << endl;
cin >> k[i][j];
}
}
cout << endl
<< endl;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << k[i][j] << ' ';
}
cout << endl;
}
cout << endl
<< endl;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (k[i][j] > max) {
max = k[i][j];
imax = i;
}
}
cout << endl;
}
min = max;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (k[i][j] < min) {
min = k[i][j];
imin = i;
}
}
cout << endl;
}
cout << endl
<< endl;
if (imax == imin) {
cout << endl
<< "Min & Max are in the same row!" << endl;
}
else {
for (j = 0; j < m; j++)
swap(k[imin][j], k[imax][j]);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << k[i][j] << ' ';
}
cout << endl;
}
}
return 0;
}
And sorry, I cannot write int k[n][m]; because this is not C++ and my compiler cannot compile it. But vector can be used in the same way.
If you are not allowed to use std::swapyou can use instead:
for (j = 0; j < m; j++) {
int tmp = k[imin][j];
k[imin][j] = k[imax][j];
k[imax][j] = tmp;
}
For the competition you could also get the min and max values already during input and write:
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
int main()
{
// Read matrix size
size_t rows{}, columns{};
if (std::cin >> rows >> columns) {
// Here we will store our matrix
std::vector<std::vector<int>> matrix(rows, std::vector<int>(columns, 0));
// Initilize min max values
int maxElement = std::numeric_limits<int>::min();
int minElement = std::numeric_limits<int>::max();
size_t indexMaxRow{}, indexMinRow{};
// Enter values in matrix
for (size_t row{}; row < rows; ++row) {
for (size_t column{}; column < columns; ++column) {
std::cout << "array[" << row << "][" << column << "]\n";
if (std::cin >> matrix[row][column]) {
// Already during input find the min and max values
if (matrix[row][column] > maxElement) {
maxElement = matrix[row][column];
indexMaxRow = row;
}
if (matrix[row][column] < minElement) {
minElement = matrix[row][column];
indexMinRow = row;
}
}
}
}
// Show original matrix
std::cout << "\n\n\nYou entered:\n\n";
for (const auto& row : matrix) {
for (const auto& col : row) std::cout << col << ' ';
std::cout << '\n';
}
// Swap
std::swap(matrix[indexMaxRow], matrix[indexMinRow]);
// Show swapped matrix
std::cout << "\n\n\nSwapped:\n\n";
for (const auto& row : matrix) {
for (const auto& col : row) std::cout << col << ' ';
std::cout << '\n';
}
}
else std::cerr << "\nError while reading size\n";
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have written next code but 3 functions must be replaced by 1 and I don't know how to.
The program creates 3 arrays but only 1 function must calculate negative numbers of each column and find the max element in each column. Here's the code:
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int n = 0;
const int m = 3, k = 3, b = 4, u = 5;
int i, j;
void calc(float** array, int i, int j );
void calc1(float** array, int i, int j);
void calc2(float** array, int i, int j);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, i, j); // FUNCTION !!!
float** arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, i, j); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc2(ar, i, j); // FUNCTION !!!
}
void calc(float** array, int i, int j) {
int max = array[0][0];
for (int j = 0; j < k; j++)
{
max = array[0][0];
for (int i = 0; i < k; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc1(float** arr, int i, int j) {
int max = arr[0][0];
for (int j = 0; j < b; j++)
{
max = arr[0][0];
for (int i = 0; i < b; i++) {
if (arr[i][j] > max)
max = arr[i][j];
if (arr[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc2(float** ar, int i, int j) {
int max = ar[0][0];
for (int j = 0; j < u; j++)
{
max = ar[0][0];
for (int i = 0; i < u; i++) {
if (ar[i][j] > max)
max = ar[i][j];
if (ar[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
The parameters to calc() should be the number of rows and columns in the array. Then it should use these as the limits in the for loops.
Also, since you're calculating total negative and maximum for each column, you must reset these variables each time through the column loop.
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int m = 3, k = 3, b = 4, u = 5;
void calc(float** array, int rows, int cols);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, m, k); // FUNCTION !!!
float *arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, b, b); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(ar, u, u); // FUNCTION !!!
}
void calc(float** array, int rows, int cols) {
for (int j = 0; j < cols; j++)
{
int n = 0;
int max = array[0][j];
for (int i = 1; i < rows; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
I am trying to make a matrix multiplication and addition code by making functions, but it is giving me an error, please help me fix it.
I am new to c++ and programming so it is quite difficult for me to understand array and pointers concept.
I have made the functions without pointers but it was giving the same output as this code. And was generating warning of overflow.
#include <iostream>
#include <cstring>
using namespace std;
void matrix(int arr[3][3])
{
cout << "Matrix Input" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "arr[" << i+1 << "][" << j+1 << "] = ";
cin >> arr[i][j];
}
}
}
void printmatrix(int arr[3][3])
{
cout << "Matrix" << endl;
cout << "{";
for (int l = 0; l < 3; l++)
{
cout << "{ ";
for (int a = 0; a < 3; a++)
{
cout << arr[l][a] << " ";
}
cout << "}";
}
cout << "}";
}
int matrixMultiply(int *arr1, int *arr2, int *result)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
result[j] += arr1[k] * arr2[j];
}
arr1 += 3;
arr2 += 3;
result += 3;
}
}
return *result;
}
int matrixAddition(int *arr1, int *arr2, int *result)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result[j] = arr1[j] + arr2[j];
}
arr1 += 3;
arr2 += 3;
result += 3;
}
return *result;
}
int main()
{
int A[3][3], B[3][3], C[3][3], D[3][3] = { {0,0,0},{0,0,0},{0,0,0} };
matrix(A);
matrix(B);
matrixMultiply(A[0], B[0], C[0]);
matrixAddition(A[0], B[0], D[0]);
cout << "Addition of matrix:" << endl;
printmatrix(D);
cout << endl << "Multiplication of matrix:" << endl;
printmatrix(C);
}
output of code after inputting the values is:
Addition of matrix:
Matrix
{{ 6 -8 9 }{ 11 7 0 }{ 2 4 6 }}
Multiplication of matrix:
Matrix
{{ -858993430 -858993460 -858993460 }{ -858993460 -858993432 -858993460 }{ -858993460 -858993460 -858993446 }}
After running this code , it will generate a random array of desired rows and columns . Then a loop will divide the array by its diagonal into an upper side and lower side.In the upper side the loop will look for a max number and in the lower side the loop will look for a min number . Then in the final stage I need to change the positions of min and max . Max in place of min and vice versa.The code runs and finds the min and max.Don't know how to change their places.
Code
#include <iostream>
#include <time.h>
#include <limits.h>
using namespace std;
int main(){
int rows, columns;
int max = INT_MAX;
int min = INT_MIN;
int XindexOfMax, YindexOfMax;
int XindexOfMin, YindexOfMin;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter columns: ";
cin >> columns;
int **array = new int *[rows]; //generating random array
for(int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int)time(NULL)); //generating randoms
for(int i = 0; i < rows; i++){ //loop for the main array
for(int j = 0; j < columns; j++){
array[i][j] = rand() % 10;
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Max: " << endl;
for(int i = 0; i < rows; i++){ //upper half of the diagonal
for(int j = 0; j < columns - i; j++){
cout << array[i][j] << " ";
if(array[i][j] > max){
max = array[i][j];
XindexOfMax = i; //find x and y coordinates if max
YindexOfMax = j;
}
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for (int i = 0; i < rows; i++){ // lower half of the diagonal
for (int j = 0; j < columns; j++){
if (j < columns - i - 1){
cout << " ";
}
else{
cout << array[i][j] << " ";
if(array[i][j] < min){
min = array[i][j];
XindexOfMin = i; //find x and y coordinates if min
YindexOfMin = j;
}
}
}
cout << "\n";
}
cout << "Result" << endl;
//swapping positions of min and max
std::swap(array[XindexOfMax][YindexOfMax], array[XindexOfMin][YindexOfMin]);
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
cout << array[i][j] << " "; //Printing the final array
}
cout << "\n";
}
return 0;
}
In addition to the min and max values, you need to remember the indizes where you found min and max, respectively. Then you can exchange the values (either manually or by using std::swap).
BTW: you need to initialize max and min with INT_MIN and INT_MAX, respectively, and not the other way around.
So it needs to be
int max = INT_MIN;
int min = INT_MAX;
Otherwise, if you write int max = INT_MAX, then no comparison like if(array[i][j] > max) will ever evaluate to true, since there is no integral value greater than INT_MAX.
The swapping is done at the end of main() using std::swap().
I broke a couple of routines into functions. The answer would be improved by breaking out more functions so that each function performs a single task. Still, I added a class, yet tried to stay true to your original design for printing the triangle halves. Hope you can handle some classes at this point.
#include <iostream>
#include <time.h>
#include <limits.h>
#include <cmath>
using namespace std;
class Point {
public:
Point(int x, int y, int value) : x(x), y(y), value(value) {}
int X() { return x; }
int Y() { return y; }
int Value() { return value; }
void SetValue(int valueArg) { value = valueArg; }
void SetPoint(int xArg, int yArg, int valueArg) {
x = xArg;
y = yArg;
value = valueArg;
}
string to_string() {
return std::to_string(value);
}
private:
int x;
int y;
int value;
};
void PrintArray(int **array, int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << array[i][j] << " ";
}
cout << "\n";
}
}
int main() {
int rows, columns;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter columns: ";
cin >> columns;
int **array = new int *[rows]; //generating random array
for (int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int) time(NULL));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = rand() % 10; //generating randoms
}
}
PrintArray(array, rows, columns);
Point maxPoint = Point(0, 0, INT_MIN); // initialize max
Point minPoint = Point(0, 0, INT_MAX);;
cout << "For finding Max: " << endl;
for (int i = 0; i < rows; i++) { //separating the upper half
for (int j = 0; j < columns - i; j++) {
if (j > columns - i) {
cout << array[i][j] << " ";
} else {
cout << array[i][j] << " ";
if (array[i][j] > maxPoint.Value()) {
maxPoint.SetPoint(i, j, array[i][j]);
}
}
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for (int i = 0; i < rows; i++) { //separating the lower half
for (int j = 0; j < columns; j++) {
if (j < columns - i - 1) {
cout << " ";
} else {
cout << array[i][j] << " ";
if (array[i][j] < minPoint.Value()) {
minPoint.SetPoint(i, j, array[i][j]);
}
}
}
cout << "\n";
}
cout << "array before: " << endl;
PrintArray(array, rows, columns);
cout << "Swapping " << "maxPoint(" << maxPoint.X() << ", " <<
maxPoint.Y() << ") with minPoint("
<< minPoint.X() << ", " << minPoint.Y() << ")" << endl;
std::swap(
minPoint.GetCellReference(array),
maxPoint.GetCellReference(array));
PrintArray(array, rows, columns);
return 0;
}
As mentioned in one of the comments, std::swap() is one method of performing a swap. I have modified the example to use std:swap() at the end of main();
I want to compare elements for each line of a matrix, from smallest to biggest.
But I don't want to print the sorted array I want to print the original position.
0 11.80 79.34 78.23
11.80 0 65.23 45.19
79.34 65.23 0 90.27
78.23 45.19 90.27 0
In this Matrix for the first line I wanna print 1, 2, 4, 3
My Code so far:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
string dummy;
double myArray[4][4];
int i;
int j;
int y;
ifstream infile("dist.dat");
cout << "Open file " << "dist.dat" <<" for reading." << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
infile >> myArray[i][j];
if (!infile) {
cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl;
return 0;
}
}
}
infile.close();
cout << "Here's the array from the file" << endl;
cout << fixed << setprecision(2);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
cout << setw(10) << myArray[i][j];
}
cout << endl;
}
cout << endl;
int x = myArray[i][j];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if(myArray[i][j] >= x) {
x = j;
}
else {
x = j + 1;
}
}
cout << x << endl;
}
return 0;
}
You need to maintain another matrix which has indices of every line and then apply the same operations on it that you are applying on each line of the original matrix to sort it.
Here is the code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
void swap(double &x, double &y)
{
double temp = x;
x = y;
y = temp;
}
int main()
{
string dummy;
double myArray[4][4];
int i;
int j;
int y;
int k;
ifstream infile("dist.dat");
cout << "Open file " << "dist.dat" <<" for reading." << endl;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
infile >> myArray[i][j];
if (!infile)
{
cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl;
return 0;
}
}
}
infile.close();
cout << "Here's the array from the file" << endl;
cout << fixed << setprecision(2);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << setw(10) << myArray[i][j];
}
cout << endl;
}
cout << endl;
int sortedIndices[4][4];
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
sortedIndices[i][j] = j+1;
}
}
int x;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
x = j;
for(k = j+1; k < 4; k++)
{
if(myArray[i][k] < myArray[i][x])
{
x = k;
}
}
swap(sortedIndices[i][j], sortedIndices[i][x]);
swap(myArray[i][j], myArray[i][x]);
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << setw(10) << sortedIndices[i][j];
}
cout<<endl;
}
return 0;
}