How to calculate immediately for three N?

#include <stdio.h> #include<math.h> #include <stdlib.h> #include <sys/time.h> main() { int N=10; //int N[3]; // N[1]=10; // N[2]=12; // N[3]=14; struct timeval start_tv,end_tv; int i,j,n,k; double A[n][n],B[n][n],C[n][n]; double s; gettimeofday(&start_tv, NULL); // for( int z=1;z<3;z++) // { n=N[z]; for( i=1;i<=n;i++) { for( j=1;j<=n;j++) { A[i][j]=i; B[i][j]=1.0/j; } } printf("Massiv Đ’â„–1:\n"); for ( i=1;i<=n;i++) { for ( j=1;j<=n;j++) printf("%.4f\t",A[i][j]); printf("\n"); } printf("Massiv Đ’â„–2:\n"); for ( i=1;i<=n;i++) { for ( j=1;j<=n;j++) printf("%.4f\t",B[i][j]); printf("\n"); } for (i=1; i<=n; i++) for (j=1; j<=n; j++) { C[i][j]=0; for (k=1; k<=n; k++) { C[i][j]+=A[i][k]*B[k][j]; } } printf("Massiv C:\n"); for ( i=1;i<=n;i++) { for ( j=1;j<=n;j++) printf("%.3f\t",C[i][j]); printf("\n"); } gettimeofday(&end_tv, NULL); printf("The corner elements:\n"); printf("\na[1][1] - %f\n",C[1][1]); printf("\na[1][n] - %f\n",C[1][n]); printf("\na[n][1] - %f\n",C[n][1]); printf("\na[n][n] - %f\n",C[n][n]); s = (double)end_tv.tv_sec+(double)end_tv.tv_usec/1000000.0 - (double)start_tv.tv_sec - (double)start_tv.tv_usec/1000000.0; s = (2.0*(double)n*n*n)/s; printf("\n Perform matrix multiplication - %f / per sec\n", s); return 0; } 
  • Very interesting. What is the question? - VladD
  • @nick: What does “not work” mean? Not compiled? And for just two N works? - VladD
  • Does not compile, but considers? O_O - VladD
  • Oh, better already. And what kind of mistake? By the way, will it be easier for you to bring the code into a readable state? (And throw out the parts that are not necessary for understanding the problem.) - VladD
  • @nick: Good. But in this code there is no variable or constant with the name N (there is only n = 10 ). And do you think we need to figure out how you changed the code so that it stops compiling? We here not telepathists though we try. - VladD

2 answers 2

Well, to write (for gcc) such a multiplication program is not a problem (it will even compile)

  int N[3] = {30, 700, 2500}, iN; for (iN = 0; iN < 3; iN++) { int i, j, k, n = N[iN]; double A[n][n], B[n][n], C[n][n]; .... for (i = 0; i < n; i++) for (j = 0; j < n; j++) { C[i][j] = 0; for (k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j]; } .... } 

only for n == 2500 these matrices will not fit in the standard stack size (it will have to be increased).

    @ nick :

    So. Everything is very bad. You urgently need to read any book on the basics of C ++, at least two times.

     int N[3]; N[1]=10; N[2]=12; N[3]=14; 

    In C ++, arrays are numbered from zero, which means that with a size of 3, the available indices are 0, 1, and 2. When attempting to write to indices 3, the element is written “where God will send”

     int i,j,n,k; double A[n][n],B[n][n],C[n][n]; 

    How does this compile at all? What is n at this point? Answer: it is not clear what. In addition, the size of the arrays must be constants.

     for( i=1;i<=n;i++) 

    Arrays are numbered from 0 to length minus 1. Urgently for a book!

    @ nickname : the desire of the person who set the task does not cancel the rules of C / C ++: arrays are numbered from zero, so it is illegal to refer to the n element. You can not give a damn about the exact task and put B[i - 1][j - 1] = 1.0 / j; for i , j from 1 to n , if this is more convenient for you. (That is, when accessing an array, all the time to adjust the indices.)

    • Thank you very much! The code works. - Nick