CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2011
    Posts
    10

    output to be displayed in 9x 9 matrix

    Hi community,

    I having trouble in displaying on of my result in 9 X 9 matrix which suppose to do so.

    I have commented on that line which suppose to produce the result.


    Code:
    #include <stdio.h>
    
    #include <stdafx.h>
    
    #include <iostream> 
    #include <ctime> 
    #include <cstdlib> 
    #include <math.h>
    #include <time.h>
    
    using namespace std;
    
    float RandomFunction(int x, int y)
    {
    
    	  srand( (unsigned)time( NULL ) );
    
    double xmax = 0.5;
    
    double xmin = -0.5;
    
    
    int S1 = 5,R = 9;
    
    float myarr[5][9];
    
    float Ab1;
    
    int c,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;
    
    
     
    	 } 
    };
    
    
    
    
    
    
    
    
     
    int main()
    {
    	int i, j;
    	float A[9][9] = {{196, 188, 248, 0, 0,232,25,255,32}, {35, 15, 42,223,255,255,255,25,35},
    	{234,236,222,224,205,231,45,205,34},{232,244,225,255,251,247,255,251,247},{59,44,40,0,0,38,15,10,38},
    	{244,228,226,255,251,246,25,25,24},{243,251,208,249,238,190,249,238,190},{57,48,35,255,233,236,55,53,36},
    	{226,230,234,235,240,250,235,240,250}};
       
        int m,n ;
    	float B[9][9] = {{208, 245, 248, 255, 255,237,25,27,24}, {16, 21, 52,255,255,255,208,237,241},
    	{235,45,218,243,211,231,21,35,36},{255,237,255,247,212,222,20,255,218},{44,13,55,22,28,12,34,15,54},
    	{229,231,245,255,27,255,234,243,222},{211,236,244,254,194,21,255,216,190},{34,55,56,255,255,245,34,10,15},
    	{247,234,277,219,244,255,21,244,238}};
    
    	int q,r;
    
    
    
       
        // print the whole array
        for(j = 0; j <= 8; j = j + 1)
        {
            for(i = 0; i <= 8; i = i + 1)
                printf("%.4f ", A[j][i]/256);
    		printf("\n");
         }
    
    	
    
    	 for(n = 0; n <= 8; n = n + 1)
        {
            for(m = 0; m <= 8; m = m + 1)
                printf("%.4f ", B[n][m]/256);
    		printf("\n");
    
    		     }
    
    
    
    	 getchar();
    
    
    	  RandomFunction(q,r);
    
    	//AB2 =  RandomFunction(q,r);
    
    
    
    	 
     
    
     getchar();
    
    
    };

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: output to be displayed in 9x 9 matrix

    Quote Originally Posted by stunner08 View Post
    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

  3. #3
    Join Date
    Sep 2011
    Posts
    10

    Re: output to be displayed in 9x 9 matrix

    Hi D_Drmmr

    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?

  4. #4
    Join Date
    Aug 2009
    Posts
    440

    Re: output to be displayed in 9x 9 matrix

    Think of srand() as starting the random number generator. It only needs to be done once. You get values from the generator by using rand().

  5. #5
    Join Date
    Sep 2011
    Posts
    10

    Re: output to be displayed in 9x 9 matrix

    Now i have arranged the RandomFunction code to be within the main function as shown as below:-


    Code:
    int main()
    {
    
    	 srand( (unsigned)time( NULL ) );
    	int i, j;
    	float A[9][9] = {{196, 188, 248, 0, 0,232,25,255,32}, {35, 15, 42,223,255,255,255,25,35},
    	{234,236,222,224,205,231,45,205,34},{232,244,225,255,251,247,255,251,247},{59,44,40,0,0,38,15,10,38},
    	{244,228,226,255,251,246,25,25,24},{243,251,208,249,238,190,249,238,190},{57,48,35,255,233,236,55,53,36},
    	{226,230,234,235,240,250,235,240,250}};
       
        int m,n ;
    	float B[9][9] = {{208, 245, 248, 255, 255,237,25,27,24}, {16, 21, 52,255,255,255,208,237,241},
    	{235,45,218,243,211,231,21,35,36},{255,237,255,247,212,222,20,255,218},{44,13,55,22,28,12,34,15,54},
    	{229,231,245,255,27,255,234,243,222},{211,236,244,254,194,21,255,216,190},{34,55,56,255,255,245,34,10,15},
    	{247,234,277,219,244,255,21,244,238}};
    
    	float Ab1;
    	int xmax = 2,xmin = -2,ymax = 2,ymin = -2;
    
    	float myarr[9][9];
       float z;
        // print the whole array
        for(j = 0; j <= 8; j = j + 1)
        {
            for(i = 0; i <= 8; i = i + 1)
                printf("%.4f ", A[j][i]/256);
    		printf("\n");
         }
    
    	
    
    	 for(n = 0; n <= 8; n = n + 1)
        {
            for(m = 0; m <= 8; m = m + 1)
                printf("%.4f ", B[n][m]/256);
    		printf("\n");
    
    		     }
    
    
    
    	 getchar();
    
    
    
    	 for (int S1;S1<=8;S1 = S1 + 1){
    
    		 for (int R;R<=8;R = R+1)
    			 	 myarr[S1][R] = (double) rand()/(double) RAND_MAX; 
    		z =  myarr[S1][R];
    		  Ab1= (xmax + z  *(xmin-xmax))/2;
    
    		   printf("%.4f\n",Ab1);
    
    	 } 
    but I'm getting this error 'error C2065: 'R' : undeclared identifier'

  6. #6
    Join Date
    Sep 2011
    Posts
    10

    Re: output to be displayed in 9x 9 matrix

    There was an error at the for loop too:-

    Code:
     for (int S1=0;S1<=8;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 + z  *(xmin-xmax))/2;
    
    		   printf("&#37;.4f\n",Ab1);
    
    	 }

    it was just only int S1,int R and supposed to be int S1=0,int R=0
    Last edited by stunner08; October 19th, 2011 at 02:07 AM.

  7. #7
    Join Date
    Sep 2011
    Posts
    10

    Re: output to be displayed in 9x 9 matrix

    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;		   
    		 
    		 }
    	 }
     for (int S=0;S<=4;S = S+1){
    
    		 for (int R1=0;R1<=8;R1 = R1+1){
    
    			 printf("%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f ",Ab1[S][R1]);
    
    			 printf("\n");
    		 }
     }
    But i'm getting the Ab1 results in just 1 column rather than 5 x9

  8. #8
    Join Date
    Sep 2011
    Posts
    10

    Re: output to be displayed in 9x 9 matrix

    Do i to do transposing of matrix?

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: output to be displayed in 9x 9 matrix

    Quote Originally Posted by stunner08 View Post
    Hi D_Drmmr

    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.
    Quote Originally Posted by stunner08 View Post
    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.
    Code:
     for (int S=0;S<=4;S = S+1){
    		 for (int R1=0;R1<=8;R1 = R1+1){
    			 printf("%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f ",Ab1[S][R1]);
    			 printf("\n");
    		 }
     }
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured