Two-dimensional arrays

Hi. so i have a two dimensional array , who's parameters are decided by the user . Hence i cannot initialize it . Now i want to take input for the first the row and then check some condition and then move on and take input for the next row.
Please Help mee...
Last edited on
Yes, thanks. but how do i take input for only the first row and check something and then go to the next row . considering that its a two dimensional array
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
#include <iostream>
#include <vector>

int main()
{
    std::size_t rows, cols ;
    std::cout << "number of rows? " ;
    std::cin >> rows ;
    std::cout << "number of cols? " ;
    std::cin >> cols ;

    std::vector< std::vector<int> > array_2d ;

    // read in the values for the first row
    std::vector<int> row ;
    std::cout << "please enter " << cols << " values for the first row: " ;
    for( std::size_t i = 0 ; i < cols ; ++i )
    {
        int value ;
        std::cin >> value ;
        row.push_back(value) ;
    }

    // TODO: check some cindition on the first rwo

    // add the row to array_2d
    array_2d.push_back(row) ;

    // no repeat for the remaining row-1 rows
    for( std::size_t i = 1 ; i < rows ; ++i )
    {
        std::cout << "please enter " << cols << " values for row #" << i << ": " ;
        for( std::size_t j = 0 ; j < cols ; ++j )
        {
            int value ;
            std::cin >> value ;
            row.push_back(value) ;
        }

        // add this row to array_2d
        array_2d.push_back(row) ;
    }
}
@pushkin

In my opinion you must begin with the necessary declarations and then enter the elements of the matrix. Then make your checks. Here's an attempt for an 2-dimension array:
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int row, col;
	cout << "Enter how many rows: ";
	cin >> row;
	cout << "Enter how many columns: ";
	cin >> col;

// dynamic memory allocation: 
// you can use any type instead of int

	int **myArray = new int *[row]; 
	for(int i = 0; i < row; ++i)
		myArray[i] = new int[col];
	
	cout << "\nEnter the elements of the matrix\n\n"; 
	for (int i=0; i < row; i++)
	{
		for (int j=0; j < col; j++)
		{
			cout << "Row " << i + 1 << " Col " << j + 1 << ": ";
			cin >> myArray[i][j]; 
		}
		cout <<'\n';
	}
	
	cout << "Check: \n\n";
	for(int i = 0; i < row; ++i)
	{
		for(int j = 0; j < col; ++j)
			cout << setw(8) << myArray[i][j];
		cout << '\n';
	}
	
//----- your code ----


//free the allocated memory

	for(int i = 0 ; i < row ; ++i )
		delete [] myArray[i] ;
		
	delete [] myArray ;
	
	return 0;
}
Enter how many rows: 3
Enter how many columns: 4

Enter the elements of the matrix

Row 1 Col 1: 1
Row 1 Col 2: 2
Row 1 Col 3: 3
Row 1 Col 4: 4

Row 2 Col 1: 5
Row 2 Col 2: 6
Row 2 Col 3: 7
Row 2 Col 4: 8

Row 3 Col 1: 9
Row 3 Col 2: 10
Row 3 Col 3: 11
Row 3 Col 4: 12

Check: 

       1       2       3       4
       5       6       7       8
       9      10      11      12
@condor
Thanks . But it didn't help that much .
My code is very much similar to the one you wrote above ..
referring to your code.
the only difference is that i have to print the two dimensional array after the user provides the row 1 col 1-4 1
Wondering if you could help me with that .
thanks:)
OK, see the next code modified how it seems that may interest you, I hope.

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
#include <iostream>
#include <iomanip>

using namespace std;

template<typename T>
void printArray(int l, int m, int n, T arg)
{
	for(int i = l; i < m; ++i)
	{
		for(int j = 0; j < n; ++j)
			cout << setw(8) << arg[i][j];
		cout << '\n';
	}
}

int main()
{
	int row, col;
	cout << "Enter how many rows: ";
	cin >> row;
	cout << "Enter how many columns: ";
	cin >> col;

// dynamic memory allocation: 
// you can use any type instead of int

	int **myArray = new int *[row]; 
	for(int i = 0; i < row; ++i)
		myArray[i] = new int[col];
	
	cout << "\nEnter the elements of the matrix\n\n"; 
	for (int i=0; i < row; i++)
	{
		cout << "Row " << i + 1 << '\n';
		for (int j=0; j < col; j++)
		{
			cout << "    Col " << j + 1 << ": ";
			cin >> myArray[i][j]; 
		}
		// your check code 
		//       ...
		// for example print out the actual array:
		
		cout << "\nCheck row " << i + 1 << " : \n";
		printArray(i, i + 1, col, myArray);
		cout << "Plus the result(s) of your check code...\n";
		// end of the check code and continue with
		// the next input line
		cout <<'\n';
	}
	
	cout << "Entered matrix: \n";
	printArray(0, row, col, myArray);
	
//----- the rest of your code ----


//free the allocated memory

	for(int i = 0 ; i < row ; ++i )
		delete [] myArray[i] ;
		
	delete [] myArray ;
	
	return 0;
}
Enter how many rows: 3
Enter how many columns: 4

Enter the elements of the matrix

Row 1
    Col 1: 1
    Col 2: 2
    Col 3: 3
    Col 4: 4

Check row 1 : 
       1       2       3       4
Plus the result(s) of your check code...

Row 2
    Col 1: 5
    Col 2: 6
    Col 3: 7
    Col 4: 8

Check row 2 : 
       5       6       7       8
Plus the result(s) of your check code...

Row 3
    Col 1: 9
    Col 2: 10
    Col 3: 11
    Col 4: 12

Check row 3 : 
       9      10      11      12
Plus the result(s) of your check code...

Entered matrix: 
       1       2       3       4
       5       6       7       8
       9      10      11      12
Last edited on
Topic archived. No new replies allowed.