Can't compile matrix multiplication - c

Hey I got this error and I tried like 10 solutions and neither works. I want to load 2 matrix, each one from its own txt file then to multiply them. I can't compile cause of LNK1120 and LNK2019 errors.
Here is my code:
int main(int argc, char *argv[])
{
FILE *macierz1, *macierz2, *fw;
char *line = malloc(1000);
int count = 0;
macierz1 = fopen("macierz1.txt", "r");
if (macierz1 == NULL) {``
printf("nie można otworzyć", argv[1]);
exit(1);
}
macierz2 = fopen("macierz2.txt", "r");
if (macierz2 == NULL) {
printf("nie można otworzyć", argv[2]);
exit(1);
}
double *data = (double*)malloc(1000 * sizeof(double));
if (data == NULL)
{
printf("błąd lokowania pamięci");
return EXIT_FAILURE;
}
getline(&line, &count, macierz1);
int read = -1, cur = 0, columCount1 = 0;
while (sscanf(line + cur, "%lf%n", &data[columCount1], &read) == 1)
{
cur += read; columCount1++;
}
int rowCount1 = 1;
while (getline(&line, &count, macierz1) != -1) { rowCount1++; }
printf("%d\n", columCount1);
printf("%d\n", rowCount1);
getline(&line, &count, macierz2);
read = -1, cur = 0;
int columCount2 = 0;
while (sscanf(line + cur, "%lf%n", &data[columCount2], &read) == 1)
{
cur += read; columCount2++;
}
int rowCount2 = 1;
while (getline(&line, &count, macierz2) != -1) { rowCount2++; }
printf("%d\n", columCount2);
printf("%d\n", rowCount2);
int i = 0;
int j = 0;
int **mat1 = (int **)malloc(rowCount1 * sizeof(int*));
for (i = 0; i < rowCount1; i++)
mat1[i] = (int *)malloc(columCount1 * sizeof(int));
fseek(macierz1, 0, SEEK_SET);
for (i = 0; i < rowCount1; i++)
{
for (j = 0; j < columCount1; j++)
fscanf(macierz1, "%d", &mat1[i][j]);
}
i = 0;
j = 0;
printf("\n\n");
//print matrix 1
for (i = 0; i < rowCount1; i++)
{
for (j = 0; j < columCount1; j++)
printf("%d", mat1[i][j]);
printf("\n");
}
i = 0;
j = 0;
int **mat2 = (int **)malloc(rowCount2 * sizeof(int*));
for (i = 0; i < rowCount2; i++)
mat2[i] = (int *)malloc(columCount2 * sizeof(int));
fseek(macierz2, 0, SEEK_SET);
for (i = 0; i < rowCount2; i++)
{
for (j = 0; j < columCount2; j++)
fscanf(macierz2, "%d", &mat2[i][j]);
}
i = 0;
j = 0;
printf("\n\n");
//print matrix 2
for (i = 0; i < rowCount2; i++)
{
for (j = 0; j < columCount2; j++)
printf("%d", mat2[i][j]);
printf("\n");
}
i = 0;
int **mat3 = (int **)malloc(rowCount1 * sizeof(int*));
for (i = 0; i < rowCount1; i++)
mat3[i] = (int *)malloc(columCount2 * sizeof(int));
i = 0;
j = 0;
int k = 0;
int sum = 0;
if (columCount1 != rowCount2)
{
puts("The number of columns in Matrix 1 is not same as the number of rows in Matrix 2");
exit(1);
}
//multiplication of two matrices
for (i = 0; i<rowCount1; i++)
{
for (j = 0; j<columCount2; j++)
{
mat3[i][j] = 0;
for (k = 0; k<columCount1; k++)
{
mat3[i][j] = mat3[i][j] + mat1[i][k] * mat2[k][j];
}
}
}
//print multiplication result
printf("\n\nResult = \n\n");
for (i = 0; i < rowCount1; i++)
{
for (j = 0; j < columCount2; j++)
printf("%d", mat3[i][j]);
printf("\n");
}
for (i = 0; i< rowCount1; i++)
free(mat1[i]);
free(mat1);
for (i = 0; i< rowCount2; i++)
free(mat2[i]);
free(mat2);
for (i = 0; i< rowCount1; i++)
free(mat3[i]);
free(mat3);
free(data);
return 0;
}

