Issue with counting ints in an array larger than input

Only allowed to use iostream library because thats is what the assignment says. i Agree. Vectors would be the bomb diggity here.
trying to calculate the actual number of numbers in a large allocated array for 100 numbers but the user does not have to enter that many numbers every time. How do i check that input[size]!= a null pointer or something like that.
1
2
3
4
5
6
7
8
void uMatrix::calcSize(int input[]) {
	//new *pointer;
	int temp = 0;
	while (input[size] !=NULL) {
		temp++;
	}
	size = temp;
} 
Last edited on
A raw array does not hold its size, the programmer must keep track of it independently to prevent buffer overflows (accessing out-of-bound indices).

If I have an int array with 5 elements, the memory looks something like this:

1
2
3
int arr[5];
for (int i = 0; i < 5; i++)
    arr[i] = (i+1)*(i+1);

1
2
           [0] [1] [2] [3] [4] 
[..., junk, 1,  4,  9, 16, 25, junk, junk junk, ... ]

There is no guarantee that arr[5] == 0, you would be invoking undefined behavior if you tried to access arr[5].

An std::vector can keep track of its own size and be dynamically resized. You might want to look into that. Otherwise, could you provide a more concrete example of what you're trying to do?
uMatrix.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
 #include "uMatrix.h"
//uMatrix::uMatrix() {
//
//}
uMatrix::uMatrix(int someArray[])
{
	//initialize variables 
	//allocate array 
	calcSize(someArray);
	Alloc(someArray);
	fillArray(someArray);
	//for(int i=0;i<)
	//Array = new int *[sizeof(matrixA)];//new array size

	////memory allocated for  elements of each column.


	//for (int i = 0; i < rows; i++)
	//	Array[i] = new int[cols];
}

uMatrix::~uMatrix()
{

	for (int i = 0; i < rows; i++)
		delete[] Array[i];
	delete[] Array;
}
//square=total length of 1d array
void uMatrix::sqrt(int square)
{

		double root = square / 3;
		if (square <= 0)
			rows = 0;
		cols = 0;
		for (int i = 0; i < 100; i++) {
			root = (root + square / root) / 2;
		}
		rows = static_cast <int>(root);
		cols = static_cast<int>(root);
	
	/*double temp;
	double x0 = square * (10 * 10); // x0 = 3 * (10^2)

	for (int i = 1; i < size;i ++) {
		temp = 0.5*(x0 + (square / x0));
	}
	*/

	
}
void uMatrix::setSize(int in)
{
	size = in;
}
void uMatrix::calcSize(int input[]) {
	//new *pointer;
	int temp = 0;
	while (input[size] !=NULL) {
		temp++;
	}
	size = temp;
}
void uMatrix::Alloc(int input[]) {
	//size = sizeof(input);
	sqrt(size);
	Array = new int*[rows];
	for (int i = 0; i < rows; i++) {
		Array[i] = new int[cols];

	}
}	

void uMatrix::fillArray(int input[])
{
	
	for (int i = 0; i < rows; i++) {
		//Array[i] = input[i];
		for (int j = 0; j < cols; j++) {
			Array[i][j]=input[i];
		}
	}
//	print();
	
}
//void uMatrix::allocArray()
//{
//	Array = new int*[rows];
//	for (int i = 0; i < rows; i++) {
//		Array[i] = new int[cols];
//}
//
//void uMatrix::input(std::string input) {
//	std::string fileName;
//	ifstream inFile(fileName);
//	if (!inFile) {
//		cout << "There was an error in opening the file.";
//	}
//	else {
//
//	}
//}

void uMatrix::print()
{
	//for (int i = 0; i < rows; i++) {
	//	
	//	for (int i = 0; i < cols; i++) {
	//		cout << Array[i];
	//	}
	//}
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			cout << Array[i][j]<< "  ";
		}
		cout << endl;
	}
}

uMatrix uMatrix::MM(uMatrix B)
{
	uMatrix temp (*this->Array);

		for (int i = 0; i < rows; i++) {
			for (int k = 0; k< B.cols; k++) {
				temp.Array[i][k] = 0;

				for (int j = 0; j < cols; j++)

				{

					temp.Array[i][j] += Array[i][j] * B.Array[j][k];

				}
			}
		}
	
	/*for (int i = 0; i < size; i++) {
		temp[i] += *B.Array[i] * *Array[i];
	}*/
	return temp;
}

uMatrix uMatrix::MMf(uMatrix B)
{
	//idea here is to combine two loops to reduce computation time. 
	uMatrix temp(*B.Array);
	for (int ij  = 0; ij < cols*cols; ij++)
	{
		int i = ij / cols;
		int j = ij % cols;

		temp.Array[i][j] = 0;
		for (int k = 0; k < cols; k++)
			temp.Array[i][j] += Array[i][k] * B.Array[k][j];
	}
	//attempted a single index solution. 
	/*for (int i = 0; i < rows; i++)
	{
		for (int k = 0; k< B.cols; k++) {
			temp.Array[i][k] = 0;

			for (int j = 0; j < cols; j++)

			{

				temp.Array[i*cols+j] += Array[i][j] * B.Array[j][k];

			}
		}
	}*/
	
	return temp;
}

