![]() |
|
#1
|
|||
|
|||
Help with matrix and string combinationI have been struggling with this program for a few days and having a hard time getting started. My assignment is listed below: Any help would be apriciated.
Write a C++ program that prompts the user with the following options: Matrix String Quit • If the user selects “M” or “m” then call a user-defined function called double average (void) that will the following: a. Prompt the user to enter three sets of five numbers (type float) and store the numbers in a 3 x 3 array. b. Determine the largest value of 9 values and print it in the main function. c. Determine the transpose of the original matrix and then find the sum of the original and transpose matrices and print the result in this function. • If the user selects “S” or “s” then call a user-defined function void rev_str (void) that a. Prompts the user to enter a string and store it into one-dimensional array. b. Replace the content of the string with the string reversed and save into another array. c. Print both the original string and reversed one in this function. • If the user enters “Q” or “q” then you should terminate the program. If the user enters any other selection it should prompts the user invalid selection and try again. Note: Do not use any global arrays or variables. Here is the program I have so far : CPP / C++ / C Code:
Last edited by LuciWiz : 25-Oct-2005 at 12:28.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
||||
|
||||
Re: Help with matrix and string combinationHi bco,
I dont understand this part: Quote:
OK. First, we should declare the user defined functions double average() and void rev_str(); So add those before the main program. Do you know about twodimensional arrays? if not, visit this link: Multi dimensional arrays How to determine maximum value from a two dimensional array? Just use two for loops and an if statement. here is a prototype: CPP / C++ / C Code:
make maxvalue equal to the first value in the array. then check whether maxvalue is lesser than the next element in the array. If it is lesser, the next element is the maxvalue in the array. So assign the value of the next element to maxvalue. Continue till you reach the end of the array. to transpose the matrix, you can do like this: consider an array: 1 2 3 4 5 6 7 8 9 So for transposing, the output should be: 1 4 7 2 5 8 3 6 9. So just swap the 0,x position value with x,0 position values. i.e swap 0,1 position to 1,0 position swap 0,2 position with 2,0 position go on... To find the sum of matrices, just add the corresponding elements in a for loop similar to the previous kind and store it in a new array. To get a string from a onedimensional array, use character array like this: char str[20]; Then get the input. To reverse the string: create another character array str2[20]. then check the size of the original str string. You can calculate it by finding where '/0' character occurs. so use a for loop. increment length until str[i] equals NULL. Next, to reverse an array: Once you determine the size, then it is easy. the last charater of str will be the first character of str2. i.e str[size-1] will be equal to str2[0] and so on. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: Help with matrix and string combinationYou've been asked many times to use code tags (defined in Guideline #1) and once explicitly to read the Guidelines. I'm sure LuciWiz is getting tired of editing your posts for you.
Concentrate on Guideline #1, #2 and #3. Explain your problem, don't let us try to figure it out -- it's your program, not ours. __________________
Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with. |
|
#4
|
|||
|
|||
Re: Help with matrix and string combinationParamesh,
Thanks for responding. Quote:
I believe what I need to do is tell the user to enter three sets of five floating point numbers then store them in a 3 X 3 array. bco1109 |
|
#5
|
|||
|
|||
Re: Help with matrix and string combinationQuote:
Huh? three sets of five numbers ==> 15 numbers 3x3 array ==> nine places to put the numbers A good trick if you can do it. Are you sure that is the assignment? Regards, Dave |
|
#6
|
|||
|
|||
Re: Help with matrix and string combinationI am I headed in the right direction?
CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
Re: Help with matrix and string combinationQuote:
Dave, I believe I need to store the largest of the 9 values in the array. bco |
|
#8
|
|||
|
|||
Re: Help with matrix and string combinationQuote:
That's not in the instructions that you wrote: Quote:
That clearly won't work. Let's suppose that you miscopied the assignment and it is supposed to be something like a. Prompt the user to enter three sets of three numbers (type float) and store the numbers in a 3 x 3 array. Then at least the rest of the assignment makes sense. Quote:
I can't tell what your direction is, and I don't believe I should give an opinion or value judgment based on incomplete knowledge. I'll tell you how I might approach the assignment. I see three parts: 1. Get the user selection. Make a loop that asks for a choice and calls one of two functions based on that choice. Repeat the loop until the user enters 'q'. 2. Implement a function that gets some numbers from the user into a matrix and performs certain manipulations: find the maximum value of the matrix elements, transpose the matrix, add the original and transposed matrices, print the resulting matrix. 3. Implement a function that gets a string from the user. Reverse the string and print the original and reversed string. What I would not do is to type 78 lines of something and then throw it into the compiler to see what happens. (Maybe I'll get lucky some day and this will work, but I kind of doubt it --- it hasn't happened yet, at least for me.) Now, I'm not trying to give you a hard time, but I would like to see people approach programming and other problems with some sort of methodology. One methodology is called top-down design. Now, when people talk about the glories of top-down design they usually don't mention that the only reasonable way to do top-down design is if you have some experience, not only in problem solving, but specifically have experience in that particular field (programming, in this case). We sometimes forget that no one is born knowing this stuff; we learn by experience. And we get experience in solving problems by: solving problems. (Catch-22, as people of the older generation sometimes call it.) Well, here are some hints. After the program specification is complete (and I have written it down --- with pencil and paper or with a text editor), I would follow the steps in the order that I wrote above. If the problem assign is complete (and logical) the program specification is pretty much all done. Just make sure that you understand it. Note that each step (or some of the steps) might be broken down into smaller steps: Step 2 could be 2a. Ask for and get user input into the 3x3 array 2b. Find the maximum value of the elements of the 3x3 array 2c. Calculate another matrix that is the transpose of the first matrix 2d. Calculate another matrix that is the sum of the original and transposed matrices. 2e. Print the values of the matrix elements. These steps could be all in one function, or some of them could be contained in separate functions. The point is, that I would implement (write the code for) each step and test it thoroughly before going go the next step. Why would I waste my time figuring out how to add two matrices if I am not sure that the code to form the matrices is correct? Anyhow, back to the top. I have taken the steps you used to get the user's choice and removed everything else in your last post. Note the comment at the beginning. This code won't do what you hoped it would. That's why we test it first. Note that instead of writing real functions for average() and rev_str(), I just put in some stubs that let me make sure the program flow is correct before I start complicating things. CPP / C++ / C Code:
Now, try it: (stand by the ctrl-c key to abort the infinite loop. Compile and execut it. Enter various things at the prompt. Why did it go into infinite loops? There have been several threads on this topic lately, you can search the board. There are tutorials on the use of cin with char variables. Look for them. If you are still stuck, come back and ask for help. Regards, Dave "We can face our problem. We can arrange such facts as we have with order and method." ---Hercule Poroit Murder on the Orient Express |
|
#9
|
|||
|
|||
Re: Help with matrix and string combinationDave,
Thanks for the advice. I was trying to write the progam at once instead of steps. I lloked for post on infinite loops and could not find the answer as to why the program infinite loops. I also don't know what to write in the void function and what to write in the main function. thanks bco |
|
#10
|
|||
|
|||
Re: Help with matrix and string combinationQuote:
Well, I shouldn't have suggested you look for posts. I don't know what got into me; there are too many ways to get infinite loops. I should have suggested you look at the code: (Remember, I am using your code from your post; I told you it wouldn't work, right?) Here is a summary of the program flow: Code:
See the problem? You need to move the user prompt and user input inside the loop. That's all. CPP / C++ / C Code:
You might want to rework the way you decide to print the error message, but at least it's not an infinite loop now. Quote:
I don't know either. Are you referring to these: CPP / C++ / C Code:
These are function prototypes. The C++ compiler starts at the top of the file and works its way down. It needs to know certain things about functions when they are used. In this main program, the functions average() and rev_str() are invoked (called) before the code for them appears in the file. By telling the compiler what type the function is (what it returns) and what its arguments are (void means no arguments), compilation is possible. Later on in the file the actual code for the functions defines the operations each function performs. If the code for the functions had appeared at the top of the file (before they were invoked in main()), the prototypes wouldn't have been necessary, since the compiler would already know about them before your program used them. I like to put my main() program first, (in keeping with my top-down methodology). It's not neccessary to do so, but that's my style. Therefore, function prototypes are necessary. Here are the actual function definitions. Since both protoypes specified that the functions don't have arguments, nothing has to go in the parentheses. CPP / C++ / C Code:
These rules apply to C and C++ programs: The compiler needs to know the return type of function and what type arguments the function has at the time that the function is invoked in a program. It doesn't have to know exactly what they to until the entire program has been compiled. One final note: the main() function is a very special function, and never has to be prototyped (you never call it from another function, so it never needs a prototype). There is another way to declare the main() program function that has arguments that allow the program to read things from the command line that caused the program to be executed in the first place. Let's save That for later. Regards, Dave Last edited by davekw7x : 25-Oct-2005 at 19:08.
|
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The