For not acting the same with variable that has predefined value - c

Why is there an error in these two codes which should be the same?
This one works:
int N=4;
int M[N][N];
for(int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &M[i][j]);
}
}
This one only reads one line:
int i=0,j=0,N=4;
int M[N][N];
for(i; i < N; i++) {
for (j; j < N; j++) {
scanf("%d", &M[i][j]);
}
}
But if you add space in front of %d in scanf function it will read two rows.

You are not re-initializing j to zero on each iteration, you need:
int i=0,j=0,N=4;
int M[N][N];
for(i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
scanf("%d", &M[i][j]);
}
}

int i=0,j=0,N=4;
int M[N][N];
//for(i; i < N; i++) {
//for (j; j < N; j++) {
scanf("%d", &M[i][j]); // i = 0; j = 0
//}
//}
j++;
scanf("%d", &M[i][j]); // i = 0; j = 1
j++;
scanf("%d", &M[i][j]); // i = 0; j = 2
j++;
scanf("%d", &M[i][j]); // i = 0; j = 3
i++;
scanf("%d", &M[i][j]); // i = 1; j = 3
i++;
scanf("%d", &M[i][j]); // i = 2; j = 3

Related

How to correctly print the result of matrices multiplication in different cases?

I'm making a program that multiplies 3 matrices and print the outcome. The program can input several cases of multiplication and each case can determined their own number of NxN matrix (n). However, if I input 2 cases, first with n=2 and second with n=3, the output of the first case will have a 3x3 matrix with row 3 and column 3 only have 0s. How do I fix this problem?
#include <stdio.h>
int main(){
int t, n, i, j, k, l, a[50][50][10], b[50][50][10], c[50][50][10], d[50][50][10], e[50][50][10];
scanf ("%d", &t);
for (l=1; l<=t; l++){
scanf("%d", &n);
// matrix a
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j][l]);
}
}
// matrix b
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & b[i][j][l]);
}
}
// matrix c
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & c[i][j][l]);
}
}
//Multiplication
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
d[i][j][l] = 0;
for (k = 0; k < n; k++) {
d[i][j][l] += a[i][k][l] * b[k][j][l];
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
e[i][j][l] = 0;
for (k = 0; k < n; k++) {
e[i][j][l] += d[i][k][l] * c[k][j][l];
}
}
}
}
//Printing the product
for (l=1; l<=t; l++){
printf ("Case #%d:\n", l);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", e[i][j][l]);
}
printf("\n");
}
}
return 0;
}
This is the expected output.

Program only adds the first row of the matrix (and messes up the other rows)

I'm trying to do some simple addition in a 3x3 matrix
int a[3][3], b[3][3], i, j, r[3][3], q, p, mul;
//matrix A
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("masukan matrix A baris %d ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
//matrix B
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("masukan matrix B baris %d ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
//result
printf("matrix A \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
}
printf("\n");
}
printf("matrix B \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
puts("1. addition");
puts("2. Transpose");
puts("3. exit");
printf("choice ");
scanf("%s", &choice);
switch (choice) {
case '1': {
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
r[i][j] = a[i][j] + b[i][j];
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d \t", r[i][j]);
}
printf("\n");
}
}
} break;
}
and i got this result
as you can see it only adds the first row of the 2 matrices.
I'm fairly new to C and i have no idea the workaround for this problem
any help would be appreciated
Sending your code through a code formatter shows that you are missing a closing brace at two locations:
Location 1
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
// missing a closing brace here.
puts("1. addition");
Location 2
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
r[i][j] = a[i][j] + b[i][j];
}
// missing a closing brace here.
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d \t", r[i][j]);
}

Line pattern in a diamond in C

