most venerable sir
most venerable sir

Reputation: 669

1D array passed to two functions, only one function output the correct result. Why?

I have two routines. I start with the array x in main. One routine is for generating triangular number for each corresponding element in array x and outputting the new array. The other routine does the same thing except it is now factorial number.

I made two calls in the main function to each routines. But only the one called first generates the correct result. So I know my code is correct for both routines. But I don't know what is wrong.

#include <stdio.h>
#define maxRow 3
#define maxCol 4

void factorial(int matrix[maxRow][maxCol]) {
	int row, column, i, product;
	for (row = 0;row < maxRow;++row) {
		for (column = 0;column < maxCol;++column) {
			product = 1;
			for (i = 1; i <= matrix[row][column];++i) {
				product = product*i;
			}//innermost loop ends
			matrix[row][column] = product;
		}//inner loop ends
	}//big for ends

	printf("Factorial number matrix:\n");

	for (row = 0;row < maxRow;++row) {
		for (column = 0;column < maxCol;++column) {
			printf("%i\t\t", matrix[row][column]);
		}//inner loop ends
		printf("\n");
	}//big for ends
}

int main() {
	void triangular(int matrix[maxRow][maxCol]);
	int x[maxRow][maxCol] = {
		{ 1,2,3,4 },
		{ 5,6,7,8 },
		{ 9,10,11,12 }
	};//array ends
	
	factorial(x);
	triangular(x);
}


void triangular(int matrix[maxRow][maxCol]) {
	int row, column, i, sum;
	for (row = 0;row < maxRow;++row) {
		for (column = 0;column < maxCol;++column) {
			sum = 0;
			for (i = 1; i <= matrix[row][column];++i) {
				sum += i;
			}//innermost loop ends
			matrix[row][column] = sum;
		}//inner loop ends
	}//big for ends
	printf("Triangular number matrix:\n");
	for (row = 0;row < maxRow;++row) {
		for (column = 0;column < maxCol;++column) {
			printf("%i\t", matrix[row][column]);
		}//inner loop ends
		printf("\n");
	}//big for ends
	printf("\n");
}

Here is the console output: enter image description here

Upvotes: 0

Views: 37

Answers (1)

user2736738
user2736738

Reputation: 30926

You are making changes to the x when called first time. Now second time when you pass it to triangular() , it is not the original x - it is the changed array x (The result of factorial() function).

Copying the array solves the problem. (make a temporary copy - print the result - then rollback the changes made to x).

void factorial(int matrix[maxRow][maxCol]) {
    int copy[maxCol][maxCol];
    int row, column, i, product;
    for (row = 0;row < maxRow;++row) {
        for (column = 0;column < maxCol;++column) {
            product = 1;
            copy[row][column]=matrix[row][column];
            for (i = 1; i <= matrix[row][column];++i) {
                product = product*i;
            }//innermost loop ends
            matrix[row][column] = product;
        }//inner loop ends
    }//big for ends

    printf("Factorial number matrix:\n");

    for (row = 0;row < maxRow;++row) {
        for (column = 0;column < maxCol;++column) {

            printf("%i\t\t", matrix[row][column]);
            matrix[row][column]=copy[row][column];
        }//inner loop ends
        printf("\n");
    }//big for ends
}

In case the number increases the factorial value wouldn't hold in an int variable. Try using long to get a better range and hold larger factorial values.

Note: A pointer to the array is being passed. If we make changes to the array that the pointer points - it will change the original array.

Upvotes: 2

Related Questions