I have a program which so far is taking in 4 values 1st is the case, 2nd is basically how many columns, the 3rd value doesn't matter right now and the last value specifies rows.
For some reason when I enter 4 6 4 the program will out put the grid of 0's fine.
When I do 7 6 4, the first 3 columns in the first row contain large nonsense numbers.
When I do 4 6 7 it blatantly crashes.
When I do 8 6 4 the problem is in the first row first 4 columns.
I have no idea what the problem is but I think its somewhere in my 2nd calloc call.
#include <stdio.h>
#include <stdlib.h>
int** makeArray();
int permute();
int main()
{ int cases, slotsToFill, possibleColors,movesMade;
int i;
int j;
int** moves;
scanf("%d", &cases);
for( i = 0; i < cases; i++){
scanf("%d %d %d", &slotsToFill,&possibleColors,&movesMade);
moves = makeArray(movesMade,slotsToFill);
}
/*for(i = 0; i < movesMade; i++){
for(j = 0; j<slotsToFill; j++){
printf(" %d", moves[i][j]);
}
printf("\n");
}*/
system("pause");
return 0;
}
int** makeArray(int rows, int cols){
int** moves;
int i;
int j;
cols = cols + 2;
moves = calloc(rows,sizeof(int*));
if(moves != NULL){
for(i = 0; i < cols; i++){
moves[i] = calloc(cols, sizeof(int));
if(moves[i]== NULL){
printf("ALLOCATION FAILED\n");
break;
}
}
for(i = 0; i < rows; i++){
for(j = 0; j<cols; j++){
printf(" %d", moves[i][j]);
}
printf("\n");
}
}
return moves;
}
Related
i cannot find solution how to solve this.
rows number is easy one. reading from keyboard and allocation but column number is different for each row.
lets assume user entered 2 rows after that user entered 3 column for row 0 and 10 column for row 1
and i dont know how to enter values for these columns and print them. because every column has different length. unfortunately != NULL not working
int** ptr; int rows, columns;
printf("enter number of rows..\n");
scanf("%d", &rows);
ptr = (int**)calloc(rows, sizeof(int*));
for (size_t i = 0; i < rows; i++) // allocation an array of integers for every row
{
printf("enter number of columns..\n");
scanf("%d", &columns);
ptr[i] = (int*)calloc(columns, sizeof(int));
}
for (size_t i = 0; i < rows; i++){
for (size_t j = 0; ptr[i][j] != NULL; j++)
ptr[i][j] = j;
}
for (size_t i = 0; i < rows; i++){
for (size_t j = 0; ptr[i][j] != NULL; j++)
printf("%d\n", ptr[i][j]);
}
Since C arrays don't have a final delimiter, then you need to store the length in an efficient way:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
int** ptr; int rows, columns;
printf("enter number of rows..\n");
scanf("%d", &rows);
ptr = (int**)calloc(rows, sizeof(int*));
for (size_t i = 0; i < rows; i++) // allocation an array of integers for every row
{
printf("enter number of columns..\n");
scanf("%d", &columns);
ptr[i] = (int*)calloc(columns+1, sizeof(int)); // Last pos will save the size
// We need to track the columns since C arrays do not have a final delimiter
ptr[i][columns] = INT_MIN; // It could also be INT_MAX depending on our needs
}
for (size_t i = 0; i < rows; i++){
for (size_t j = 0; ptr[i][j] != INT_MIN; j++)
ptr[i][j] = j;
}
for (size_t i = 0; i < rows; i++)
{
printf("Line %li\n", i);
for (size_t j = 0; ptr[i][j] != INT_MIN; j++)
{
printf(" Column %li: ", j);
printf("%d\n", ptr[i][j]);
}
}
return 0;
}
I am trying to create a program which prints a matrix of integers, but the output returns weird numbers before the actual matrix. There are no compiling errors.
This is what my code looks like: //ignore void function for now, focus on main function::
#include <stdio.h>
#include <stdlib.h>
//array[30][30] = 2D array of max 30 rows and 30 columns
//n = number of rows and columns
void printmatrix(int array[30][30], int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d", array[i][j]);
}
printf("\n");
}
return;
}
int main(){
int n;
scanf("%d", &n);
int ints2D[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &ints2D[i][j]);
}
}
printmatrix(ints2D, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", ints2D[i][j]);
}
printf("\n");
}
return 0;
}
And this is my output (I only want the last three lines)
123
-514159984327663
-51415932632766-514159305
1 2 3
4 5 6
7 8 9
You are missing a space in "%d" in printmatrix, and, more importantly, it is not proper to pass an int [n][n] array for an int [30][30] parameter unless n is 30.
Change void printmatrix(int array[30][30], int n) to void printmatrix(int n, int array[n][n]), and change printmatrix(ints2D, n); to printmatrix(n, ints2D);. That makes the type of the argument you are passing match the type of the parameter.
In your function args you defined the array as fixed size ([30][30]) but you are passing a VLA ([3][3] in your example) which makes it find uninitialized memory and why you are seeing the strange numbers.
#Eric Postpischil answer is spot on. Another way to solve it: 2d arrays could be flatten into 1d. Here is a working code for you:
#include <stdio.h>
#include <stdlib.h>
//array[30][30] = 2D array of max 30 rows and 30 columns
//n = number of rows and columns
void printmatrix(int *array, int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", array[i * n + j]);
}
printf("\n");
}
return;
}
int main(){
int n;
scanf("%d", &n);
int ints2D[n * n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &ints2D[i * n + j]);
}
}
printmatrix(ints2D, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", ints2D[i * n + j]);
}
printf("\n");
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've tried to create two matrices and do the product in another matrix, but the compiler gives an error of core dump. The creation of the first two matrices is right; something is wrong with the third matrix.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m;
int n;
int t;
int i;
int **A;
int **B;
int k;
int j;
scanf("%d",&n);
scanf("%d",&m);
scanf("%d",&t);
A=malloc(n*sizeof(int*));
for(i=0;i<n;i++){
A[i]=malloc(m*sizeof(int));
}
for(i=0;i<n;i++){ // A[n][m]
for(j=0;j<m;j++)
{
scanf("%d",&(A[i][j]));
}
}
B=malloc(t*sizeof(int*));
for(i=0;i<t;i++) //B[m][t]
{
B[i]=malloc(n*sizeof(int));
}
for(i=0;i<t;i++){
for(j=0;j<n;j++)
{
scanf("%d",&(B[i][j]));
}
}
int **C;
C=malloc(t*sizeof(int*));
for(i=0;i<t;i++){{A[i]=malloc(m*sizeof(int));}
for(i=0;i<t;i++){
for(j=0;j<m;j++){
C[i][j]=0;
for(k=0;k<n;k++)
{
(C[i][j])=(C[i][j])+((A[k][j])*(B[i][k]));
}
}
}
}
return 0;
}
You must separate memory for C, not for A. That is why when you try to access C[i][j] it generates this error. Change:
for(i=0;i<t;i++){{A[i]=malloc(m*sizeof(int));}
to
for(i=0;i<t;i++){ C[i]=malloc(m*sizeof(int));}
Complete code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m;
int n;
int t;
int i;
int **A;
int **B;
int k;
int j;
scanf("%d",&n);
scanf("%d",&m);
scanf("%d",&t);
A=malloc(n*sizeof(int*));
for(i=0;i<n;i++){
A[i]=malloc(m*sizeof(int));
}
for(i=0;i<n;i++){ // A[n][m]
for(j=0;j<m;j++){
scanf("%d", &(A[i][j]));
}
}
B=malloc(t*sizeof(int*));
for(i=0;i<t;i++) //B[m][t]
{
B[i]=malloc(n*sizeof(int));
}
printf("B\n");
for(i=0;i<t;i++){
for(j=0;j<n;j++)
{
scanf("%d",&(B[i][j]));
}
}
int **C;
C=malloc(t*sizeof(int*));
for(i=0;i<t;i++){
C[i]=malloc(m*sizeof(int));
}
for(i=0;i<t;i++){
for(j=0;j<m;j++){
C[i][j]=0;
for(k=0;k<n;k++)
{
(C[i][j])=(C[i][j])+((A[k][j])*(B[i][k]));
}
}
}
return 0;
}
Previous answers may have fixed the problem that caused the crash, but the code is still broken. It would be nice if the variables had more descriptive names. Since each variable is declared on a separate line, you could make use of the space to provide descriptive comments about the variables.
It is conventional to reference the rows of a matrix first, and then the columns. So, I will diverge from your usage, and say that matrix A has m rows and n columns. B must then have n rows, and it looks like you intend for t to hold the number of columns in B. Then the product C will be a m X t matrix.
Your code started to go wrong when you allocated space for B. You allocated for t rows of n elements, when you should have allocated for m rows of t elements (by your own notation). In my code below, because I have changed the order of m and n, I allocate for n rows of t elements.
Then, for the product matrix, I have allocated for m rows of t elements, where you had allocated for t rows of m elements. The calculation of the elements of the product matrix was also wrong in your code. The [i][j] element of C is the vector dot-product of the ith row of A and the jth column of B. The way you have calculated this element, C[i][j] is the dot-product of the jth column of A and the ith row of B.
Here is the code with corrections. I included some input prompts, and some code to display the entered matrices and the resulting product.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int m; // rows in A
int n; // cols in A
int t; // cols in B
int i;
int **A; // points to first row of A
int **B; // points to first row of B
int **C; // points to first row of C
int k;
int j;
printf("Enter number of rows in A: ");
scanf("%d", &m);
printf("Enter number of columns in A: ");
scanf("%d", &n);
printf("Enter number columns in B: ");
scanf("%d", &t);
A = malloc(sizeof(int*) * m); // A[m][n]
for(i = 0;i < m; i++){
A[i] = malloc(sizeof(int) * n);
}
for(i = 0; i < m; i++){
for(j = 0; j < n; j++)
{
scanf("%d", &(A[i][j]));
}
}
B = malloc(sizeof(int*) * n); // B[n][t]
for(i = 0; i < n; i++)
{
B[i] = malloc(sizeof(int) * t);
}
for(i = 0; i < n; i++){
for(j = 0; j < t; j++)
{
scanf("%d", &(B[i][j]));
}
}
C = malloc(sizeof(int*) * m); // C[m][t]
for(i = 0; i < m; i++){
C[i] = malloc(sizeof(int) * t);
}
for(i = 0; i < m; i++){
for(j = 0; j < t; j++){
C[i][j] = 0;
for(k = 0; k < n; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
printf("Matrix A:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%-5d", A[i][j]);
}
putchar('\n');
}
printf("Matrix B:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < t; j++) {
printf("%-5d", B[i][j]);
}
putchar('\n');
}
printf("Matrix product:\n");
for (i = 0; i < t; i++) {
for (j = 0; j < m; j++) {
printf("%-5d", C[i][j]);
}
putchar('\n');
}
return 0;
}
Here is the result of a test run:
λ> ./a.out
Enter number of rows in A: 2
Enter number of columns in A: 3
Enter number columns in B: 2
2 3 4
1 3 5
3 4
5 6
7 8
Matrix A:
2 3 4
1 3 5
Matrix B:
3 4
5 6
7 8
Matrix product:
49 58
53 62
So I coded traditional matrix multiplication in C (I'm a beginner to C), but for some reason my result isn't correct, although I don't see any glaring errors. My input file looks like:
3 2
2 2
2 2
2 2
2 3
1 1 1
1 1 1
The 3 2 and 2 3 in the first and fifth lines represent the number of rows and columns in the subsequent matrices. The result should be a 3x3 matrix, all 4s. However, this code returns
4197299 4 4
4 4 4
-1912599044 32621 572
I'm inclined to believe this might be due to the way I declared the matrices. Instead of using malloc, I scanned the row and column values from the input file and directly instantiated the required matrices. I'm very new to C, so the concept of dynamic memory allocation isn't 100% clear yet. I could be totally off the mark, but I'm not sure. What confuses me especially is that about half of the matrix returned correct values. Why is this the case? Below is my code.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char** argv){
int i, j, k, row1, col1, row2, col2, temp;
if(argc != 2){
printf("error\n");
exit(1);
}
FILE *file = fopen(argv[1], "r");
if(file == NULL){
printf("error\n");
exit(1);
}
//MATRIX 1
fscanf(file, " %d\t%d", &row1, &col1);
int matrix1[row1][col1];
for(i = 0; i<row1; i++){
for(j=0; j<col1; j++){
fscanf(file, "%d", &temp);
matrix1[i][j] = temp;
}
}
//MATRIX TWO
fscanf(file, " %d\t%d", &row2, &col2);
int matrix2[row2][col2];
int product[row1][col2]; //DECLARING PRODUCT MATRIX
for(i = 0; i<row2; i++){
for(j=0; j<col2; j++){
fscanf(file, "%d", &temp);
matrix2[i][j] = temp;
}
}
for(i = 0; i<row1; i++){
for(j = 0; j<col2; j++){
for(k = 0; k<col1; k++){
product[i][j] += matrix1[i][k]*matrix2[k][j]; //MULTIPLICATION STEP
}
}
}
for(i = 0; i<row1; i++){
for(j = 0; j<col2; j++){
printf("%d\t", product[i][j]); //PRINTING THE PRODUCT
}
printf("\n");
}
return 0;
}
for(i = 0; i<row1; i++){
for(j = 0; j<col2; j++){
product[i][j] = 0; // should be zero before summing
for(k = 0; k<col1; k++){
product[i][j] += matrix1[i][k]*matrix2[k][j]; //MULTIPLICATION STEP
}
}
}
You don't initialize product, which means that its contents is indeterminate, then using it in calculations (with e.g. +=) results in undefined behavior.
You need to initialize the products matrix to all zeroes first.
Yeah, that way of declaring dynamic arrays doesn't necessarily work, in any version before or after C99, although what’s causing the problem is that you don’t initialize product. Try calloc( row1*col2, sizeof(int) ); This allocates all the elements and initializes them to 0.
The problem statement goes like this: Calculate the sum of the elements in a bidimensional matrix, using a separate thread to calculate the sum of each row. The main thread adds up these sums printing the final result.
From what I have seen so far, the code runs correctly. The only issue is when I choose the number of rows to be smaller than the number of columns (e.g. rows = 2, columns = 3), because it only computes the sum for the first 2 columns, completely ignoring the third one.
This is the code I wrote in C and I would really appreciate any help understanding what I did wrong or missed. Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define M 10
#define N 10
int rows, columns, a[M][N], s[M];
// compute the sum of each row
void* f(void* p) {
int k = *((int*) p);
int i;
for (i = 0; i < columns; i++) {
s[i] += a[k][i];
}
return NULL;
}
int main() {
int i, j, *p, rc;
int sum = 0;
pthread_t th[M];
// matrix creation
printf("no. of rows = ");
scanf("%d", &rows);
printf("no. of columns = ");
scanf("%d", &columns);
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("a[%d][%d] = \n", i, j);
scanf("%d", &a[i][j]);
}
}
printf("\nThe matrix is: \n");
for(i=0; i < rows; i++) {
for(j=0; j < columns; j++)
printf("%d ", a[i][j]);
printf("\n");
}
// thread creation
for (i=0; i < rows; i++) {
p = malloc(sizeof(int));
*p = i;
rc = pthread_create(&th[i], NULL, f, p);
if (rc != 0) {
printf("Thread creation failed");
exit(-1);
}
}
for (i=0; i < rows; i++) {
pthread_join(th[i], NULL);
}
// compute the final sum
for (i=0; i < rows; i++) {
sum += s[i];
}
printf("The sum is = %d\n", sum);
return 0;
}
You need
s[k] += a[k][i];
instead of
s[i] += a[k][i];
Sum of each rows should be added up in each index of each row as s is declared as s[M].