I'm a beginner in C, I'm facing a problem in order to implement a diamond, I'm following guided exercises from a book but I got stuck, I have to implement the pattern:
According to the exercise, they suggest me to implement the pattern using the operator %
I coded the structure but when I want to implement the pattern #.o.# I just destroy everything
#include<stdio.h>
int main(){
int n, space;
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (int i = 0; i < n; i++){
for (int j = 0;j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j <= i*2; j++){
printf("#");
}
printf("\n");
space--;
}
space = 0;
for (int i = n; i >= 0; i--){
for (int j = 0; j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j < (i*2)-1;j++){
printf("#");
}
printf("\n");
space++;
}
}
I really appreciate it if somebody could give me a hint or help with this, I got stuck here for the last 2 weeks.
Thank you.
This is what you need
#include<stdio.h>
int main(){
int n, space,i,j;
char pattern[]={'#','.','o','.'};
int position;
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (i = 0; i < n; i++){
position=0;
for (j = 0;j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (j = 0;j <= i; j++,position++)
printf("%c",pattern[position%sizeof(pattern)]);
position-=2;
for(;j<=2*i;j++,position--)
printf("%c",pattern[position%sizeof(pattern)]);
printf("\n");
space--;
}
}
The output for some cases:
./test
Number of sides: 10
#
#.#
#.o.#
#.o.o.#
#.o.#.o.#
#.o.#.#.o.#
#.o.#.o.#.o.#
#.o.#.o.o.#.o.#
#.o.#.o.#.o.#.o.#
#.o.#.o.#.#.o.#.o.#
./test
Number of sides: 5
#
#.#
#.o.#
#.o.o.#
#.o.#.o.#
./test
Number of sides: 2
#
#.#
For a diamond:
#include<stdio.h>
int main(){
int n, space,i,j;
char pattern[]={'#','.','o','.'};
int position;
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (i = 0; i < n; i++){
position=0;
for (j = 0;j < space; j++){
printf(" ");
}
for (j = 0;j <= i; j++,position++)
printf("%c",pattern[position%sizeof(pattern)]);
position-=2;
for(;j<=2*i;j++,position--)
printf("%c",pattern[position%sizeof(pattern)]);
printf("\n");
space--;
}
space = 0;
for (int i = n-1; i >= 0; i--){
for (int j = 0; j < space; j++){
printf(" ");
}
position=0;
for (j = 0;j <= i; j++,position++)
printf("%c",pattern[position%sizeof(pattern)]);
position-=2;
for(;j<=2*i;j++,position--)
printf("%c",pattern[position%sizeof(pattern)]);
printf("\n");
space++;
}
}
And the output
./test
Number of sides: 10
#
#.#
#.o.#
#.o.o.#
#.o.#.o.#
#.o.#.#.o.#
#.o.#.o.#.o.#
#.o.#.o.o.#.o.#
#.o.#.o.#.o.#.o.#
#.o.#.o.#.#.o.#.o.#
#.o.#.o.#.#.o.#.o.#
#.o.#.o.#.o.#.o.#
#.o.#.o.o.#.o.#
#.o.#.o.#.o.#
#.o.#.#.o.#
#.o.#.o.#
#.o.o.#
#.o.#
#.#
#
you have to just change printf("#") to printf("%c",pattern[j%4])
Here is a code:
int main(){
int n, space;
char pattern[4] = {'#','.','o','.'};
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (int i = 0; i < n; i++){
for (int j = 0;j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j <= i*2; j++){
printf("%c",pattern[j%4]);
}
printf("\n");
space--;
}
space = 0;
for (int i = n; i >= 0; i--){
for (int j = 0; j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j < (i*2)-1;j++){
printf("%c",pattern[j%4]);
}
printf("\n");
space++;
}
}
The reason why you "destroy everything" is probably because you tried to do it with printf(pattern[j%4]). printf as first parameter wants string (address in memory when string is stored) when you pass pattern[j%4], it thinks that for example '#' (in ASCII 64) is adress in memory, and want to read from it, but it is not valid memory adress so operating system kill your program and it "get's destroyed"

What's wrong with this code for multiplying two matrices in C?

The aim of the matrixMultiplier function is to multiply A * B, and then show the resulting matrix C, for any two square matrices A and B. A and B sizes are limited by 10*10.
When the user is asked the size of the matrix they wish to introduce, although A and B are 10*10, the function only works with the submatrices in A and B up to the dimension which the user has specified, say 3*3.
I've tested this out multiplying the 3*3 identity matrix by itself and it's not giving me the correct answer.
#include <stdio.h>
void matrixMultiplier(int A[][10], int B[][10], int C[][10], int n) {
int i, j, k;
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
C[i][k] = 0; /* Initialize output matrix to zero */
}
}
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
for (j = 0; j < n; j++) {
C[i][k] += A[i][j] * B[j][k];
}
}
}
printf("\n");
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
printf("%4d", C[i][k]);
}
printf("\n");
}
}
int main(void) {
int A[][10] = {{0}};
int B[][10] = {{0}};
int C[][10] = {{0}}; /* Initialize output matrix to zero */
int i, j;
int n;
printf("Enter square matrix dimension: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Assign a value: ");
scanf("%d", &A[i][j]);
}
printf("New row.\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", A[i][j]);
}
printf("\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Assign a value: ");
scanf("%d", &B[i][j]);
}
printf("New row.\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", B[i][j]);
}
printf("\n");
}
matrixMultiplier(A, B, C, n);
return 0;
}
You need to use statically allocated arrays like this:
int A[10][10] = {{0}};
int B[10][10] = {{0}};
int C[10][10] = {{0}}; /* Initialize output matrix to zero */
As other commentators noticed, in C int A[][10] = {{0}}; is basically an equivalent of int A[1][10] = {{0}};, so only first rows are correctly set.
Take a look at this code:
#include <stdio.h>
#define MAX_SIZE 10
void matrixMultiplier(int A[][MAX_SIZE], int B[][MAX_SIZE], int C[][MAX_SIZE], int n)
{
int i, j, k;
for (i = 0; i < n; i++)
for (k = 0; k < n; k++)
for (j = 0; j < n; j++)
C[i][k] += A[i][j] * B[j][k];
printf("\nProduct Matrix C:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", C[i][j]);
putchar('\n');
}
}
int main(void)
{
//int A[][10] = {{0}}; By initializing like this you are creating this int A[1][10] = {{0}};
int A[MAX_SIZE][MAX_SIZE] = {0};
int B[MAX_SIZE][MAX_SIZE] = {0};
int C[MAX_SIZE][MAX_SIZE] = {0};
int i, j, n;
printf("Enter square matrix dimension: ");
scanf("%d", &n);
printf("Input %d values row-wise, for matrix A: ", n*n);
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &A[i][j]);
printf("Input %d values row-wise, for matrix B: ", n*n);
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &B[i][j]);
printf("\nGiven Matrix A:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", A[i][j]);
putchar('\n');
}
printf("\nGiven Matrix B:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", A[i][j]);
putchar('\n');
}
matrixMultiplier(A, B, C, n);
return 0;
}
If you need more explanation about 2D arrays refer to this link

