function not printing correctly in c - c

I am trying to build a dynamic maze i got to the part where i get the size and get the chars that i need to build the maze from them.
but the function thats build the maze prints it really asymmetrical how can i fix that?
my code:
#include <stdio.h>
#include <stdlib.h>
char **board;
int size = 0;
void print_Board();
void initialize_Board();
int main()
{
initialize_Board();
print_Board();
return 0;
}
/*initialize the board*/
void initialize_Board()
{
int i, j;//indexs
char s;
scanf("%d", &size);
board = (char**)malloc(sizeof(char*)* (size));
if (!board) { printf("ERROR - memroy allocation.\n"); exit(1); }
for (i = 0; i < size; i++)//for loops to build the board for the game
{
board[i] = (char*)malloc(sizeof(char)*(size));
if (!board[i]) { printf("ERROR - memroy allocation, for loop\n");
exit(1);
}
for (j = 0; j < size; j++)
{
scanf("%c", &s);
board[i][j] = s;
}//for col
printf("\n");
}//for row
}
//print the board
void print_Board()
{
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%c ", board[i][j]); //print the value in the [i][j] place.
}//for col
printf("\n");
}//for row
}

Change:
for (j = 0; j < size; j++)
{
scanf("%c", &s);
board[i][j] = s;
}//for col
To:
for (j = 0; j < size; j++) {
scanf("%c ", &s);
board[i][j] = s;
}//for col
board[i][j] = '\n'; // Add new line to end of row making it a string.
This ensures that each character is read and the return char is discarded.
and change:
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%c ", board[i][j]); //print the value in the [i][j] place.
}//for col
printf("\n");
}//for row
to:
int i;
for (i = 0; i < size; i++) {
printf("%s", board[i]); //print the values in the [i] row.
}
This prints each row with a newline at the end.

Related

Expression must have arithmetic type issue

I need to multiply two square matrixes A and B 15x15.
Unfortunately, I'm getting this kind of error.
I know the problem is in pointers while calculating matrix C.
C[i][j] += *(A + k) * *(B + k)
I hope you can explain me what's wrong. I'm a beginner xD.
Thank you in advance.
#include <stdio.h>
#define N 15
#define _CRT_SECURE_NO_WARNINGS
int main() {
int A[N][N];
int B[N][N];
int C[N][N];
printf("Input matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &A[i][j]);
}
printf("\n");
}
printf("Input matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &B[i][j]);
}
printf("\n");
}
printf("Matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("Matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", B[i][j]);
}
printf("\n");
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
C[i][j] = 0;
for (int k = 0; k < 14; k++) {
C[i][j] += *(A + k) * *(B + k);
k++;
}
}
}
printf("Your result:\n");
printf("Matrix C.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
The problem in the multiplication is that A+k and B+k have type int (*)[15] which means dereferencing it once only makes a pointer out of them; furthermore, you need to take row and column items individually, which means A[i][k] and B[k][j], right? (also, there's no point on using confusing syntax, as the underlying operation is exactly the same).
Here's a fixed and improved version:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 15
/* Improvement 1 (type abstraction) */
typedef int NxN_int_matrix[N][N];
/* Improvement 2 (input function & wrapper) */
#define input_matrix(var) input_matrix_ex((var), #var)
static void input_matrix_ex(NxN_int_matrix dst, char *name)
{
printf("Input matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 3 (nicer prompt) */
printf("%s[%2d][%2d]: ", name, i, j);
fflush(stdout);
scanf_s("%d", &dst[i][j]);
}
}
printf("\n");
}
/* Improvement 4 (print function) */
#define print_matrix(var) print_matrix_ex(#var, (var))
static void print_matrix_ex(char *name, NxN_int_matrix M)
{
printf("Matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", M[i][j]);
}
printf("\n");
}
}
/* Improvement 5 (move multiplication to a function too, and fix it) */
static void mult_matrix(NxN_int_matrix dst, NxN_int_matrix a, NxN_int_matrix b)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 6 (don't write out intermediate values) */
int tmp = 0;
for (int k = 0; k < N; k++)
tmp += a[i][k] * b[k][j];
dst[i][j] = tmp;
}
}
}
int main()
{
NxN_int_matrix A, B, C;
input_matrix(A);
input_matrix(B);
print_matrix(A);
print_matrix(B);
mult_matrix(C, A, B);
printf("Your result:\n");
print_matrix(C);
return 0;
}
/* Possible further improvements:
* - using a transposed B might make multiplication faster
*/

