New to programming, needing help with a task.

Hello. I have a task to do a program, which does:
Sort the rows of a two-dimensional numeric array in ascending order and display the maximum elements for each row of the array.

1. The array should be of dimension [m] [n], where m and n are integers in the interval [4,10].
2. Create functions for input, output, finding the maximum element of a row, and sorting the two-dimensional array in ascending order.
3. Call up the created functions.
4. The sorted array to be displayed on the screen and saved in a text file by lines.
5. Display and save the maximum element for each row of the array in a text file.

With a friend, all we've come up to currently is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  #include<iostream>
#include<fstream>
using namespace std;
void output(int **arr, int n, int m);
void input(int **arr, int n, int m);
int max(int **arr, int n, int m);
int max(int **arr,int n,int m)
{
	int max = 0;
	for (int row = 0; row < m; row++)
	{
	  for (int col = 0; col < n; col++) 
		 { 
		 	if (arr[n][m] > max)
		 	{
		 		max = arr[n][m];
		 	}
		 } 
	}
	return max;
}
void input(int **arr,int n,int m)
{
	for (int row = 0; row < m; row++)
	{
		cout<<"number row: "<<row<<endl;
	 		 for (int col = 0; col < n; col++) 
			 { 
		 		cout<<"masiv ["<<row<<"]["<<col<<"] = ";
				cin>>arr[row][col];
			 } 
	}
}
void output(int **arr,int n,int m)
{
	for (int row = 0; row < m; row++)
	{
	  for (int col = 0; col < n; col++) 
		 { 
			cout<<"array ["<<row<<"]["<<col<<"] ="<<arr[row][col]<<endl;
		 } 
	}
}
int main()
{
	int n, m;
	do
	{
		cout<<"n= ";
		cin>>n;
		cout<<"m= ";
		cin>>m;
	}
	while(n<4||n>10||m<4||m>10);
	int** arr = new int*[n];
  	for(int i = 0; i < n; ++i)
    {
		arr[i] = new int[m];
  	}						  
	input(arr,n,m);
	output(arr,n,m);
	fstream file2;
	file2.open("C:/file2.txt",ios::out);
	file2<<max(arr,n,m);
	file2.close();
	fstream file;
	file.open("C:/file.txt",ios::out);
	for(int i= 0;i<5;i++)
	{
		for(int j = 0;j<5;j++)
		{
			file<<arr[n][m]<<" ";
		}
	file<<endl;
	}
	file.close();
}


We can't figure out how to sort it, also why doesn't the first file even get created. Also the second one has no content. I'd be glad if someone can help us do this program. Thanks in advance.
The problem is in your function max.
Have a closer look at lines 14 and 16 and 68 and 70 and 72
The problem might be a bit more obvios if you choose names like rows and columns.
Hello vsxxx,

As Thomas1965 has mentioned proper names make a big difference.

Next when working with a 2D array in C++ the "row" is always first and the "column" is second. With variable names like "m" and "n" it is easy to get them backwards. Which you have managed to do.

There have been many posts involving sorting an array here. Do a search and see what you can find or do a google search, I used (c++ sort array). The first choices deal with using "std::sort" from the "algorithm" header file and farther down I found code that yo can use. See what you can come up with.

