Fozzy Fozborne Posted December 2, 2009 Share Posted December 2, 2009 Hey guys, this is probably really easy to do, but I'm sort of at a dead-end as far as where to go with this assignment. The promt is as follows: Write a program that declares a float matrix of size nXn. The user will supply the value of n which should be in the range between 1-20. The program must check if n is in the proper range and prompt the user to enter a different value as long as it is out of that range. The program then prompts the user to input the values of the elements in the matrix. After that, the program must compute the summation of elements in the main diagonal of the matrix. Also, the program will transpose the matrix and store the transposed one on another matrix of the same dimension. The transposed matrix should be displayed on the screen in a matrix form (row by row). I thought matrices had to be defined by the code and couldn't be dynamically changed. I've read some forums online and they suggested I make a sort of imaginary matrix which is only there to temporarily hold values. To be honest we never went over that and I don't really get how it would work or how it could be outputted. This is as far as I've gotten and I know it's not very impressive, but like I said I have no other ideas. #include <iostream>#include <iomanip>#include <fstream> #include <cmath>#include <string>using namespace std;int main(){int n = 0;float Matrix [20][20];int size = 0;int null = 0;cout << "Please enter the number of rows and columns. The number must be between 1 and 20. ";cin >> n; if(n > 20) cout << "invalid entry" << endl; else if(n < 1) cout << "invalid entry" << endl; else size = n; size = n;return 0;} Link to comment https://gtaforums.com/topic/434410-creating-and-modifying-a-user-defined-matrix/ Share on other sites More sharing options...
K^2 Posted December 2, 2009 Share Posted December 2, 2009 (edited) There are two ways to go about it. I'm not sure which one your instructor meant, but if you have not talked about pointers and operator new, I suspect it is the first method. 1) Define a static matrix of size 20x20, like you do, and then only use first n rows and n columns of that matrix. 2) Create matrix on the fly. This is a bit more complex. The idea is that the variable responsible for the array is just a pointer to the place in memory where that array is stored. Matrix is array of arrays. To create an nxn matrix that you can access with [][] notation, you have to do a bit of work. Here is the code for it. int n,i; //n will be initiated to some positive integer value. before matrix is actually created.float **matrix; //Pointer to array of pointers.//This is where the matrix is actually created.matrix=new (*float[n]); //Create array of n pointers of type float.for(i=0;i<n;i++)matrix[i]=new float[n]; //Create an array for particular column in the matrix. After these two lines here, matrix is an nxn array. You can access an element exactly same way as in fixed matrix using matrix[y][x], where x and y are two integers in range 0 to n-1. 2.b) The above creates some overhead and is not always convenient to work with. Very often, instead of creating an nxn matrix, people will create a single array with n*n elements in it. The code consists of single declaration and single memory allocation call. int n;float *matrix; //Pointer to array of type float that we'll use as a matrix.matrix=new float[n*n]; //Create the array of n*n values. The difference is that now instead of using matrix[y][x] you'll be using matrix[n*y+x] or matrix[n*x+y], depending on whether you are more interested in working with rows or columns. There are a lot of reasons to do this, but for this assignment it doesn't really matter. Edited December 2, 2009 by K^2 Link to comment https://gtaforums.com/topic/434410-creating-and-modifying-a-user-defined-matrix/?do=findComment&comment=1059667004 Share on other sites More sharing options...
Fozzy Fozborne Posted December 3, 2009 Author Share Posted December 3, 2009 Ah! thanks a ton dude, I guess I never thought about using only part of the matrix because I thought it had to be defined as length but forgot the fact that it's like an array where you can under-use it but not over-use it. Link to comment https://gtaforums.com/topic/434410-creating-and-modifying-a-user-defined-matrix/?do=findComment&comment=1059667040 Share on other sites More sharing options...
K^2 Posted December 3, 2009 Share Posted December 3, 2009 There are situations where you can overuse the array, but you have to be extremely careful about it. For example, if you define int matrix[5][5], then matrix[0][6] will usually address the same cell as matrix[1][1]. Similarly, you can use arrays to access other variables, and even variables from the function that called current function! But you really should learn pointers first and see for yourself what happens when you mess up with addressing. Link to comment https://gtaforums.com/topic/434410-creating-and-modifying-a-user-defined-matrix/?do=findComment&comment=1059667323 Share on other sites More sharing options...
Fozzy Fozborne Posted December 8, 2009 Author Share Posted December 8, 2009 Write a C++ program to solve the following problem. Your program should properly compile and run and is well documented. You need to submit the following two things: 1. Write a program that have four different functions, main() in addition to three more user-defined functions. The main() function is the one that will invoke all other functions. Appropriate display messages should be used in main() before and after each invocation to indicate that the function is about to be called and the function did return. The first user-defined function is a void one that basically computes the result of the series below and displays it on the screen in a table format as shown also below. All computation and display must be inside the function. The function should repetitively compute the series for values of I from 1 to 20 (as shown in the table). Once done, the function returns to the calling function, main(). Hint: Function prototype can take the form “void ComputeSeries(viod)”. i m(i) 1 0.5 2 1.1667 ... 19 16.4023 20 17.3546 The second function is a value-returning function that uses reference parameters to pass values to the calling module. This function takes the following form “double ComputeTrigonometricValues(int, double&, double&)”. The first argument is the degree which is passed as integer, the second one is a reference parameter the will contain the value of computing the trigonometric sin function upon that degree, the last argument is the value of computing the trigonometric cosine function upon passed degree. The return value of that function is the tangent (tan value) of the same degree. The mere task of this function is to receive a degree from main() then compute and return back to the caller the values for sin, cos, and tan of that degree. The main() function is in charge of repeatedly calling that function, ComputeTrigonometricValues(), in order to display on the screen the table shown below. Degree Sin Cos tan 0 0.0 1.0 0.0 10 0.1736 0.9848 0.1763 ... 350 -0.1736 0.9848 -0.1763 360 0.0 1.0 0.0 The last function is a value returning function that has the form “double ComputeSquareRoot(double num) and uses passing-by-value to communicate the values in which we need to compute its square root. This function does NOT use the standard sqrt() library function to compute the square root instead it uses the following approximation. The square root of a number, num, can be approximated by ýrepeatedly performing a calculation using the following ýformula:ý nextGuess = (lastGuess + (num / lastGuess)) / 2ý The initial guess can be any positive value (e.g., 1). This ývalue will be the starting value for lastGuess. If the ýdifference between nextGuess and lastGuess is less than a ývery small number, such as 0.0001, you can claim that ýnextGuess is the approximated square root of num. If not, ýnextGuess becomes the lastGuess and continue the ýapproximation process.ý Hint: this algoeithm can be implemented with while loop(s) Once the function is over, it will return the value of the computed square root to the calling module, main(). Also, main() is in charge of repeatedly calling the function to compute the square root for (0, 10, 20, 30, ….. 100) and put them in tabular format shown below. In addition, main() should display in the third column of that table the value of the computed square root for that given number but now using the sqrt library function found in the cmath header file. Num sqrt() ComputeSquareRoot 0 0.0 10 ... 90 100 10.0 Oh sweet lord. Don't worry I've got tme to do this, power of deductive reasoning all the way! Link to comment https://gtaforums.com/topic/434410-creating-and-modifying-a-user-defined-matrix/?do=findComment&comment=1059676069 Share on other sites More sharing options...
K^2 Posted December 8, 2009 Share Posted December 8, 2009 I have no idea what you even need to do on the first because I don't recognize the series. Do you have a formula or name for that series? The other two are pretty straight forward. It is just worded very poorly. For your second function, have it declared as something like double ComputeTrigonometrcValues(int degree, double &sinValue, double &cosValue). All your function needs to do is first compute another parameter: double angle=/*compute radians from degrees*/; and then set sinValue to sin(angle), cosValue to cos(angle), and finally return sinValue/cosValue. Pretty trivial. I'm sure you'll figure out what to do with your main() from here. Third function is a Newton's Method for computing a Square Root. Powerful stuff. Of course, he skips all the advanced algebra of it and gives you the formula. The idea is that the formula he provides holds true if both your nextGuess and lastGuess hold the square root value. So if you put anything for your lastGuess, your nextGuess will be a correction you need to make. Fortunately, this particular formula converges pretty fast. So you'll have your nextGuess almost equal to lastGuess, and therefore the square root in no time. Try to write this one. If you'll have trouble, I'll try to help you out. But at this point, I'd be just giving away the whole thing, and that won't be doing you any good. Link to comment https://gtaforums.com/topic/434410-creating-and-modifying-a-user-defined-matrix/?do=findComment&comment=1059676314 Share on other sites More sharing options...
Fozzy Fozborne Posted December 9, 2009 Author Share Posted December 9, 2009 (edited) Oh sorry about that, the first function is one that adds 1/2 to 2/3 to 3/4 ... basically i / i + 1 until i gets to 20. **scratch that, the first part is RTS'd. Now for the other two... Edited December 9, 2009 by Fozzy Fozborne Link to comment https://gtaforums.com/topic/434410-creating-and-modifying-a-user-defined-matrix/?do=findComment&comment=1059677853 Share on other sites More sharing options...
Recommended Posts