1

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... :)

8
Contributors
11
Replies
17
Views
8 Years
Discussion Span
Last Post by Nick Evan
Featured Replies
  • It doesn't work because you're using nonstandard code written for ancient compilers. [list][*][I]conio.h[/I] 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.[/list] Read More

  • 1

    [QUOTE=joolie09;1102094]we have encountered error as" too many arguments to function random while running the program in linux os.[/QUOTE] Try reading some of the replies in this thread. :icon_frown: Read More

0

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.

0

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...

2

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.
0

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.

0

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

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

That's all.

0

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?

0

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 :(

0

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.

-1

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

Edited by Nick Evan: Removed qouted &amp; unformatted code. Removed extra line numbers

1

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:

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.