Visual Studios c++ Square Matricies

i am having trouble creating a square matrix with my code. I am not allowed to use the vector library or the math library. I am restricted to iostream and fstream. i am attempting to assign rows and cols with this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//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);
	
}

and i can tell from the debugger its not running how i want it to at numbers like 4,9 and 16
i created an array pointer with which i allocate memory storage. i need the rows and cols # from the total length in order to limit the size my other methods iterating through the array. if you are really want to see the rest of it, its here https://github.com/UW-COSC-2030-SP-2018/umatrix-f18--william-bollinger
Last edited on
closed account (E0p9LyTq)
You can simulate a 2D array using one dimension:

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

int main()
{
   std::cout << "Enter the size of your square matrix: ";
   size_t mSize = 0;
   std::cin >> mSize;
   std::cout << '\n';

   int* matrix = new int[mSize * mSize];

   // let's initialize with values
   for (size_t row_loop = 0; row_loop < mSize; row_loop++)
   {
      for (size_t col_loop = 0; col_loop < mSize; col_loop++)
      {
         matrix[(mSize * col_loop) + row_loop] = (100 * (row_loop + 1)) + col_loop + 1;
      }
   }

   // let's print the matrix
   for (size_t row_loop = 0; row_loop < mSize; row_loop++)
   {
      for (size_t col_loop = 0; col_loop < mSize; col_loop++)
      {
         std::cout << matrix[(mSize * col_loop) + row_loop] << ' ';
      }
      std::cout << '\n';
   }

   // forgot the most important part of dealing with the heap,
   // deleting the newed array.  OOOOPS!
   delete[] matrix;
}

Enter the size of your square matrix: 5

101 102 103 104 105
201 202 203 204 205
301 302 303 304 305
401 402 403 404 405
501 502 503 504 505
Last edited on
That makes clear a comment i need on my code. The square input is the length of the 1d array. and i am having trouble solving for the square. And your iteration method is really interesting.
Last edited on
Your question is not entirely clear.

On one hand you talk about some "matrix" but on the other hand your question is not at all about any matrices.


Do you actually want to compute an integer square root of an integer value?
Topic archived. No new replies allowed.