AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

N dimension array - C

Go down

N dimension array - C

Post by Lucaci Andrei on Thu Mar 22, 2012 10:09 pm

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

int main(){
int **** a;
int m, n, p, q, sum=0, i, j, k, l;
printf ("Dimensions (4 numbers separated by space): \n");
scanf ("%d %d %d %d",&m, &n, &p, &q);
printf ("\n");
a=(int****) malloc(m*sizeof(int***));
for (i=0; i<m; i++){
a[i]=(int***)malloc(n*sizeof(int**));
for (j=0; j<n; j++){
a[i][j]=(int**)malloc(p*sizeof(int*));
for (k=0; k<p; k++){
a[i][j][k]=(int*)malloc(q*sizeof (int));
for (l=0;l<q;l++){
a[i][j][k][l]=1;
sum+=a[i][j][k][l];}}}}
for (i=0; i<m; i++){
for (j=0; j<n; j++){
for (k=0; k<p; k++){
free (a[i][j][k]);
}
free (a[i][j]);
}
free (a[i]);
}
free (a);
printf ("Sum is %d", sum);
printf ("\n\n");
return 0;
}


avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 25
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum