Ordered Pair to Matrix Problem

Good day! So I have this code that creates matrices when ordered pairs are entered.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;

int matrix();
void ordPair();


int size, i, j, ans, matSize, haha;
int mat[16][16];

int main()
{
    int size, choice;
    do{
    system("cls");
    cout<<"[1] Matrix\n[2] Ordered Pair\nEnter choice: ";
    cin>>choice;
    switch(choice)
    {
		  case 1: matrix();
			  break;
		  case 2: ordPair();
			  break;
		  default: break;
    }
    system("cls");
    cout<<"Do you want to try again?\n[1] Yes\n[0] No\n";
    cin>>ans;
    }while(ans!=0);
    getch();
    return 0;
}

int matrix()
{
    int size, i, j;
    int mat[10][10];
    system("cls");
    cout<<"[2] 2x2\n[3] 3x3\n[4] 4x4\nEnter choice: ";
    cin>>size;
    cout<<"Enter matrix elements(0 and 1 only)"<<endl;
    for(j=1; j<=size; j++)
    {
	     for(i=1; i<=size; i++)
	     {
		      cout<<"Element["<<i<<"]["<<j<<"]: ";
		      cin>>mat[i][j];
	     }
    }
    system("cls");
    cout<<"Displaying matrix: "<<endl;
    for(i=1; i<=size; i++)
    {
	     for(j=1; j<=size; j++)
	     {
		      cout<<setw(4)<<left<<mat[i][j];
	     }
	     cout<<endl;
    }
    cout<<"Ordered pair:"<<endl;
    cout<<"{";
    for(i=1; i<=size; i++)
    {
	     for(j=1; j<=size; j++)
	     {
		      if(mat[i][j]==1)
		      cout<<"("<<i<<", "<<j<<")";
	     }
    }
    cout<<"}"<<endl;
    getch();
    return 0;
}

void ordPair()
{
    int size, i, j, matmat;
    int temp1,temp2;
    int pair[16][16];
    system("cls");
    cout<<"[2] 2x2\n[3] 3x3\n[4] 4x4\nEnter size of matrix: ";
    cin>>matSize;
        
    if(matSize>4)
    {
              cout<<"Invalid input!!"<<endl;
              system("pause");
              ordPair();
              system("pause");
    }
    
    matmat=matSize*matSize;
    cout<<"Enter number of ordered pairs to be entered(1-"<<matmat<<" only): ";
    cin>>size;
    
    do{if(size>matmat)
    {
              cout<<"Invalid input!!"<<endl;
              system("pause");
              cout<<endl;
              cout<<"Enter again: ";
              cin>>size;
    }}while(size>matmat);
    
    for(i=1; i<=size; i++)
    {
	     for(j=1;j<=size;j++)
	     {
	     pair[i][j]=0;
	     }
    }
    
    for(int k=1; k<=size;k++)
    {
            cout<<"Enter domain["<<k<<"]: ";
            cin>>temp1;
            cout<<"Enter range["<<k<<"]: ";
            cin>>temp2;
            cout<<endl;
            pair[temp1][temp2]=1;
    }
    cout<<endl;

    system("cls");

    
    cout<<"Matrix:"<<endl;
    for(i=1; i<=matSize; i++)
    {
	     for(j=1; j<=matSize; j++)
	     {
                                cout<<setw(4)<<left<<pair[i][j];
	     }
	     cout<<endl;
    }
    cout<<endl;
    getch();
}


When I input a 4x4 matrix, and enter 4 ordered pairs, (1,1),(2,2),(3,3) and (4,4), it functions normally. But when I try a 2x2 matrix and input one ordered pair, (2,1), the output would be:
0 16842752
1 1973338893

Any idea why it prints out random numbers?
Thank you!
Last edited on
Topic archived. No new replies allowed.