Since you're using Visual C++, there is no such C function as getline. The C compiler that comes with Visual C++ adheres to the C89 specification.
Use fgets to retrieve the data from the file.
If you really want to use getline in your Visual C++ (really C) program, you could try this code from GNU that implements the function.

There is a stray '`' in program posted at the end of the line:
if (macierz1 == NULL) {``
and
getline
function is not defined.
add needed includes:
#include <stdio.h>
#include <stdlib.h>

Related

How to add a 2D array group which having reads from a file numbers in c?

I need to read a file which contain numbers, and make 4 rows and 5 columns matrices from those numbers. Then, I need to add these matrices and print the sum matrix (the numbers of matrices depends on the user input).
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[]) {
int a = 4;
int b = 5;
int m;
scanf("%d", &m);
FILE *file = fopen("matrix.txt", "r");
int arr[a][b][m], i, j, k;
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
for (m = 0; k < m; k++) {
fscanf(file, "%d", &arr[i][j][k]);
arr[i][j] += arr[i][j];
}
}
}
for (i = 0; i < a;i++) {
for (j = 0; j < b; j++) {
for (k = 0; k < m; k++) {
printf("%d", arr[i][j][k]);
printf("\t");
}
printf("\n");
}
}
}
You don't need to load the matrix data into separate matrices, you can just add the matrix contents on the fly as you read each matrix from the file:
#include <errno.h>
#include <stdio.h>
#include <string.h>
void main(int argc, char *argv[]) {
int m;
if (scanf("%d", &m) != 1)
return 1;
const char *filename = "matrix.txt";
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "cannot open %s: %s\n", filename, strerror(errno));
return 1;
]
int a = 4;
int b = 5;
int sum[a][b];
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
sum[i][j] = 0;
}
}
for (int k = 0; k < m; k++) {
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
int value;
if (fscanf(file, "%d", &value) != 1) {
fprintf(stderr, "not enough data\n");
return 1;
}
sum[i][j] += value;
}
}
}
fclose(file);
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}
If you are required to load all the data in memory, use an array of matrices: int arr[m][a][b]; and 2 separate loops to read and sum the data:
#include <errno.h>
#include <stdio.h>
#include <string.h>
void main(int argc, char *argv[]) {
int m;
if (scanf("%d", &m) != 1)
return 1;
const char *filename = "matrix.txt";
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "cannot open %s: %s\n", filename, strerror(errno));
return 1;
]
int a = 4;
int b = 5;
int arr[m][a][b];
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
sum[i][j] = 0;
}
}
for (int k = 0; k < m; k++) {
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if (fscanf(file, "%d", &arr[k][i][j]) != 1) {
fprintf(stderr, "not enough data\n");
return 1;
}
}
}
}
fclose(file);
int sum[a][b];
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
int s = 0;
for (int k = 0; k < m; k++) {
s += arr[k][i][j];
}
sum[i][j] = s;
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}

Matrix multiplication from files in C

