GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 5.00 average.
  #1  
Old 25-Oct-2005, 12:26
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road
Question

Help with matrix and string combination


I 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:
#include <iostream.h>
#include <math.h>  b
#include<stdio.h>
#include<conio.h>
#include<assert.h>
//function main begins program execution
double average ();

int main() 
{																					
	 float num1, num2, num3, num4, num5;		
	 float num6, num7, num8, num9, num10;		
	 float num11, num12, num13, num14, num15;			
  	 char Type;														
	 cout << "Please enter 'm' or 's' to proceed\n";	
	 cout << "Please enter 'q'' to quit\n";
	 cout << "and hit enter\n";					
	 cin>> Type;						
	 do				
	 {							
			if((Type =='m')||(Type =='M'))		
			{					
				int arr [3][3];
				cout <<"The number you entered is:"<< endl;						
			}					
			else if((Type == 's')||(Type== 'S'))                         			
			{					
			}
			else 
			{					
				cout << "invalid entry" << endl;	
	 }while ((Type !='q')&&(Type!='Q'));							
	 cout << "To terminate program" << endl;						
	 }
			double average (void)			
			{
				float maximum;
				cout << "Enter three sets of 5 numbers" << endl;							
				cout << "Enter first set";
					cin >>	num1>> num2>> num3>> num4>> num5;
				cout << "Enter second set";
					cin >> num6>> num7>> num8>> num9>> num10;
				cout << "Enter third set";
					cin >> num11>> num12>> num13>> num14>> num15;
			
			}
}
Last edited by LuciWiz : 25-Oct-2005 at 12:28. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 25-Oct-2005, 13:01
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Help with matrix and string combination


Hi bco,

I dont understand this part:
Quote:
a. Prompt the user to enter three sets of five numbers (type float) and store the numbers in a 3 x 3 array.
Please explain them in detail.


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:
float arr[3][3];
//get the input using for loops
float maxvalue = arr[0][0];   //assign the first value of array to max value.
for(int i = 0; i < 3; i++)
{
   for( int j = 0; j < 3; j++)
   {
      if( max < arr[i][j] )
      maxvalue = arr[i][j];
   }
}
This program follows this logic:
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  
Old 25-Oct-2005, 13:02
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: Help with matrix and string combination


You'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  
Old 25-Oct-2005, 13:22
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

Re: Help with matrix and string combination


Paramesh,

Thanks for responding.
Quote:
a. Prompt the user to enter three sets of five numbers (type float) and store the numbers in a 3 x 3 array.

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  
Old 25-Oct-2005, 13:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with matrix and string combination


Quote:
Originally Posted by bco1109
Paramesh,

Thanks for responding.


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

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  
Old 25-Oct-2005, 13:41
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

Re: Help with matrix and string combination


I am I headed in the right direction?
CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>  b
#include<stdio.h>
#include<conio.h>
#include<assert.h>
//function main begins program execution
double average (void);
	void rev_str (void);

