Computer Science Canada vector/matrix program help |
Author: | ronnieb23 [ Sun Nov 01, 2009 9:30 pm ] |
Post subject: | vector/matrix program help |
I was wondering if anyone could take a look at my code and tell me why when i run my program and it completes its task it will say segmentation error core dumped or something like that. It should continue to run the program over again but it doesn't. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> bool vector_addition(int* v1, int n1, int* v2, int n2, int* result) { if (n1!=n2) { return 0; } for (int x=0; x<n1; x++) { *(result+x)=(*(v1+x))+(*(v2+x)); }; return 1; } void vector_scalar_mult(int* v1, int n1, int a, int* result) { for (int x=0; x<n1; x++) { *(result+x)=(*(v1+x))*a; }; } bool dot_product(int* v1, int n1, int* v2, int n2, int* result) { if (n1!=n2) { return 0; } for (int x=0; x<n1; x++) { *result+=(*(v1+x))*(*(v2+x)); }; return 1; } void outer_product(int* v1, int n1, int* v2, int n2, int* result) { int nProduct=0; for (int x=0; x<n1; x++) { for (int y=0; y<n2; y++, nProduct++) { *(result+nProduct)=(*(v1+x))*(*(v2+y)); }; }; } bool matrix_addition(int* M1, int r1, int c1, int* M2, int r2, int c2, int* result) { if (r1!=r2 || c1!=c2) { return 0; } int nMA; nMA=0; for (int x=0; x<r1; x++) { for (int z=0; z<c1; z++, nMA++) { *(result+nMA)= (*(M1+nMA))+(*(M2+nMA)); } }; } bool matrix_multiplication(int* M1, int r1, int c1, int* M2, int r2, int c2, int* result) { if (c1!=r2) { return 0; } int nMult=0; int nRow[c1]; int nCol[r2]; for (int n=0; n<r1; n++) { for (int m=0; m<c2; m++, nMult++) { for (int o=0; o<c1; o++) { nRow[o]=*(M1+n*c1+o); }; for (int p=0; p<r2; p++) { nCol[p]=*(M2+m*r2); }; if (dot_product(nRow, c1, nCol, r2, result)) return 1; }; }; } void print_vector(int *v, int n) { printf("<"); for (int j=0; j<n; j++) { printf("%d\t", *(v+j)); }; printf(">\n"); } void print_matrix(int *M, int r, int c) { int nCount=0; for (int k=0; k<r; k++) { printf("|"); for (int l=0; l<c; l++, nCount++) { printf("%d\t", *(M+nCount)); }; printf("|\n"); }; printf("\n"); } void read_vector(int *v, int n) { printf("Vector value: "); for (int i=0; i<n; i++) { scanf(" %d", (v+i)); }; } void read_matrix(int *M, int r, int c) { int nNumb=0; printf("Entries of matrix:\n"); for (int g=0; g<r; g++) { for (int h=0; h<c; h++, nNumb++) { scanf(" %d ", (M+nNumb)); }; printf("\n"); }; } int main() { int Menu; int Length1; int Length2; int Length3; int Width1; int Width2; int Width3; int Scalar; char charCheck; while (1) { printf("--------------------------------------------------\n"); printf("Main menu:\n"); printf("\t 1. Vector Addition\n"); printf("\t 2. Scalar Multiplication\n"); printf("\t 3. Dot Product\n"); printf("\t 4. Outer Product\n"); printf("\t 5. Matrix Addition\n"); printf("\t 6. Matrix Multiplication\n"); printf("\t 7. Quit\n"); printf("\tEnter your selection[1..7]: "); scanf("%d", &Menu); switch(Menu) { {case 1: printf("Size of the first vector: "); scanf(" %d", &Length1); int Vector1[Length1]; read_vector(&Vector1[0], Length1); printf("Size of the second vector: "); while(getchar() != '\n'); scanf(" %d", &Length2); int secondVector1[Length2]; read_vector(&secondVector1[0], Length2); int Results1[Length1]; if (vector_addition(&Vector1[0], Length1, &secondVector1[0], Length2, &Results1[0])) { printf("Sum of two vectors:\n"); print_vector(&Results1[0], Length1); } else { printf("Error: Two vectors must have same size!"); } break;} {case 2: printf("Size of the first vector: "); scanf(" %d", &Length1); int Vector2[Length1]; read_vector(&Vector2[0], Length1); printf("Scalar: "); while(getchar() != '\n'); scanf(" %d", &Scalar); int Results2[Length1]; vector_scalar_mult(&Vector2[0], Length1, Scalar, &Results2[0]); printf("Result:\n"); print_vector(&Results2[0], Length1); break;} {case 3: printf("Size of the first vector: "); scanf(" %d", &Length1); int Vector3[Length1]; read_vector(&Vector3[0], Length1); printf("Size of the second vector: "); scanf(" %d", &Length2); int secondVector3[Length2]; read_vector(&secondVector3[0], Length2); int Results3; if (dot_product(&Vector3[0], Length1, &secondVector3[0], Length2, &Results3)) { printf("Dot product of two vectors: "); printf("%d\n", Results3); } else { printf("Error: Two vectors must have same size!"); } break;} {case 4: printf("Size of the first vector: "); scanf(" %d", &Length1); int Vector4[Length1]; read_vector(&Vector4[0], Length1); printf("Size of the second vector: "); scanf(" %d", &Length2); int secondVector4[Length2]; read_vector(&secondVector4[0], Length2); int Results4[Length1][Length2]; outer_product(&Vector4[0], Length1, &secondVector4[0], Length2, &Results4[0][0]); printf("Outer product of two vectors:\n"); print_matrix(&Results4[0][0], Length1, Length2); break;} {case 5: printf("Size of the first matrix (row and column): "); scanf(" %d %d", &Length1, &Width1); int Matrix1[Length1][Width1]; read_matrix(&Matrix1[0][0], Length1, Width1); printf("Size of the second matrix (row and column): "); scanf(" %d %d", &Length2, &Width2); int secondMatrix1[Length2][Width2]; read_matrix(&secondMatrix1[0][0], Length2, Width2); int Results5[Length1][Width1]; if (matrix_addition(&Matrix1[0][0], Length1, Width1, &secondMatrix1[0][0], Length2, Width2, &Results5[0][0])) { printf("Sum of two matrices:\n"); print_matrix(&Results5[0][0], Length1, Width1); } else { printf("Error: Two matrices must have same numbers of rows and columns!"); } break;} {case 6: printf("Size of the first matrix (row and column): "); scanf(" %d %d", &Length1, &Width1); int Matrix2[Length1][Width1]; read_matrix(&Matrix2[0][0], Length1, Width1); printf("Size of the second matrix (row and column): "); scanf(" %d %d", &Length2, &Width2); int secondMatrix2[Length2][Width2]; read_matrix(&secondMatrix2[0][0], Length2, Width2); int Results6[Length1][Width2]; if (matrix_multiplication(&Matrix2[0][0], Length1, Width1, &secondMatrix2[0][0], Length2, Width2, &Results6[0][0])) { printf("Multiplication of two matrices:\n"); print_matrix(&Results6[0][0], Length1, Width1); } else { printf("Error: Two matrices must have same numbers of rows and columns!"); } break;} {case 7: exit(EXIT_SUCCESS); default: printf("Invalid choice");} } } return 0; } |
Author: | Tony [ Sun Nov 01, 2009 9:34 pm ] |
Post subject: | RE:vector/matrix program help |
use debug statements to find the line where segmentation fault occurs. |