Struct , Pointers

The program below, when activated, will ALWAYS hang when i retry the option
for eg. Menu -> Option 6 -> successful output -> menu ( its a do while loop )
and den from menu -> Option 6 -> output 1/2 and hang.

can anyone tell me what is wrong with my program here?

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
		else if(option == 6)
		{
			cout << "Enter elements for 1st sparse matrix: ";
			cin >> size;
			cout << "Enter elements for 2nd sparse matrix: ";
			cin >> size2;
			constructMatrix(sparseA, size);
			constructMatrix(sparseB, size2);
			cout << "\nArray information 1 - Row Major Order\n" << endl;
			printMatrix(sparseA, size);
			cout << "\nArray information 2 - Row Major Order\n" << endl;
			printMatrix(sparseB, size2);
			
			int sizeSum = addTwoMatrix(sparseA, sparseB, sparseC, size, size2);
			//nth to do with combined. should be add matrix error
			combineMatrix(sparseC, sizeSum);
			cout << "\nSum Array information 2 - Row Major Order\n" << endl;
			printMatrix(sparseC, (sizeSum));

		
		}
		else if(option == 7)
		{
			cout << "Enter elements for 1st sparse matrix: ";
			cin >> size;
			cout << "Enter elements for 2nd sparse matrix: ";
			cin >> size2;
			constructMatrix(sparseA, size);
			constructMatrix(sparseB, size2);
			cout << "\nArray information 1 - Row Major Order\n" << endl;
			printMatrix(sparseA, size);
			cout << "\nArray information 2 - Row Major Order\n" << endl;
			printMatrix(sparseB, size2);
			
			int sizeSub = subtractTwoMatrix(sparseA, sparseB, sparseC, size, size2);
			//nth to do with combined. should be sub matrix error
			combineMatrix(sparseC, sizeSub);
			cout << "\nDifference Array information 2 - Row Major Order\n" << endl;
			printMatrix(sparseC, (sizeSub)); 
			  	  
		
		}


using this call, i am doing these functions

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
int addTwoMatrix(Element *eleArray ,Element *eleArray2 ,Element *sparseC ,int size,int size2)
{
	Element *ptrA = &eleArray[0];
	Element *ptrB = &eleArray2[0];
	Element *ptrC = &sparseC[0];
	
	//SEE HERE . i assign 2 additional pointers so
	// They wont move my pointer A and B to other places.
	Element *testA = &eleArray[0];
	Element *testB = &eleArray2[0];
	Element *testC = &sparseC[0];
	int size3;

	cout << "\n size = " << size << endl;
	for(int q=0; q<size; q++)
	{
		cout << "testA value " << q << " is : " << testA -> i << " " 
			 << testA -> j << " " << getEle(testA -> value) << endl;
			 
		testA++;
	}
	cout << endl;
	cout << "\n size 2 = " << size2 <<endl;
	cout << endl;
	for(int r=0; r<size2; r++)
	{
		cout << "testB value " << r << " is : " << testB -> i << " " 
			 << testB -> j << " " << getEle(testB -> value) << endl;
			 
		testB++;

	}
	cout << endl;
	
	size3 = size+size2;
	
	for(int k = 0; k < size3; k++)
	{
		int *valueC;
		valueC = new int;
		
		if(ptrA->i != ptrB -> i && ptrA -> j != ptrB -> j)
		{
			*ptrC = *ptrA;
			
			cout << "testC value " << k << " is : " << testC -> i << " " 
			 << testC -> j << " " << getEle(testC -> value) << endl;
			 
			testC++;
			ptrA++;
			ptrC++;
		}
		
		else if(ptrA->i == ptrB ->i && ptrA -> j == ptrB -> j)
		{
			ptrC -> i = ptrA -> i;
			ptrC -> j = ptrA -> j;
			
			*valueC = getEle(ptrA -> value) + getEle(ptrB -> value);
			ptrC -> value = valueC;
			
			cout << "testC value " << k << " is : " << testC -> i << " " 
			 << testC -> j << " " << getEle(testC -> value) << endl;
			
			testC++;
			ptrA++;
			ptrB++;
			ptrC++;
			size3 --;
		}
		else
		{
			*ptrC = *ptrB;
			cout << "testC value " << k << " is : " << testC -> i << " " 
			 << testC -> j << " " << getEle(testC -> value) << endl;
			 
			testC++;
			ptrB++;
			ptrC++;
		}
		
		
	}
	
	return size3;
}

int subtractTwoMatrix(Element *eleArray ,Element *eleArray2 ,Element *sparseC ,int size,int size2)
{
	Element *ptrA = &eleArray[0];
	Element *ptrB = &eleArray2[0];
	Element *ptrC = &sparseC[0];
	int size3 =  size + size2;
	
	//SEE HERE . i assign 2 additional pointers so
	// They wont move my pointer A and B to other places.
	Element *testA = &eleArray[0];
	Element *testB = &eleArray2[0];
	Element *testC = &sparseC[0];

	cout << "\n size = " << size << endl;
	for(int q=0; q<size; q++)
	{
		cout << "testA value " << q << " is : " << testA -> i << " " 
			 << testA -> j << " " << getEle(testA -> value) << endl;
			 
		testA++;
	}
	
	cout << "\n size 2 = " << size2 <<endl;
	for(int r=0; r<size2; r++)
	{
		cout << "testB value " << r << " is : " << testB -> i << " " 
			 << testB -> j << " " << getEle(testB -> value) << endl;
			 
		testB++;

	}
	
	
	for(int k = 0; k < size3; k++)
	{
		int *valueC;
		valueC = new int;
		
		if(ptrA->i != ptrB -> i && ptrA -> j != ptrB -> j)
		{
			*ptrC = *ptrA;
			
			cout << "testC value " << k << " is : " << testC -> i << " " 
			 << testC -> j << " " << getEle(testC -> value) << endl;
			 
			testC++;
			ptrA++;
			ptrC++;
		}
		
		else if(ptrA->i == ptrB ->i && ptrA -> j == ptrB -> j)
		{
			ptrC -> i = ptrA -> i;
			ptrC -> j = ptrA -> j;
			
			*valueC = getEle(ptrA -> value) - getEle(ptrB -> value);
			ptrC -> value = valueC;

			cout << "testC value " << k << " is : " << testC -> i << " " 
			 << testC -> j << " " << getEle(testC -> value) << endl;
			
			testC++; 	    		
			ptrA++;
			ptrB++;
			ptrC++;
			
			if((*ptrC).value == 0)
			{
				size3 -= 2;
			}
			else
				-- size3;
		}
		else
		{
			ptrC -> i = ptrB -> i;
			ptrC -> j = ptrB -> j;
			
			*valueC = 0 - getEle(ptrB -> value);
			ptrC -> value = valueC;
			
			cout << "testC value " << k << " is : " << testC -> i << " " 
			 << testC -> j << " " << getEle(testC -> value) << endl;
			
			testC++;
			ptrB++;
			ptrC++;
		}	   
		
	} 
	
	return size3;
}
Topic archived. No new replies allowed.