Hello i have go a assignment where i have 3 files a,b and c which contains 2 matrix which i have to multiply. In all a and b the first line is the size of the array and the next line is the contetnt for that array. c is the output of multiplication of the matrix. When i run the program i get a segmentation fault, and i cant seem to find the problem
#include<stdio.h>
#include<stdlib.h>
int **arrayA; //unallocated array A
int **arrayB; //unallocated array B
int **arrayC; //unallocated array C
int rA, cA; //Size of A
int rB, cB; //Size of B
int rC, cC; //Size of C
/*int matrixmultiplication(int *arrayA, int *arrayB, int *arrayC)//Function that multiplice the matrix
{
for (int i = 0; i < rC; i = i + 1)
for (int j = 0; j < cC; j = j + 1)
{
arrayC[i][j] = 0;
for (int k = 0; k < cA; k = k + 1)
arrayC[i][j] = arrayC[i][j] + arrayA[i][k] * arrayB[k][j];
}
}*/
int main(void)// Main function
{
//File pointer to A, B and C
FILE *Aptr;
FILE *Bptr;
FILE *Cptr;
//Opening the files containing the information for the matrix
Aptr = fopen("/home/au/Desktop/Matrixmulti/a", "r");
if(Aptr == NULL)
{
printf("ERROR!! opening file A");
exit(1);
}
Bptr = fopen("/home/au/Desktop/Matrixmulti/b", "r");
if(Bptr == NULL)
{
printf("ERROR!! opening file B");
exit(1);
}
Cptr = fopen("/home/au/Desktop/Matrixmulti/c", "w");
if(Cptr == NULL)
{
printf("ERROR!! opening file C");
exit(1);
}
//Scan file a for size of matrix a and matrix b
fscanf(Aptr, "%d", &rA);
fscanf(Aptr, "%d", &cA);
fscanf(Bptr, "%d", &rB);
fscanf(Bptr, "%d", &cB);
rC = rA;
cC = cB;
//Allocating space for the arrays
arrayA = malloc(rA * sizeof(int*));
for (int i = 0; i < rA; i++)
{
arrayA[i] = malloc(cA * sizeof(int));
}
arrayB = malloc(rB * sizeof(int*));
for (int i = 0; i < rB; i++)
{
arrayB[i] = malloc(cB * sizeof(int));
}
arrayC = malloc(rC * sizeof(int*));
for (int i = 0; i < rC; i++)
{
arrayA[i] = malloc(cC * sizeof(int));
}
//Scan file a for data for matrix a
for (int i = 0; i < rA; i++)
{
for (int j = 0; j < cA; j++)
{
fscanf(Aptr, "%d", &arrayA[i][j]);
}
}
//Scan file b for data for matrix b
for (int i = 0; i < rB; i++)
{
for (int j = 0; j < cB; j++)
{
fscanf(Bptr, "%d", &arrayB[i][j]);
}
}
//Matrix multiplication
for (int i = 0; i < rC; i = i + 1)
for (int j = 0; j < cC; j = j + 1)
{
arrayC[i][j] = 0;
for (int k = 0; k < cA; k = k + 1)
arrayC[i][j] = arrayC[i][j] + arrayA[i][k] * arrayB[k][j];
}
//Print result matrix to file c
for (int i = 0; i < rC; i++)
{
for (int j = 0; j < cC; j++)
{
fprintf(Cptr, "%d \t", arrayC[i][j]);
}
}
//Closing the files
fclose(Aptr);
fclose(Bptr);
fclose(Cptr);
return 0;
}

Multidimensional array - fscanf