In C program which remove duplicates from an array, when I print an array, the output is not true

This program which written in C should remove duplicated elements from 2 input arrays by user, so when I print the second array which is b[z] after removing duplicates, the output is not true as it prints weird number instead of the input number by the user. (the problem in code is determined by comment).
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, z ;
printf("Enter size of array\n");
scanf("%d", &n);
int a[n];
if(n <= 20) {
for(int i = 0 ; i < n; i++) {
printf("Enter integer \n");
scanf("%d", &a[i]);
}
}
for(int i = 0 ; i < n; i++) {
printf("%d ", a[i]);
}
printf("\nEnter size of the 2nd array\n");
scanf("%d", &z);
int b[z];
if(z <= 20) {
for(int i = 0 ; i < z; i++) {
printf("Enter integer \n");
scanf("%d", &b[z]);
}
}
for(int i = 0 ; i < z; i++) {
printf("%d ", b[z]);
}
for(int i = 0 ; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(a[i] == a[j]) {
for(int l = j; l < n; l++)
{
a[l] = a[l + 1];
}
n--;
j--;
}
}
}
printf("\nArray1: ");
for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
for(int t = 0; t < z; t++) {
for(int u = t + 1; u < z; u++) {
if(b[t] == b[u]) {
for(int l = u; l < z; l++)
{
b[l] = b[l + 1];
}
z--;
u--;
}
}
}
printf("\nArray2: ");
for(int e = 0; e < z; e++) {
printf("%d ", b[e]);
}
return 0;
}

Resources