Matrix Multiplication

Hello,

I have a program matrix multiplication but it work not for big arrays.
I dont understood what is problem.Anyone can help me? n should be bis 200.

#include <iostream>
#include <time.h>
#include <cstdlib>

using namespace std;

void matrixMatrix ( const int n, const double* A, const double* B, double* C ){

for(int i=0;i<n;i++){
for (int j=0;j<n;j++){
C[i*n+j]=0;
}
}

for(int i=0;i<n;i++){
for (int j=0;j<n;j++){
for(int k=0;k<n;k++){
C[i*n+j]+= A[i*n+k]*B[k*n+j];
}
}
}

for(int i=0;i<n;i++){
for (int j=0;j<n;j++){
cout<<C[i*n+j];
cout<<" ";
}
cout<<endl;
}

}

int main(int argc, char* argv[]){

int n;
n=100;

double* A = new double[n*n];
double* B = new double[n*n];
double* C = new double[n*n];


cout<<"Matrix A :";
for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {
A[i*n+j] = rand()% 100 + 1;
cout << A[i*n+j];
}
cout << endl;
}

cout<<"Matrix B :";
for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {
B[i*n+j] = rand()% 100 + 1;
cout << B[i*n+j];
}
cout << endl;
}

clock_t start3 = clock();
matrixMatrix(n,A,B,C);
clock_t finish3 = clock();

//deallocate
delete[] A;
delete[] B;
delete[] C;


return 0;
}

What do you mean by it doesn't work? Does it crash? Does it calculate the wrong result?
sorry sorry ı have false code wrote.I give you my example again.İf I n=100 write that work, but n=200 write give me compiler error "Segmentation fault (core dumped)"
new code :

PS : results are true..

#include <iostream>
#include <time.h>
#include <cstdlib>

using namespace std;

void matrixVector ( const int n, const double* A, const double* x, double* y ){

for(int i = 0; i < n; i++){
y[i*n] = 0.;
for(int j = 0; j < n; j++){
y[i*n] += A[i*n+j]*x[j*n];
}
}

/*for(int i = 0; i < n; i++){
cout << y[i*n] << " ";
}*/

}


int main(int argc, char* argv[]){

int n;
n=200;


//Allocate Memory
double* A = new double[n*n];
double* x = new double[n];
double* y = new double[n];


//random input für x,y,A,B
//cout<<"x=";
for(int i=0; i<n; i++){
x[i*n] = (rand()%100)+1;
// cout<<x[i*n];
}

// cout<<endl;

//cout<<"y=";
for(int j=0; j<n; j++){
y[j*n] = (rand()%100)+1;
// cout<<y[j*n];
}

// cout<<endl;

//cout<<"Matrix A :";
for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {
A[i*n+j] = rand()% 100 + 1;
// cout << A[i*n+j];
}
// cout << endl;
}


clock_t start2 = clock();
matrixVector(n,A,x,y);
clock_t finish2 = clock();


//deallocate
delete[] x;
delete[] y;
delete[] A;


return 0;
}
http://www.cplusplus.com/forum/general/112111/

1
2
3
4
	double* x = new double[n];
	for(int i=0; i<n; i++){ 
		x[i*n] = (rand()%100)+1;  //out of bounds
	}
I don't understand.It not work.
The problem is that the array only contain n elements but in the loop that ne555 posted you multiply the index so that it becomes bigger than the number of elements in the array.
how can ı write as a matrix :

double* x = new double[n*n];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x[i*n+j] = rand()% 100 + 1;
cout << x[i*n+j];
}
}

!!?
Topic archived. No new replies allowed.