uMatrix uMatrix::MA(uMatrix B)
{

	//added another loop to make the other one faster. thus potato creator is the potato number
	uMatrix temp(*Array);
	for (int PotatoNumber = 0; PotatoNumber < size; ++PotatoNumber) {
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				temp.Array[i][j] = Array[i][j] + B.Array[i][j];
			}
		}
	}
	return temp;
}

uMatrix uMatrix::MAf(uMatrix B)
{
	uMatrix temp(*Array);
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			temp.Array[i][j] = Array[i][j] + B.Array[i][j];
		}
	}
	return temp;
}

uMatrix uMatrix::SM(int S)
{
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			Array[i][j] = Array[i][j] * S;
		}
	}
	return *this;
}

uMatrix uMatrix::SMf(int S)
{
	// i feel like a cant reduce this lower than 2 loops
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			Array[i][j] = Array[i][j] * S;
		}
	}
	return *this;
}

uMatrix uMatrix::SA(int S)
{
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			Array[i][j] = Array[i][j] + S;
		}
	}
	return *this;
}

uMatrix uMatrix::SAf(int S)
{
	//i honestly dont know how to make this any faster. 
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			Array[i][j] = Array[i][j] + S;
		}
	}
	return *this;
}


main.cpp
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
 
#include "uMatrix.h"
int array1[MAX];
int array2[MAX];
int tempScale;

int main(int argc, char **argv)
{
	cout << "hello world\n";
	//string lineInput = " ";
	//while (lineInput.length()>0) {
	//	cin >> lineInput;
	//	//cout << lineInput;
	//}
	////ifstream inFile(*argv);
	//std::string fileName;
	//ifstream inFile;
	//inFile.open(fileName); 
	//if (!inFile) {
	//	cout << "There was an error in opening the file.";
	//}
	//else {

	//	//array1[] = inFile.getline();
	//}
	//
	//You should probably read in two matrices and a scalar value
	int tempSize;

	//bool file = false;
	std::string fileName;
	// diddnt know how else to make a variable limit for the amount of numbers input without a vector.
	cout << "how many numbers will be in your array? ";
	cin >> tempSize;
	if (cin.fail()) {
		cin.clear();
		cin.ignore();
		cout << "There was an error with the number you put in, try again: ";
		cin >> tempSize;
	}
	//	array1(tempSize);
	//cout << "Enter your entire first array: ";

	cout << "fill your array by entering a row of numbers: ";//seperated by white space
	int n;
	for (int i = 0; i < tempSize; i++) {

		/*cin>>n;
		if (cin.fail()) {
			cin.clear();
			cin.ignore();

		}
		else {*/
		cin >> array1[i];

	}



	//	array2[tempSize];
	//string lineInput = " ";
	//while (lineInput.length() > 0)){
	cout << "fill your second array";
	for (int i = 0; i < tempSize; i++) {
		/*cin >> n;
		if (cin.fail()) {
			cin.clear();
			cin.ignore();
		}
		else {*/
		cin>>array2[i];
		//cin >> n;
	}



	cout << "Enter a value to be the scalar: ";
	cin >> tempScale;
	int S = tempScale;

	
	uMatrix A(array1);
	A.setSize(tempSize);
    uMatrix B(array2);
	B.setSize(tempSize);
	//cout << "Enter a value to be the scalar: ";
	//int tempScale;
	//cin >> tempScale;
 //   int S = tempScale;
 //   
    //How about multiplying those two and creating a new uMatrix C?
    
    uMatrix C = A.MM(B);
	C.print();
    //Add C to B?
    
    uMatrix D = B.MA(C);
    
    //Scalar Add to A?
    
    uMatrix E = A.SA(S);
    
    // so on...
	A.~uMatrix();
	B.~uMatrix();
	C.~uMatrix();
	D.~uMatrix();
	E.~uMatrix();
	system("pause");
	return 0;
}

diddnt want to do that. but if it helps you help me by all means look at how poorly i code. Help me please :D!
Last edited on
1
2
int array1[MAX];
int array2[MAX];

When you first declare an array, it has uninitialized values. If you want to set uninitialized values to zero, initialize each array by doing
1
2
int array1[MAX] = {}; // initializes each element to its default value, 0.
int array2[MAX] = {};


Then, when you fill in your array, say, with {1, 2, 3, 4, 5}, the rest of the numbers will be 0.
{1, 2, 3, 4, 5, 0, 0, 0, 0, ...}.

1
2
3
4
5
	int temp = 0;
	while (input[size] != 0) {
		temp++;
	}
	size = temp;

Note that this won't be able to detect when 0 is actual valid data; you are treating 0 as a sentinel value in this manner.

Also, note you're not really supposed to call the destructors manually.
Last edited on
Topic archived. No new replies allowed.