I having trouble in displaying on of my result in 9 X 9 matrix which suppose to do so.
You should learn to properly indent your code, then the problem becomes much easier to spot.
Here's the relevant part of the code properly indented:
Code:
float RandomFunction(int x, int y)
{
srand( (unsigned)time( NULL ) );
double xmax = 0.5;
double xmin = -0.5;
int S1 = 5;
int R = 9;
float myarr[5][9];
loat Ab1;
int c;
int d;
float z;
for(int S1 = 0; S1 <= 4; S1=S1 +1)
{
for(int R = 0;R <= 8;R = R + 1)
myarr[S1][R] = (double) rand()/(double) RAND_MAX;
z = myarr[S1][R];
Ab1 = (xmax + y *(xmin-xmax))/2;
//print out the values:
printf("Ab1:%.4f ", Ab1);//this part will display only 1 result but I want it to be displayed in 9 x 9 matrix
printf("\n");
return Ab1;
}
};
As you can see, the variable R declared in the inner loop goes out of scope after that loop. Therefore, in the line "z = myarr[S1][R];" the variable R refers to the one declared at the beginning of the function. Also, you return from inside the (outer) loop, so it will only run a single iteration.
A few more hints:
- Only call srand once, at the beginning of your program. Do not call it inside a function other than main.
- Define each variable on a single line. Don't define variables before you need them, i.e. before you can give them a proper initial value. This would have prevented the error you have now.
- Always use curly brackets for the body of an if, else or loop, even if the body is just a single statement.
- Prefer to use C++ streams instead of the printf family of functions. These C functions are not type safe, which can cause major headaches in real programs.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Ur saying that the srand function can be called only in the main and not inside other functions?
Then how will i get the random numbers if the srand function is outside the RandomFunction()?
Do I have to get the random numbers by including it also in the main?
srand can be called from anywhere. However, usually you only want to call it once at the start of the program.
rand() and srand() remember their state internally. You don't need to call srand before you call rand, but if you don't you will get the same sequence of 'random' numbers every time you run the program. That's not really random and that's why you seed the random number generator with a value based on the current time. But you only need to do that once every time you run your program. Hence, a logical place to call srand is at the beginning of main.
Originally Posted by stunner08
Now I have totally changed the for loop as shown below:-
Code:
for (int S1=0;S1<=4;S1++){
for (int R=0;R<=8;R = R++){
myarr[5][9] = (float) rand()/(float) RAND_MAX;
Ab1 [S1][R]= (xmax + myarr[5][9]*(xmin-xmax))/2;
}
}
The size of myarr is 9 x 9. Therefore, valid indices are [i][j], where both i and j are in {0,...,8}. myarr[5][9] will actually be myarr[6][0] in this case.
You need to provide the correct number and type of arguments to printf, in accordance with the format string you provide. Your format string states 9 floats, but you only pass one. That's undefined behavior. Like I said, stop using printf and use std::cout instead.
It's not clear to me what you want to do. Your code already includes code to print both A and B matrices, so how come you can't figure out how to do it for myarr. Perhaps it would help to write a function to print any of your matrices, like so
Code:
#include <iostream> // std::cout
void PrintMatrix(float matrix[9][9])
{
// you fill this in
}
int main()
{
float test[9][9] = { { ... }, ... }; // fill in some numbers
std::cout << "Value of test matrix:" << std::endl;
PrintMatrix(test);
}
Try to make this program work correctly first.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Bookmarks