Allocazione dinamica di una matrice

Creiamo una matrice rettangolare di numeri casuali compresi tra 1 e 90. La matrice sarà allocata dinamicamente

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int n,m;
float **matrice;
float *riga;
int i,j;
srand(time(NULL));
printf("Inserisci N:");scanf("%d",&n);
printf("Inserisci M:");scanf("%d",&m);
matrice=malloc(n*sizeof(float *));
for(i=0;i<n;i++)
{
*(matrice+i)=malloc(m*sizeof(float));
}
for(i=0;i<n;i++)
{
  riga=*(matrice+i);
  for(j=0;j<m;j++)
  {
    *(riga+j)=1+rand()%90;
  }
}
for(i=0;i<n;i++)
{
  riga=*(matrice+i);
  for(j=0;j<m;j++)
  {
    printf("%3.2f\t",*(riga+j));
  }
  printf("\n");
}
return 0;
}

Download mat_din
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s