I'm doing the past exam and everything has been going well, I've finished the task, but I have some formatting problems.
What I get:
What I want to achieve:
It bothers me and I would be grateful if someone could come up with a solution.
My code for printing the array:
void printArray(int tab[][MAX], int n, int m) {
for (int j = 0; j < m; j++)
{
printf("%5d", j);
}
printf("\n");
for (int k = 0; k < 3 * n + 4; k++)
{
printf("-");
}
printf("\n");
for (int i = 0; i < n; i++)
{
printf("%2d |", i);
for (int j = 0; j < m; j++)
{
printf("%3d", tab[i][j]);
}
printf("\n");
}
}
The offset is off and printf("%5d", j); makes the heading numbers too wide.
Fix it by prepending the first line, but instead of printf("%2d |", i); that you use to prepend each line when printing the values, you can use a blank string, printf("%2s |", "");.
void printArray(int tab[][MAX], int n, int m) {
printf("%2s |", ""); // this fixes the offset
for (int j = 0; j < m; j++)
{
printf("%3d", j); // and use the same width as when printing the values
}
// ...
Here's my solution, with liberal use of width specifiers on the printf statements.
As you change the constant COL_WIDTH, the table should generally automatically adjust.
#include <stdio.h>
void printArray(int tab[][4], int n, int m)
{
const int COL_WIDTH = 4;
printf("%*.*s|", COL_WIDTH, COL_WIDTH, "");
for (int j = 0; j < m; j++)
{
printf("%*d", COL_WIDTH, j);
}
printf("\n%.*s", COL_WIDTH*(n+1)+1, "----------------------------------------");
for (int i = 0; i < n; i++)
{
printf("\n%*d |", COL_WIDTH-1, i);
for (int j = 0; j < m; j++)
{
printf("%*d", COL_WIDTH, tab[i][j]);
}
}
}
int main(void) {
int table[][4] = { { 1, 2, 1, 2}, {2, 2, 3, 2}, {1, 3, 2, 3}, {2, 2, 2, 1} };
printArray(table, 4, 4);
return 0;
}
Output
| 0 1 2 3
---------------------
0 | 1 2 1 2
1 | 2 2 3 2
2 | 1 3 2 3
3 | 2 2 2 1
Related
i currently have a array function that reads in values of user input and I want to print out the array using another function.
this is my code
Function to read in user input and store in 2d array
void readMatrix(){
int arr[3][4];
int *ptr;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
printf("row %d, col %d: ", i + 1, j + 1);
scanf("%d", &arr[i][j]);
}
ptr = &arr;
}
}
function to print array stored in readMatrix() function
void printMatrix(){
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 4; ++j)
{
printf("row %d, col %d = %d\n", i + 1, j + 1, arr[i][j]);
}
}
}
int main(){
//function to read matrix
readMatrix();
//function to print matrix
printMatrix();
return 0;
}
int arr[3][4]; is a local variable only valid inside that function, so you can't pass it to anyone else. The easiest and best solution is to use caller allocation instead. You can also rewrite the functions to work with variable amounts of rows/cols.
With just a few tweaks to your code:
#include <stdio.h>
void readMatrix (int rows, int cols, int arr[rows][cols]){
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("row %d, col %d: ", i + 1, j + 1);
scanf("%d", &arr[i][j]);
}
}
}
void printMatrix (int rows, int cols, int arr[rows][cols]){
for (int i = 0; i < rows; ++i)
{
printf("{ ");
for (int j = 0; j < cols; ++j)
{
printf("%2.d ", arr[i][j]);
}
printf("}\n");
}
}
int main (void)
{
int arr[3][4];
readMatrix(3,4,arr);
printMatrix(3,4,arr);
return 0;
}
Input:
1 2 3 4 5 6 7 8 9 10 11 12
Output:
{ 1 2 3 4 }
{ 5 6 7 8 }
{ 9 10 11 12 }
If you don't understand why this works, then "array decay" is the next best thing to study. What is array to pointer decay?
The problem is that you are trying to access the array from outside the scope of the function that created it. You need to pass it as a parameter to your print function. You can also return a pointer to the array from your read function and then pass the pointer to your print function. Here is an example of the second option:
#include <stdio.h>
int *readMatrix(int *arr);
void printMatrix(int *arr);
int main()
{
int arr[3][4];
readMatrix(&arr[0][0]);
printMatrix(&arr[0][0]);
return 0;
}
int *readMatrix(int *arr)
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 4; ++j)
{
printf("row %d, col %d: ", i + 1, j + 1);
scanf("%d", &arr[i * 4 + j]);
}
}
return arr;
}
void printMatrix(int *arr)
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 4; ++j)
{
printf("row %d, col %d = %d\n", i + 1, j + 1, arr[i * 4 + j]);
}
}
}
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.
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;
}
This is a program that is supposed to calculate the sum of all the diagonals in the matrix and then print them out.
ex. if the matrix is
1 2 3 4 5
2 3 4 5 6
0 1 1 2 5
5 5 5 5 5
7 8 9 7 7
the output should be
17 13 13 10 5
15 17 13 13 10
14 15 17 13 13
13 14 15 17 13
7 13 14 15 17
#include <stdio.h>
int main()
{
int n, sum=0, i, j, sub_i, sub_j, sub1_i, sub1_j;
scanf("%d ", &n);
int array1[n][n];
for(i=0;i<n;i++){
for(j=0; j<n; j++){
scanf("%d", &array1[i][j]);
}
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
sub_i=i;
sub_j=j;
sub1_i=i;
sub1_j=j;
sum=0;
if(j>i){
while(sub_j<n){
sum+=array1[sub_i][sub_j];
sub_i++;
sub_j++;
}
while(sub_j<n){
array1[sub_i][sub_j]=sum;
sub1_i++;
sub1_j++;
}
}
if(i>j){
while(sub_i<n){
sum+=array1[sub1_i][sub1_j];
sub_i++;
sub_j++;
}
while(sub1_i<n){
array1[sub1_i][sub1_j]=sum;
sub1_i++;
sub1_j++;
}
}
}
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf("%d ", array1[i][j]);
}
printf("\n");
}
return 0;
}
When i run the program it prints the array as if no value was assigned to the matrix. Can someone point out what is happening?
Quoting the comment by Weather Vane:
The program alters the array it is examining — see array1[sub_i][sub_j]=sum; — and then prints incorrect values, since you can't correctly sum the diagonals of an array that is changing.
The OP already realize that
... what you are telling me is to assign the values to another array and print that.
Yes, that is way easier:
#include <stdio.h>
int main(void)
{
int n;
// Checking the input is always a good idea, but you
// may prefer something less brutal, in case of error
if (scanf("%d", &n) != 1 || n < 1)
return 1;
int mat[n][n];
for (int i = 0; i < n; ++i) {
for (int j= 0; j < n; ++j) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
// Calculate and store the sum of the diagonals. Note that
// it could be done in the previous loop, but it may be better
// to refactor those snippets into separate functions.
int diagonals[2 * n + 1];
for (int i = 0; i < 2 * n + 1; ++i)
diagonals[i] = 0; // consider 'memset' instead of this loop
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
diagonals[n + i - j] += mat[i][j];
}
}
// Now print the correct values in their position
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
printf("%4d", diagonals[n + i - j]);
}
printf("\n");
}
return 0;
}
Testable HERE.
You can do like the follow:
#include <stdio.h>
#define N 5
int main()
{
int array[N][N] = {
1, 2, 3, 4, 5,
2, 3, 4, 5, 6,
0, 1, 1, 2, 5,
5, 5, 5, 5, 5,
7, 8, 9, 7, 7};
for(int i = 1; i < N; ++i)
for(int j = 1; j < N; ++j)
array[i][j] += array[i - 1][j - 1];
for (int i = 0; i + 1 < N; ++i)
for (int j = 0; j + 1 < N; ++j)
if (i == j)
array[i][j] = array[N - 1][N - 1];
else if (i > j)
array[i][j] = array[N - 1][N - 1 - i + j];
else
array[i][j] = array[N - 1 - j + i][N - 1];
for (int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j)
printf("%d ", array[i][j]);
printf("\n");
}
return 0;
}
I cannot figure out why the output of my program is so strange. I just wanted to print matrix with pointers, but what I get is:
1 7 3 8 9
10 8 9 3 4
1 7 3 8 9
7 3 8 9 10
What am I doing wrong here?
#include<stdio.h>
#define NK 5
#define NW 2
int sum(int *w);
int main(void) {
srand(time(NULL));
int T[NW][NK];
int i, j;
for (i = 0; i<NW; i++) {
for (j = 0; j<NK; j++) {
T[i][j] = rand() % 10 + 1;
printf("%d ", T[i][j]);
}
printf("\n");
}
int *wsk = T;
printf("\n");
sum(wsk);
return 0;
}
int sum(int *w) {
int i, j;
int suma = 0;
printf("\n");
for (i = 0; i<NW; i++) {
for (j = 0; j<NK; j++) {
printf("%d ", *((w + i)+j));
}
printf("\n");
}
}
If the geometry is fixed, you can just declare the argument with the proper type:
int sum(int w[NW][NK]) {
printf("\n");
for (int i = 0; i < NW; i++) {
for (int j = 0; j < NK; j++) {
printf("%d ", w[i][j]);
}
printf("\n");
}
}
If you insist on passing a pointer to a linearized version:
int sum(int *w) {
printf("\n");
for (int i = 0; i < NW; i++) {
for (int j = 0; j < NK; j++) {
printf("%d ", w[i * NK + j]);
}
printf("\n");
}
}