how can i store fibonacci sequence in a matrix - c

well, i need to store the fibonacci sequence in a 2x5 matrix but what im doing its not working. here's my attempt
int main()
{
int i,j,k;
int mat[2][5];
mat[0][0]=0;
mat[0][1]=1;
k=2;
for (i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
mat[i][k]=mat[i][j]+mat[i][j+1];
k++;
}
}
for(i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
return 0;
}

How about using the row and column of the matrix to calculate the "position" of that element at the fibonacci sequence?
#include <stdio.h>
#define ROWS 2
#define COLS 5
int fibonacci(int position)
{
if (position == 0)
return 0;
if (position == 1)
return 1;
return fibonacci(position - 1) + fibonacci(position - 2);
}
int main()
{
int mat[ROWS][COLS];
for(int row = 0; row < ROWS; row++)
{
for(int col = 0; col < COLS; col++)
{
/*
[0,0] = 0 [0,1] = 1, [0,2] = 2, [0,3] = 3, [0,4] = 4
[1,0] = 5 [1,1] = 6, [1,2] = 7, [1,3] = 8, [1,4] = 9
*/
mat[row][col] = fibonacci(COLS * row + col);
printf("%3d, ", mat[row][col]);
}
printf("\n");
}
return 0;
}

Would you please try the following:
#include <stdio.h>
int main()
{
int i, j, k;
int mat[2][5] = {{0, 1}};
for (k = 2; k < 10; k++) {
mat[k / 5][k % 5] = mat[(k - 1) / 5][(k - 1) % 5] + mat[(k - 2) / 5][(k - 2) % 5];
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}

Related

Sorting matrix by the sum of columns in C

I need help with code for sorting matrix by sum of columns.
On this site there is already a similar program with sorting by sum of rows.I copied that code and tried to adapt it for columns and it doesn't work.
And one more question:I don't understand the operation in function sum.
If someone could help me with this.
Thank you.
#include<stdio.h>
int sum(int *row, int size) {
int s = 0;
for (size--; size >= 0; size--) s += row[size];
return s;
}
int main() {
int matrix[3][4] = {{1, 2, 3, 4},
{9, 10, 11, 12},
{5, 6, 7, 8}};
int tmp;
printf("Before:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Bubble sort.
for (int i = 0; i < 2; i++) {
for (int j = i + 1; j < 3; j++) {
// Comparing row value.
if (sum(matrix[i], 4) > sum(matrix[j], 4)) {
// Swapping.
for (int k = 0; k < 4; k++) {
tmp = matrix[i][k];
matrix[i][k] = matrix[j][k];
matrix[j][k] = tmp;
}
}
}
}
printf("After:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}

Here I have used functions and arrays to find out whether the matrix is symmetric or non- symmetric . Can anyone optimise my code using pointers?

Here we are given a martix and we have to find out whether the matrix is symmetrix or not. I want to optimise it using array pointer or passsing by reference or you can suggest better approach , which uses multiple concepts, and please provide an explanation , it would be helpful.
I am learning arrays so, that why I am asking you to use multiple concepts , I want to see if there is a better approach
#include <stdio.h>
#include <stdlib.h>
void swap(int arr[3][3], int i, int j)
{
int temp;
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
void check(int arr[3][3], int i, int j)
{
static int count = 0;
if (arr[i][j] == arr[j][i])
{
count++;
if (count == 9)
{
printf("matrix is symmetric");
}
}
}
int main()
{
int arr[3][3] = {1, 3, 3,
3, 1, 5,
3, 5, 5};
int i, j;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3; j++)
{
swap(arr, i, j);
}
printf("\n");
}
printf("THE TRANSPOSE MATRIX IS \n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
check(arr, i, j);
if (arr[i][j] != arr[j][i])
{
printf("matrix is non-symmetric");
exit(0);
}
}
}
}
To find symmetric matrix simply use the following code:
#include <stdio.h>
#define dim 4
int main()
{
int i=0;
int arr[dim][dim]= {1, 3, 2, 3,
3, 1, 5, 4,
2, 5, 5, 16,
3, 4, 16, 7};
int isSymmetric = 1;
while (isSymmetric && i<dim)
{
for (int j = i; j < dim; j++)
{
if(arr[i][j]!=arr[j][i]) {
isSymmetric = 0;
break;
}
}
i++;
}
isSymmetric==1?printf("Symmetric"):printf("notSymmetric");
return 0;
}
The complexity of this algorithm at most is O(N/2) when N is the number of elements.

how to return the last element of each row in C

This exercise asks me to fill the first row of the matrix then put the last element of each row in the first box of the next row and transfers the elements of the previous row to the next row like the following example:
5 7 9 2
2 5 7 9
9 2 5 7
7 9 2 5
5 7 9 2
The problem is start from the second row to the end in my code(the program does not return the last value of row 1 to the row 2) , can you help me
my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows=5,cols=4;
int T[rows][cols];
int p=1;
printf("The filling of this matrix :\n");
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
if(i==0)
{
scanf("%d",&T[i][j]);
}
T[p][0]=T[p-1][cols-1];
for(int k=1;k<rows;k++)
{
T[k][j+1]=T[k-1][j];
}
}
p++;
}
printf("\nThe display of this matrix :\n");
for(int i=0;i<rows;i++)
{printf("\n");
for(int j=0;j<cols;j++)
{
printf("[%d]",T[i][j]);
}
}
}
#include <stdio.h>
int main(void){
int rows = 5, cols = 4;
int T[rows][cols];
printf("Filling the first row\n");
for(int i=0; i < cols; i++){
printf("Enter T[0][%d] = ",i)
scanf("%d",&T[0][i]);
}
printf("Filling the rest matrix\n");
for(i=1; i < rows; i++){
for(j=0; j < cols; j++){
if(j == 0){
T[i][j] = T[i-1][cols-1];
}
else{
T[i][j] = T[i-1][j-1];
}
}
}
printf("display the matrix\n");
for(i=0 ; i < rows; i++){
for(j=0 ; j < cols; j++){
printf(" %d ",T[i][j]);
}
printf("\n")
}
}
You are accessing outside of the bounds of the array here:
T[k][j+1]=T[k-1][j]; // j + 1 = 4 in the last iteration in a range [0 ... 3]
You are over-complicating things, you don't need an extra loop (k) nor an extra variable (p), all the information needed is already in the inner loop, just check the position of i and j:
#include <stdio.h>
int main(void)
{
int rows = 5, cols = 4;
int T[rows][cols]; // Notice that this is a VLA, avoid using
// them and use enum {rows = 5, cols = 4}
// when values are known before hand
printf("The filling of this matrix :\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (i == 0)
{
scanf("%d", &T[i][j]);
}
else
{
if (j == 0)
{
T[i][j] = T[i - 1][cols -1];
}
else
{
T[i][j] = T[i - 1][j - 1];
}
}
}
}
printf("\nThe display of this matrix :\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("[%d]", T[i][j]);
}
printf("\n");
}
}
You can simplify:
if (j == 0)
{
T[i][j] = T[i - 1][cols -1];
}
else
{
T[i][j] = T[i - 1][j - 1];
}
using a ternary operator:
T[i][j] = T[i - 1][(j == 0) ? cols - 1 : j - 1];

