Sorry, I meant m<n give troubles!
Sorry, I meant m<n give troubles!
Below, your main with corrections and remarks. Personnally I would
avoid having the function M called recursively by itself. The called
functions don't return a value until m=1 is reached. This could lead
to many threads running simultaneously, I think. There are several
solutions. A for loop could be used:
int answerm=1;
for(int i=2; i<=m; i++)
answerm *=i;
Another remark: if n<m, you get in serious troubles!
#include <iostream>
using namespace std;
int M(int m);
int N(int n);
int X(int x);
int main()
{
int m,n,x,c;
//cout << "Please enter value of M: "<<M(m)<<endl; As M(m) has
//not yet been calculated, it cannot be printed
cout << "Please enter value of M: "<<endl;
cin>>m;
cout << "Please enter value of N: "<<endl;
cin>>n;
x=(m-n);
//c=(M(m)/(N(n)*X(x)); there is an extra (
c=M(m)/(N(n)*X(x));
cout << "Calculated value of C is :"<<c<<endl;
//why 3 times the same function? M, N, and X are absolutely identical.
//You could simply write
c=M(m)/(M(n)*M(x));
cout << c << endl;
return 0;
I would think that most modern compilers generate flat 32-bit code, don't they?. An int occupies 32 bits of memory. To see that, declare an int array, for example int a[3] and watch &a, &a[1] and &a[2] during debugging.
Here is the disassembly of the code generated by the visual studio compiler for your snippets above:
int main()
{
01171350 push ebp
01171351 mov ebp,esp
01171353 sub esp,0D8h
01171359 push ebx
0117135A push esi
0117135B push edi
0117135C lea edi,[ebp-0D8h]
01171362 mov ecx,36h
01171367 mov eax,0CCCCCCCCh
0117136C rep stos dword ptr es:[edi]
int adad=12345;
0117136E mov dword ptr [adad],3039h
char character;
character=*((char *)(&adad)+2);
01171375 mov al,byte ptr [ebp-6]
01171378 mov byte ptr [character],al
return 0;
This is probably not of much help.
As 12345 = 3039h, its first (most significant) two bytes are zero, so that character = 0. With adad = 1234567 = 12d687h, you get character = 18 = 12h, as expected.
Books and tutorials are of great help. Which ones to advise depend on your programming experience. See thread 1 C++ books here and the section C programming language of amazon.com where excerpts are available. See also http://msdn.microsoft.com/en-us/beginner/cc305129.aspx.
This is due to the processor precision during the computations. With float variables, it is about 7 digits. See http://www.cplusplus.com/doc/tutorial/variables/. At the processor level, the binary substraction of a constant from a variable is not performed in exactly the same way as the substraction of a variable from another variable. To prove this define a new variable
float b=0.2;
and replace the instruction
a[i][j]=a[i][j]-0.2;
by
a[i][j]=a[i][j]-b;
You can also use
while ( !(choice == 'D' || choice == 'R' || choice == 'd' || choice == 'r') )
Again I agree that blackrainbowhun's version with an error flag is easier to read.
Programming can be fun; so if you want to do it by yourself, you could use structure arrays and replace each row of your matrix by an instance of a structure. An advantage: the rows can have different lengths. The comments and additions to your program shown below are for a public matrix, so that I didn't use the private variables rowNumber and colNumber.
#include<iostream>
using namespace std;
const int maxrownbr = 100; //maximum row number of matrices used in your program
class Matrix
{
public:
Matrix();
Matrix(int r, int c);
struct row //defines a structure array: a structure instance for
{ //each matrix row
double *matelt;
};
private:
int rowNumber; //number of row in the matrix
int colNumber; //number of columns in the matrix
};
Matrix::Matrix()
{
rowNumber=0;
colNumber=0;
}
Matrix::Matrix(int r, int c)
{
struct row rowinst[maxrownbr]; //defining enough structure instances
for( int irow=0; irow<r; irow++ ) //to allocate one for each row
rowinst[irow].matelt = new double[c]; //each row is a one-dimensional array:
//memory allocation
for( int i=0; i<r; i++ ) //filling the matrix for testing
{
for( int j=0; j<c; j++ )
{
rowinst[i].matelt[j] = c*i + j;
cout << rowinst[i].matelt[j] << " ";
}
cout << endl;
}
rowNumber=r;
colNumber=c;
}
int main()
{
int r=2;
int c=3;
Matrix::Matrix(r,c);
return 0;
}
A and condition returns false when any one of the propositions is false. Therefore, if choice == d for instance, the proposition choice != d is false as well as the whole condition and the while loop will not be repeated. Of course, the solution using an error flag is easier to understand. Maybe it's the one I would use myself, but I tried to stay close to your version of the program. Of course I checked the solution I sent you and it seems to work.
A do while loop is more appropriate to your problem than a while loop, since then the condition is checked at the end of the loop. Of course, this is a detail.
/*There are several possibilities. Here is one.*/
#include <iostream>
using namespace std;
int main () {
double input_x, number;
cout << "Insert value: ";
cin >> input_x;
char choice ;
//choice = 'D', 'R', 'd', 'r'; I don't understand the purpose of this instruction
choice = 'e'; //Initialize choice so that it be incorrect
while ( choice != 'D' && choice != 'R' && choice != 'd' && choice != 'r' ) {
//while no correct choice has been made
/********** The while loop must start here so that, in case of error,
a second choice can be made **************/
cout << "Insert choice: ";
cin >> choice;
//while ( //what suppose i write here?? )
if ( choice == 'D' || choice == 'd' ) {
number = input_x * 3.142 / 180;
}
else if ( choice == 'R' || choice == 'r') {
number = input_x;
}
else {
cout << "Invalid input!" ;
}
}
cout << "The number in radian is: " << number << endl;
}