I have to create a random matrix m*n where m and n are user inputs.
Now i have to fill the matrix with random nos. Finally I must have a matrix where each value is either 0 , 1 , a fraction no between 0 and 1, and also sum of each column should be equal to 1. how should i create the matrix ??

thanks for help... :)

try randomize function header file stdlib

try randomize function header file stdlib

And why not use a flux capacitor? Everyone knows that it will produce the jiggawatts necessary to create a matrix.
I get it now; it is the plutonium, isn't?


@mindbender. Without showing a minimum of the effort you have putted into it, already, you are not going to get much help. Post what you have tried.

ok.. i have written the code

it is working fine with turbo c ++ and borland.. heres the code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
 int i,j,k,m,n;
 float **x,*y;
 int sum=0;

	  printf("enter row and column no\n");
	  scanf("%d%d",&m,&n);

	 x = (float **) malloc(m*sizeof(float));
	 for(i=0;i<m;i++)
	 x[i] = (float *) malloc(n*sizeof(float));

	 y = (float *) malloc(n*sizeof(float));

	 for(i=0;i<m;i++)
		{
		  for(j=0;j<n;j++)
			{
			x[i][j]=random(100);
			printf("%f\t",x[i][j]);

			}
			printf("\n");
	  }

	  printf("\n\n");

	  for(i=0;i<n;i++)
		 {
		 sum=0;
		  for(j=0;j<m;j++)
			 {
			  sum = sum + x[j][i];
			 }
			 y[i]=sum;
			 printf("%f\t",y[i]);

		  }

		for(i=0;i<m;i++)
		  {
			 for(j=0;j<n;j++)
			 {
			  x[i][j] = x[i][j]/y[j];
			  }
			}

	  printf("\n\n");

	 for(i=0;i<m;i++)
		  {
			 for(j=0;j<n;j++)
			 {
			  printf("%f\t",x[i][j]);
			  }
			  printf("\n");
			}
 getch();
 }

now the problem is that i m working in microsoft VC++ 2008 express edition.. and when i m running the same code..it says for random() identifier not found.. now what is that?? i guess now i will get some help.. thanks...

It doesn't work because you're using nonstandard code written for ancient compilers.

  • conio.h should be removed from your code.
  • main() by standard requires a return value(except in very rare cases).
  • And read the site rules: use CODE TAGS, because it keeps your code formatted and possible to read.

mindbender> says for random() identifier not found.. now what is that??
Since you have stdlib.h included already, substitute random() for the standard C function rand()

MosaicFuneral> conio.h should be removed from your code.
Only if getch() gets eliminated or replaced by a standard way of holding the stdin hostage, so stdout do your bidding.

Simply define your own random function with same functionality as Borland random:

int random(int n)
{
    return rand() % n;
}

That's all.

And why not use a flux capacitor?

42.

Also, I missed something here, because I was going to answer "You use the malloc function to dynamically allocate the array's memory. ." or something along those lines. Anyone care to clarify?

The whole code looks a bit shaky to be honest...
If you're going to use rand() you need to seed the random generator first with srand(). Here's how to do it.

Also: If you use malloc to allocate memory, be sure to free() it before your program exits. If not: memoryleak :(

Agreed. There is also some order to which you need to malloc if I remember correctly. I don't remember the order though -- outermost dimensions [of an array] first? I'll look it up.

we have encountered error as" too many arguments to function random while running the program in linux os.

we have encountered error as" too many arguments to function random while running the program in linux os.

Try reading some of the replies in this thread. :icon_frown:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.