How to print a row/column/diagonal from matrix

I am trying to get my program print out the rows, columns or diagonals if they don't equal the magic square rule, for example,
if the matrix is
1 9 5
2 4 3
6 8 7
Row 1 [2, 4, 3] doesn't work
Row 2 [6, 8, 7] doesn't work
Column 0 [1, 2, 6] doesn't work
Diagonal 1 [1, 4, 7] doesn't work
I've tried print("%d", matrix[row])
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
int main()
{
//declared variables
int size = 3;
int matrix[3][3];
int row, column = 0;
int sum0, sum1, sum2;
int flag = 0;
//ask user to input 1-9 and scans it
printf("Enter in the values: \n");
for (row = 0; row < size; row++){
for (column = 0; column < size; column++)
scanf("%d", &matrix[row][column]);
}
//enters number into magic square format
printf("You entered: \n");
for (row = 0; row < size; row++) {
printf("\n");
for (column = 0; column < size; column++) {
printf("%d ", matrix[row][column]);
}
}
//diagonal calculations
sum0 = 0;
for (row = 0; row < size; row++) {
for (column = 0; column < size; column++) {
if (row == column)
sum0 = sum0 + matrix[row][column];
}
}
//row calculations
for (row = 0; row < size; row++) {
sum1 = 0;
for (column = 0; column < size; column++) {
sum1 = sum1 + matrix[row][column];
}
if (sum0 == sum1)
flag = 1;
else {
flag = 0;
break;
}
}
//column calculations
for (row = 0; row < size; row++) {
sum1 = 0;
for (column = 0; column < size; column++) {
sum2 = sum2 + matrix[row][column];
}
if (sum1 == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
printf("\nAnalyzing...\n");
if (flag == 1) {
sleep(2);
printf("This is a magic square!\n");
}
else {
sleep(2);
printf("This is not a magic square!\n");
}
return 0;
}
You have to use loops to print each character separately.
printf(3) offers no way to print an array of integers.
it seems weired that initailzied sum1 in colum calculations
//column calculations
for (row = 0; row < size; row++) {
sum1 = 0; // <= sum2 = 0; maybe
for (column = 0; column < size; column++) {
sum2 = sum2 + matrix[row][column];
}
if (sum1 == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
and if row calcuations used loop as below
for(row = 0; row < size; row++){
for(column = 0; column < size; column++){
// access to matrix[row][column]
}
}
column calculations loop will be revised
for(column = 0; column < size; column++){
for(row = 0; row < size; row++){
// access to matrix[row][column]
}
}
plus, you can write the diagonal calculations simply
//diagonal calculations
sum0 = 0;
for (int diag = 0; diag < size; diag++) {
sum0 += matrix[diag][diag];
}
I think you get the logic:
const int x = 5;
const int y = 7;
int matrix[x][y];
for (int j = 0; j < x; ++j)
{
for (int i = 0; i < y; ++i)
{
matrix[j][i] = j + i;
printf("%3d ", matrix[j][i]);
}
putchar('\n');
}
puts("\nPrint a column: ");
for(int i = 0; i < x; i++)
{
printf("%3d ", matrix[i][0 /*constant*/]);
}
puts("\nPrint a row: ");
for (int k = 0; k < 7; ++k)
{
printf("%3d ", matrix[0/*constant*/][k]);
}
puts("\nPrint a diagonal");
for (int l = 0, m = 0; l < x && m < y; ++l, ++m)
{
printf("%3d ", matrix[l][m]);
}

Removing duplicated numbers in an array

#include <stdio.h>
int main()
{
int array[8] = {4, 1, 5, 4, 2, 7, 4, 2};
int i, j, k;
int len = 8;
for(i = 0; i < len; i++)
{
for(j = i + 1; j < len;)
{
if(array[j] == array[i])
{
for(k = j; k < len - 1; k++)
{
array[k] = array[k + 1];
}
len--;
}
else
{
j++;
}
}
for(i = 0; i < len; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
return 0;
}
Hi,
Above is the code to remove duplicated numbers in an array, but when compiles and executes, I get 4 1 5 2 7 2--which isn't right because I'm supposed to get 4 1 5 2 7.
It seems that I have a problem with len but couldn't figure out where and what in the code specifically needs to be fixed.
Don't know if it is a mistake in indentation, or you miss-copied the code from somewhere ?
anyways to get it run, make this minor changes and it runs (Don't expect an explanation I doubt you don't need it)
#include <stdio.h>
int main()
{
int array[8] = {4, 1, 5, 4, 2, 7, 4, 2};
int i, j, k;
int len = 8;
for(i = 0; i < len; i++)
{
for(j = i + 1; j < len;)
{
if(array[j] == array[i])
{
for(k = j; k < len - 1; k++)
{
array[k] = array[k + 1];
}
len--;
}
else
{
j++;
}
}
} // close the main `for` loop
// you used to print out after each iteration!
for(i = 0; i < len; i++)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}

Resources