int main(){
int word, r=3, i, j;
FILE *fp1 = fopen("key.txt","r");
int **arr = (int **)malloc(sizeof(int *) * r);
for(i = 0;i<r;i++)
arr[i] = (int *)malloc(sizeof(int)*r);
int a = 0, b = 0;
while (!feof(fp1)) {
fscanf(fp1,"%d",&word);
if (b == r){
a++;
b=0;
continue;
}
arr[a][b++] = word;
}
for (i = 0; i < r; i++)
for (j = 0; j < r; j++)
printf("%d \n", arr[i][j]);
fclose(fp1);
}
And this is my key.txt.
0 -1 0
-1 2 -1
0 -1 0
I want to store key.txt in a dynamic 2d array but it did not work. All the time a part of it is missing. Which part is wrong?
You should use fscanf in while, not feof
You should delete continue.
The follow code could work:
#include <stdio.h>
#include <stdlib.h>
int main(){
int word, r=3, i, j;
FILE *fp1 = fopen("key.txt","r");
int **arr = (int **)malloc(sizeof(int *) * r);
for(i = 0;i<r;i++)
arr[i] = (int *)malloc(sizeof(int)*r);
int a = 0, b = 0;
while (fscanf(fp1,"%d",&word) == 1) {
if (b == r) {
a++;
b=0;
}
arr[a][b++] = word;
}
for (i = 0; i < r; i++) {
for (j = 0; j < r; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
fclose(fp1);
return 0;
}

Creating a matrix and free() problems - c

it seems i have some problems in my code. the first one is that i cant use the free function correctly:"heap corruption detected".
in the second part of my work i wanted to create a matrix:it puts a red mark under the j and then the compiler says that subscript requires array and pointer type.
i only wanted to create a two dimension matrix.
thanks for your help!!!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <math.h>
void main()
{
char *word[1000] = { 0 }, letter[1000], *temparr, *originalarr;
int i = 0, j = 0, counter = 0, k = 0, w = 0, anagram = 0, size = 0, *mat;
for (i = 0; letter[i] != '\n'; i++)
{
letter[i] = getchar();
counter++;
if (letter[i] == ' ' || letter[i] == '\n')
{
if (letter[i] == 10)
{
letter[i] = 0;
word[j] = malloc(sizeof(char*)*(counter - 1));
strcpy_s(word[j], (counter - 1) * sizeof(char*), &letter[i - (--counter)]);
break;
}
if (letter[i - 1] == ' ')
continue;
letter[i] = '\0';
word[j] = malloc(sizeof(char*)*counter);
strcpy_s(word[j], (counter - 1) * sizeof(char*), &letter[i - (--counter)]);
letter[i] = ' ';
counter = 0;
j++;
}
}
while (j > 0)
{
printf_s("%s ", word[j--]);
if (j == 0)
printf_s("%s", word[j]);
}
for (j = 0; word[j] != 0; j++)
{
originalarr = (char*)malloc((sizeof(char) * strlen(word[j])));
strcpy_s(originalarr, strlen(word[j]) + 1, word[j]);
for (i = 0; word[i] != 0; i++)
{
temparr = (char*)malloc((sizeof(char) * strlen(word[i])));
strcpy_s(temparr, strlen(word[i]) + 1, word[i]);
counter = 0;
if (strlen(originalarr) != strlen(temparr))
continue;
for (k = 0; originalarr[k] != 0; k++)
{
for (w = 0; w < strlen(temparr); w++)
{
printf_s("temparr - %c ", temparr[w]);
printf_s("original - %c\n", originalarr[k]);
if (originalarr[k] == temparr[w])
{
temparr[w] = '0';//problem, but cant be equal to 0
counter++;
printf_s("counter- %d\n", counter);
}
}
}
if (counter == strlen(originalarr))
anagram++;
free(temparr);//problem
}
}
k = 0;
size = ceil(sqrt(strlen(letter)));
mat = (int*)malloc((sizeof(int) * size));
for (i = 0; i < size; i++)
{
mat[i] = (int)malloc((sizeof(int) * size));
for (j = 0; j < size; j++)
mat[i][j] = '\0';
}
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
{
mat[i][j] = letter[k];
k++;
}
}
For the second part of your question you need to declare a pointer to a pointermat instead of a simple pointer as you have done. Then allocate as shown.
int ** mat;
mat = malloc((sizeof(int*) * size));
for (i=0; i<size; i++)
mat[i] = malloc( sizeof(int) * size));
You can use this array mat as a 2D array mat[i][j]

what is error in this C code using realloc?

I am trying to have some kind of dynamically growing array/data structure in C. Below is the C code I have for it. But after it prints the array, it gives a run-time error as shown below in the snapshot. What is going wrong? It is being compiled using MS-Visual C++ 2010 (Free version) on a Windows-7.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a;
int i = 5;
if((a = (int *)malloc(i * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed malloc\n");
return 1;
}
for(i = 0; i < 5; i++)
a[i] = i;
printf("-- array after malloc\n");
for(i = 0; i < 5; i++)
printf(" a[%d] = %d\n", i, a[i]);
if((a = (int *)realloc(a, i * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed realloc\n");
return 1;
}
for(i = 0; i < 10; i++)
a[i] = i;
printf("\n-- array after realloc\n");
for(i = 0; i < 10; i++)
printf(" a[%d] = %d\n", i, a[i]);
free(a);
return 0;
}
//<important>
//weren't you supposed to do i = 10 here ????
//</important>
if((a = (int *)realloc(a, i * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed realloc\n");
return 1;
}
for(i = 0; i < 10; i++)
a[i] = i;
I think this was supposed to resize array to 10 and use it, but you never changed i to 10 so i is still 5 and you go out of range
On this line:
if((a = (int *)realloc(a, i * sizeof(int))) == NULL) {
i still contains the value 5. So basically you are realloc'ing for 5 integers while you assign 10 integers in the next loop: an error.
This would be the fixed code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a;
int i;
if((a = (int *)malloc(5 * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed malloc\n");
return 1;
}
for(i = 0; i < 5; i++)
a[i] = i;
printf("-- array after malloc\n");
for(i = 0; i < 5; i++)
printf(" a[%d] = %d\n", i, a[i]);
if((a = (int *)realloc(a, 10 * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed realloc\n");
return 1;
}
for(i = 0; i < 10; i++)
a[i] = i;
printf("\n-- array after realloc\n");
for(i = 0; i < 10; i++)
printf(" a[%d] = %d\n", i, a[i]);
free(a);
return 0;
}

Resources