int main() 
{																					
	 float num1, num2, num3, num4, num5;																
	 float num6, num7, num8, num9, num10;																	//calculation variables
	 float num11, num12, num13, num14, num15;															//item to be input by user
  	 char Type;																		//item to be input by user
	 cout << "Please enter 'm' or 's' to proceed\n";	
	 cout << "Please enter 'q'' to quit\n";											//prompt
	 cout << "and hit enter\n";														//prompt
	 cin>> Type;																	//read type
	 do																				//begining of do while loop
	 {																				//begining of do while loop																			//
			if((Type =='m')||(Type =='M'))											//begining of first if else ststement
			{																		//begining of first if else ststement
				int arr [3][3];
				cout <<"The number you entered is:"<< endl;						
			}																		//end of first if statement
			else if((Type == 's')||(Type== 'S'))                                    //begining of else statement       			
			{																	    //begining of else statement
			}
			else 
			{																		//break out of the loop
				cout << "invalid entry" << endl;								    //print invalid enttry                                
																					//end of if statement																		
	 }while ((Type !='q')&&(Type!='Q'));											//while loop
	 cout << "To terminate program" << endl;										
	 }
			double average (void)

			{
				float maxvalue = arr[3][3];
				float maximum;
				cout << "Enter three sets of 5 numbers" << endl;					
				cout << "Enter first set";
					cin >>	num1>> num2>> num3>> num4>> num5;
				cout << "Enter second set";
					cin >> num6>> num7>> num8>> num9>> num10;
				cout << "Enter third set";
					cin >> num11>> num12>> num13>> num14>> num15;

						for(int i = 0; i < 99; i++)b
						{
							for( int j = 0; j < 99; j++)
							{
								if( max < arr[i][j] )
									maxvalue = arr[i][j];
								{
			void rev_str (void)
					const int max = 100; 
    	char str[max]; 
    	int count = 0;
    	int i = 0;
    	
    	cout << "Enter a string:\n";
    	cin.getline(str,max,'\n'); 
    	
    	while(str[count] != '\0')
    		count++; 
    	
    	for(i = count; i >=0; i--) 


        	{
        		cout << str[i]; 
        	}
			}

			return 0;
}
  #7  
Old 25-Oct-2005, 13:44
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

Re: Help with matrix and string combination


Quote:
Originally Posted by davekw7x
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

Dave,

I believe I need to store the largest of the 9 values in the array.

bco
  #8  
Old 25-Oct-2005, 14:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with matrix and string combination


Quote:
Originally Posted by bco1109
Dave,

I believe I need to store the largest of the 9 values in the array.

bco

That's not in the instructions that you wrote:

Quote:

a. Prompt the user to enter three sets of five numbers (type float) and store the numbers in a 3 x 3 array.


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:
Originally Posted by bco1109
I am I headed in the right direction?

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:
// Note that this program works OK if the user enters q or Q.
// For everything else, the program goes into an infinite loop
// This does not harm your computer, but it doesn't do anything
// worth while either. (Hit ctrl-c to stop the scrolling maddness.)
//
//
#include <iostream>

using namespace std;

int main()
{
  double average(void);
  void rev_str(void);

  double average_value;


  char Type;

  cout << "Please enter 'm' or 's' to proceed." << endl;
  cout << "Please enter 'q'' to quit." << endl;

  cin>> Type;

   do {
      if((Type =='m')||(Type =='M')) {
        cout << "mmmm: you entered " << Type << endl
             << "Here is where I will call the function 'average()'" 
             << endl << endl;
        average_value = average();
        cout << "average_value = " << average_value << endl << endl;

      }
      else if((Type == 's')||(Type== 'S')) {
        cout <<"ssss: you entered " << Type << endl
             << "Here is where I will call the function 'rev_str()'" 
             << endl << endl;
        rev_str();
        cout << endl << endl;
      }
      else {
        cout << "Not an mmmmm; not an sssss; you entered " << Type << endl
             << endl << endl;
      }
   } while ((Type !='q')&&(Type!='Q'));
}

double average()
{
  cout << "This is the function 'average()'" << endl;
  return 1.0; // have to return something
}


void rev_str()
{
  cout << "This is the function 'rev_str()'" << endl;
}


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  
Old 25-Oct-2005, 15:55
bco1109 bco1109 is offline
New Member
 
Join Date: Oct 2005
Posts: 12
bco1109 is on a distinguished road

Re: Help with matrix and string combination


Dave,

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  
Old 25-Oct-2005, 17:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with matrix and string combination


Quote:
Originally Posted by bco1109
Dave,

I lloked for post on infinite loops and could not find the answer as to why the program infinite loops.

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:
1. Prompt the user for a menu choice, and get the user input into a char variable, Type 2. Start a loop: if the input was 'm' do something else if the input was 's' do something else else print the "not an ..." message repeat the loop until the value of the variable Type is either 'q' or 'Q'

See the problem? You need to move the user prompt and user input inside the loop. That's all.

CPP / C++ / C Code:
  char Type;

  do {
      cout << "Please enter 'm' or 's' to proceed." << endl;
      cout << "Please enter 'q' to quit." << endl;

      cin >> Type;

      if((Type =='m')||(Type =='M')) {
        cout << "mmmm: you entered " << Type << endl
..
..
..  
..
   } while ((Type !='q')&&(Type!='Q'))

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:
Originally Posted by bco1109
I also don't know what to write in the void function and what to write in the main function

I don't know either.

Are you referring to these:

CPP / C++ / C Code:
int main()
{
  double average(void);
  void rev_str(void);


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:
double average()
{
  cout << "This is the function 'average()'" << endl;
  return 1.0; // have to return something
}


void rev_str()
{
  cout << "This is the function 'rev_str()'" << endl;
}

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 GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.