Loop to delete matching values in a list not working

I wrote this code to delete all the elements from arr2 that match with the elements of arr. Can anyone help me understand where did I go wrong?
int main() {
int arr[]={1,2,3,4,5};
int arr2[]={1,1,2,3,2,5,7,8};
int count = 8;
for (int i=0; i<5; i++)
{
for (int j=i+1; j<count; j++)
{
if (arr[i]==arr2[j])
{
printf("\nMATCH FOUND %d & %d\n", arr[i], arr2[j]);
for(int k=j; k<count-1; k++)
{
arr2[k] = arr2[k+1];
printf("After deletion %d", arr2[k]);
}
count--;
}
j--;
}
}
printf("\nCount: <%d>\n", count);
for(int i=0; i<count; i++)
{
printf("No: <%d>\n", arr2[i]);
}
return 0;
}
If i am understanding correctly, the resulting array should be {7,8}.
Then, I think it will be easier to just make a new array. Note that the maximum size of the resulting array can be is the size of arr2. (i.e. when there is no match at all)
#define SIZE 8
int main() {
int arr[]={1,2,3,4,5};
int arr2[]={1,1,2,3,2,5,7,8};
int count = 8;
int result[SIZE];
int k = 0;
for (int i = 0; i < count; i++)
{
int has_dup = 0;
for (int j = 0; j < 5; j++)
{
if (arr2[i]==arr[j])
{
printf("\nMATCH FOUND %d & %d\n", arr2[i], arr[j]);
has_dup = 1;
break;
}
}
if (has_dup == 0)
result[k++] = arr2[i];
}
for(int i = 0; i < k; i++)
{
printf("No: <%d>\n", result[i]);
}
return 0;
}

Cannot input number in multi-dimensional array pointer to pointer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
int N;
scanf("%d", &N);
int **arr;
arr = (int**)malloc(sizeof(int) * N);
for(int i = 0; i < N; i++){
arr[i] = (int*)malloc(sizeof(int) * N);
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(i == 0 || j == 0)
arr[i][j] = 1;
else
arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
for(int i = 0; i < N; i++)
free(arr[i]);
free(arr);
return 0;
}
This code makes a 2-dimensional array with a pointer; I want assign value arr[i][j] = 1. However, an error EXC_BAD_ACCESS occurs in XCode on a Mac. I don't know how to solve the problem. What is my mistake? The assigning for loop is where the trouble occurs.

printing a matrix adress instead of values | C

I wrote a code that tries to multiply two matrices and put them in another result matrix. The code is working (I think) but it prints a very strange output .. I think it has to do with one of the functions pointing to something diffrent than the values, not sure though.
I tried checking each function in separete, also inizialzing each matrix with {0}.
#include <stdio.h>
#define SIZE 5
//#pragma warning(disable:4996)
//#define _CRT_SECURE_NO_WARNINGS
void read_mat(int mat[][SIZE])
{
int i, j, k = 0;
char s[100]; // assign input str to 's'
fgets(s,25,stdin); // recieving only the first 25 numbers
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (s[k] == '\0') { //if the string is just \0 -- the end of the string
mat[i][j] = 0;
}
else { // is there are values in s
if (s[k] != '0') { // binary matrix -- only 0 or 1
mat[i][j] = 1;
k++;
}
else {
mat[i][j] = 0;
++k;
}
}
}
}
}
void mult_mat(int mat_a[][SIZE], int mat_b[][SIZE], int result_mat[][SIZE])
{
int i, j, k;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
for (k = 0; k < SIZE; k++) {
result_mat[i][j] += mat_a[i][k] * mat_b[k][j]; // by definition of matrix multiplication
k++;
}
}
}
}
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("%3d", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}
int main()
{
int mat_a[SIZE][SIZE] = {0}, mat_b[SIZE][SIZE] = {0}, result_mat[SIZE][SIZE] = {0}; // initializing matricies to {0}
printf("Please Enter Values For Matrix 1\n");
read_mat(mat_a);
printf("Please Enter Values For Matrix 2\n");
read_mat(mat_b);
mult_mat(mat_a, mat_b, result_mat);
printf("The First Matrix Is :- \n");
print_mat(mat_a);
printf("The Second Matrix Is :- \n");
print_mat(mat_b);
printf("The Resultant Matrix Is :- \n");
print_mat(result_mat);
return 0;
}
the outout I am getting:
enter image description here
thanks!
improve your print function
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("(%3d), ", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}