I made some changes to your code. This should give you an idea of what you can do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
int main()
{
    int maxRow{}, maxCol{};

    std::cout << "\n Enter The number of rows and columns.\n Minimum is 4 and maximum is 10.\n";

    do
    {
        cout << "\n Enter how many rows = ";
        cin >> maxRow;

        cout << "\n Enter how many columns = ";
        cin >> maxCol;

        if (!std::cin)
            std::cout << '\n';

        if (!std::cin || (maxRow < 4 || maxRow>10 || maxCol < 4 || maxCol > 10))
        {
            if (!std::cin)
            {
                std::cerr << "\n     Invalid input! Must be a number.\n";

                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
            }
            else if (maxRow < 4 || maxRow>10 || maxCol < 4 || maxCol > 10)
            {
                std::cerr << "\n     Invalid Choice! row or column is out of range. Must be 4 - 10.\n";
            }
        }
        else
            break;
    } while (true);

    int** arr = new int*[maxRow];

    for (int i = 0; i < maxRow; ++i)
    {
        arr[i] = new int[maxCol];
    }

    input(arr, maxRow, maxCol);

    //output(arr, maxRow, maxCol);

    //fstream file2;

    //file2.open("C:/file2.txt", ios::out);

    //file2 << max(arr, maxRow, maxCol);

    //file2.close();

    //fstream file;
    //file.open("C:/file.txt", ios::out);

    //for (int i = 0; i < 5; i++)
    //{
    //    for (int j = 0; j < 5; j++)
    //    {
    //        file << arr[n][m] << " ";
    //    }

    //    file << endl;
    //}

    for (int row = 0; row < maxRow; row++)
        delete[] arr[row];
 
    delete[] arr;

    //file.close();  // <--- Not needed as the file will close when the function looses scope. OK if you leave it
}

In the do/while loop the if statements will catch any problems and allow you to fix them before the program continues.

Lines 15 and 16 are to format what is printed on the screen so it looks better.

The section I have commented is because I have not worked on this part yet or completely.

Lines 58 - 66. This part is what Thomas1965 is referring to. At least as a start.

This has several problems.

First the for loops define "i" and "j" which is fine for the loop iterators, but they are never used for anything else.

In line 62 you are using "n" for the row and "m" for the column, but look back at lines 50 and 52 where you have entered the maximum size of the array. here you are accessing memory outside the boundary of the array and what you get is unknown.

The code should be written as:
1
2
3
4
5
6
7
8
9
10
11
    ofstream outFile("file.txt");

    for (int row = 0; row < maxRow; row++)
    {
        for (int col = 0; col < maxCol; col++)
        {
            outFile << arr[row][col] << " ";
        }

       outFile << endl;
    }

Line 1 will define and open the file with 1 line of code. The stream name is your choice, I tend to like this one.

And when I tested the code it produced a file with these numbers:

25 10 99 5 
42 66 100 12 
99 5 122 256 
512 1048 2096 55 



When you get to a function here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int max(int **arr, int maxRow, int maxCol)
{
    int max = 0;

    for (int row = 0; row < maxCol; row++)
    {
        for (int col = 0; col < maxRow; col++)
        {
            if (arr[row][col] > max)
            {
                max = arr[row][col];
            }
        }
    }

    return max;
}


I have not tried this yet, but I am thinking it could be shortened to:
1
2
3
4
int max(int **arr, int row, int maxCol)
{
     return arr[row][maxCol - 1];
}

This would change the "row" every time the function is called and since the array should be sorted by the time you call this function the largest value would be in the last column. The (- 1) is because "maxCol" would be 1 past the size of the array.

In the beginning "maxRow" and "maxCol" define the size of the array and refers to the number of elements in the array not an actual array element.

Looking at what you need to do:
1. The array should be of dimension [m] [n], where m and n are integers in the interval [4,10].
When you get instructions like this you will find that those who write them like to use single letter variable names. that does not mean that yo have to. Since C++ deals wit a 2D array by "row" and "column" that would mean that "m" refers to the "row" and "n" refers to the column. The [4,10] part I am not sure if this means a maximum of 4 rows and 10 columns or a minimum of 4 and maximum of 10. based on your code I take it as the latter.

4. The sorted array to be displayed on the screen and saved in a text file by lines
Here you have not displayed the array to the screen, but you do have code to output it to a file.

You have not done anything with step 5 yet. This could be because your "Max" function is not working yet.

Work on your program in small steps and not try to do every thing at once.

First get your input to work then sort the array before moving on. Then a function to print the array would be handy to see what you have input and what you get after it is sorted. These function calls can be moved or deleted later.

Andy
Thanks you all for the help, we will see what we can manage to do :D
Topic archived. No new replies allowed.