Memory allocation for pointer to array and array of pointer

Below is the part of code I am stuck on. I want to dynamically allocate memory for
Pointer to array
Array of pointers
I am getting several error messages like invalid conversion from int * to int and so on.
Pointer to array
int (*array)[nrows][ncolumns];
array = (int*)malloc(nrows * ncolumns * sizeof(int));
printf("\n Enter the elements:\n");
for(i=0; i<nrows; i++)
{
for(j=0; j<ncolumns; j++)
{
scanf("%d", array[i][j]);
}
}
printf("Entered array is :\n\n");
for(i = 0;i<nrows; i++)
{
for(j = 0; j<ncolumns; j++)
{
if(j== ncolumns-1)
{
printf("%d \n", *array[i][j]);
}
else
{
printf("%d", *array[i][j]);
}
Array of pointers
int *array[nrows][ncolumns];
array[nrows][ncolumns] = (int*)malloc(nrows * ncolumns * sizeof(int));
printf("Enter elements:\n");
for(i = 0; i<nrows; i++)
{
for(j = 0; j<ncolumns;j++)
{
scanf("%d",&array[i][j]);
}
}
printf("Entered array is: \n");
for(i = 0; i<nrows; i++)
{
for(j = 0; j<ncolumns;j++)
{
if(j == ncolumns-1)
{
printf("%d \n",array[i][j]);
}
else
{
printf("%d \t",array[i][j]);
}
}
}
1> pointer to array
#include <stdio.h>
#include <stdlib.h>
int main(void){
int nrows = 3;
int ncolumns = 4;
int i, j;
int (*array)[nrows][ncolumns];//do you want <<int (*array)[ncolumns]>> ?
//like as int src[nrows][ncolumns]; array = &src;
array = malloc(nrows * ncolumns * sizeof(int));//(int*) : type mismatch
printf("\nEnter the elements:\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
scanf("%d", &(*array)[i][j]);
}
}
printf("Entered array is :\n\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
if(j != 0)
putchar(' ');
printf("%d", (*array)[i][j]);//need ( )
}
putchar('\n');
}
free(array);
return 0;
}
2> Array of pointers
#include <stdio.h>
#include <stdlib.h>
int main(void){
int nrows = 3;
int ncolumns = 4;
int i, j;
int *array[nrows][ncolumns];
int *src = (int*)malloc(nrows * ncolumns * sizeof(int));//no need (int*)
printf("Enter elements:\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns;j++){
array[i][j] = &src[ i * ncolumns + j];//pointer pointed to entity (src[ i * ncolumns + j])
scanf("%d", array[i][j]);//type of array[i][j] is int *
}
}
printf("Entered array is: \n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
if(j != 0)
putchar(' ');
printf("%d", *array[i][j]);//need * for dereference
}
putchar('\n');
}
free(src);
return 0;
}
3> option
#include <stdio.h>
#include <stdlib.h>
int main(void){
int nrows = 3;
int ncolumns = 4;
int i, j;
int (*array)[ncolumns];
array = (int (*)[ncolumns])malloc(nrows * sizeof(*array));//sizeof(*array) : sizeof(int[ncolumns])
printf("\nEnter the elements:\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
scanf("%d", &array[i][j]);
}
}
printf("Entered array is :\n\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
if(j != 0)
putchar(' ');
printf("%d", array[i][j]);
}
putchar('\n');
}
free(array);
return 0;
}

Resources