Wrong scanf in 2D array - c

I have the following code:
#include <stdio.h>
int main() {
int tc,T;
scanf("%d", &T);
for(tc=0; tc<T; tc++){
int flag=0;
int R,C;
scanf("%d %d", &R, &C);
{
int i,j;
int r,c;
int Grid[R][C];
//Read the Grid
for(i=0; i<R; i++){
for(j=0; j<C; j++){
scanf("%d", &Grid[i,j]);
}
}
scanf("%d %d", &r, &c);
{
int Pattern[r][c];
//Read the Grid
for(i=0; i<r; i++){
for(j=0; j<c; j++){
scanf("%d", &Pattern[i,j]);
}
}
//Here we have both the Grid and the Pattern
for(i=0; i<R; i++){
for(j=0; j<C; j++){
if(Grid[i,j]==Pattern[0,0] && ((i+r)<=R) && ((j+c)<=C)){
int x,y;
int innerFlag=1;
for(x=0; x<r; x++){
for(y=0; y<c; y++){
if(Grid[x+i,y+j]!=Pattern[x,y]) innerFlag=0;
}
}
if(innerFlag == 1){
//Set the flag to 1 and break out of the nested loops
flag=1;
j=C;
i=R;
}
}
}
}
//Here all the calculation is done and the flag is set
if(flag==1) printf("YES\n");
else printf("NO\n");
}
}
}
return 0;
}
Which gives me the following errors:
warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int ()[(sizetype)(C)]'
scanf("%d", &Grid[i,j]);
warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int ()[(sizetype)(c)]'
scanf("%d", &Pattern[i,j]);
Do you know what's wrong with this code?
P.S I also checked the Grid and the Pattern and it reads trash!

In C you should subscript arrays like this: array[i][j].
array[i, j] is equivalent to array[j], since i is ignored.

Arrays in c are not accessed like Grid[i, j] but as Grid[i][j]. So change it to that in your code. Similarly Pattern[i, j] should be written as Pattern[i][j].
This will solve your problem.
Happy to Help! :)

Try something like following:
#include <stdio.h>
int main() {
int tc,T;
printf("Enter one number\n");
scanf("%d", &T);
for(tc=0; tc<T; tc++){
int flag=0;
int R,C;
printf("Enter 2 numbers\n");
scanf("%d %d", &R, &C);
{
int i,j;
int r,c;
int Grid[R][C];
printf("now reading the grid\n");
//Read the Grid
for(i=0; i<R; i++){
for(j=0; j<C; j++){
scanf("%d", &Grid[i][j]);
}
}
printf("Enter 2 mre numbers\n");
scanf("%d %d", &r, &c);
{
int Pattern[r][c];
printf("now reading the pattern\n");
//Read the Grid
for(i=0; i<r; i++){
for(j=0; j<c; j++){
scanf("%d", &Pattern[i][j]);
}
}
//Here we have both the Grid and the Pattern
for(i=0; i<R; i++){
for(j=0; j<C; j++){
if(Grid[i][j]==Pattern[0][0] && ((i+r)<=R) && ((j+c)<=C)){
int x,y;
int innerFlag=1;
for(x=0; x<r; x++){
for(y=0; y<c; y++){
if(Grid[x+i][y+j]!=Pattern[x][y]) innerFlag=0;
}
}
if(innerFlag == 1){
//Set the flag to 1 and break out of the nested loops
flag=1;
j=C;
i=R;
}
}
}
}
//Here all the calculation is done and the flag is set
if(flag==1) printf("YES\n");
else printf("NO\n");
}
}
}
return 0;
}

Related

Getting errors while compiling a C program to find the duplicate values from an m×n matrix using malloc() and free()

SO i was solving this problem and i am getting this error
What is my mistake here?Why is it saying invalid argument type?Is there any declaration mistake I made?
I am a newbie still I am trying hard to learn these. A detailed explanation will be useful
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int m,n;
printf("Input the number of Rows: ");
scanf("%i", &m);
printf("Input the number of Columns: ");
scanf("%i", &n);
int *arr=(int*)malloc(m * sizeof(int));
for(int i=0;i<m;i++)
{
arr[i] = (int*)malloc(n*sizeof(int));
}
printf("Populate Matrix Row by Row\n--------------------------\n");
for(int i=0; i<m; i++){
printf("Row [%i]\n--------\n",i);
for(int j=0; j<n; j++){
printf("At Col [%i]= ",j);
scanf("%i", &*(*(arr+i) + j));
printf("\n");
}
}
printf("[MATRIX]\n------------------\n");
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
printf("%i ",*(*(arr+i) + j));
}
printf("\n");
}
printf("------------------\n");
printf("The duplicate value(s) are:\n");
int temp_index=0;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
temp_index=j;
for(int x=i; x<m; x++){
for(int y=temp_index; y<n; y++){
if(j!=y){
if(*(*(arr+i) + j) == *(*(arr+x) + y)){
printf("%i in position (%i, %i)\n",*(*(arr+i) + j),x,y);
}
}
}
temp_index=0;
}
}
}
free(arr);
return 0;
}
int *arr=(int*)malloc(m * sizeof(int));
*(*(arr+i) + j): *(arr+i) is int, you are trying to dereference the int value *(arr+i) + j . I assume you would wish
int **arr = malloc(m * sizeof(int*));
It would be more clear and shorter if you had used arr[i][j] instead of *(*(arr+i) + j).

Given three nxn arrays, produce the output A+B=C where A, B and C are represented as matrices in the output using C programming language

Assume the matrices to be of the form,
The required output on stdout using C language should be
My code:
#include<stdio.h>
int main(void)
{
int size, i=0, j=0;
printf("Enter the size of the array:");
scanf("%d",&size);
int A[size][size], B[size][size], C[size][size];
//Entering the values in A[][]
printf("enter the elements of Array A:\n");
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
scanf("%d",&A[i][j]);
//Entering the values in B[][]
printf("enter the elements of Array B:\n");
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
scanf("%d",&B[i][j]);
// Calculating C[][]=A[][]+B[][]
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
C[i][j]=A[i][j]+B[i][j];
i=0;
j=0;
while(i<size)
{
if( i==size/2)
printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');
while(j<size)
printf("%d\t",A[i][j++]);
j=0;
while(j<size)
printf("%d\t",B[i][j++]);
j=0;
while(j<size)
printf("%d\t",C[i][j++]);
j=0;
printf("\n\n");
i++;
}
}
But this code only gives the desired output when size=2.
But I need a solution which works for all values of size entered by the user.
you use a tab (8 columns) to go to the next column, so replace
printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');
by
printf("%*c%*c\n\n",8*size-4,'+',8*size,'=');
example :
of course that supposes all numbers need up to 7 columns
If you want to always have 1 line between each line with numbers modify the while to have :
while(i<size)
{
if( i==size/2)
printf("%*c%*c\n",8*size-4,'+',8*size,'=');
else
putchar('\n');
while(j<size)
printf("%d\t",A[i][j++]);
j=0;
while(j<size)
printf("%d\t",B[i][j++]);
j=0;
while(j<size)
printf("%d\t",C[i][j++]);
j=0;
putchar('\n');
i++;
}
producing :
Out of that I encourage you to check scanf always return 1 else you do not detect an invalid input
This code will solve my question,
int size_odd_or_even=size%2;
int check=1;
while(i<size)
{
if( size_odd_or_even==0 && i==size/2 && check==1)
{
printf("\n%*c%*c\n",8*size-4,'+',8*size,'=');
check=0;
continue;
}
if( size_odd_or_even==1 && i== (int)(size/2) )
{
while( j<size-1)
printf("%d\t",A[i][j++]);
printf("%d + ",A[i][j]);
}
else
{
while( j<size)
printf("%d\t",A[i][j++]);
}
j=0;
if( size_odd_or_even==1 && i== (int)(size/2))
{
while( j<size-1)
printf("%d\t",B[i][j++]);
printf("%d = ",B[i][j]);
}
else
{
while(j<size)
printf("%d\t",B[i][j++]);
}
j=0;
while(j<size)
printf("%d\t",Sum[i][j++]);
j=0;
if( size_odd_or_even==0 && !(i==size/2-1) )
printf("\n\n");
else if(size_odd_or_even==1)
printf("\n\n");
i++;
}
This code gives the outputs for size=4 and size=5 respectively as,
If this code can be optimised then please do tell.

I'm trying to add numbers to each new line

I am trying to print out a sequence of numbers and * that form a rectangle depending on the number provided by the user. I managed to print a rectangle with * but I do not know how to incorporate numbers to print out something like:
1*****
12****
123***
1234**
12345*
123456
This is what I have:
#include <stdio.h>
int main{
int i, j;
int num;
printf("Enter a number from 1-9: ");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
for(j=1; j<=num; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Hope this helps!
#include <stdio.h>
int main(){
int i, j;
int num;
printf("Enter a number from 1-9: ");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
for(j=1; j<=i; j++)
{
printf("%d",j);
}
for(;j<=num;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Also,Instead for using 2 loops inside of Master loop,You can Use conditional operators to manipulate output!
Cheers! :)

Using Functions to Pass an Array

Hi, i am using a Function for the first time for my 2-D Array and so far i have been told it is right but i am having a problem with the 'For Loop'.
Specifically repeating the printed statement and numbers until the array is full, and also shows the specific array you are filling. (As the image shows).
This only prints 1 statement with 1 Array, whilst 4 numbers afterwards.. How could i get each number printed after each statement? I have tried the only way i know which made it worse.
Incorrectly Looped printed statement
Code below:
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
DisplayArray(a);
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
for(j=0; j<2; j++)
printf("%d \n" , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==2)
printf("\n");
}
You forgot to properly define the scope of the inner for loops.
The lines
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
are equivalent to:
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
// Outside of both the for loops.
scanf("%d", &a[i][j]);
The indent expresses the intent to a human reader but not to the computer.
You need to use:
for (i=0; i<2; i++)
for(j=0; j<2; j++)
{ // Add
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
} // Add
To make the code more readable add an explicit scope for the outer for loops too.
for (i=0; i<2; i++)
{ // Add
for(j=0; j<2; j++)
{ // Add
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
} // Add
} // Add
Make similar changes to the other loops.
Your for loops are probably not acting as you intended, because without the curly braces, they will only act on the first statement.
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
}
}
DisplayArray(a);
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d \n" , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==2)
printf("\n");
}
}
}
You're missing some curly braces. Change to this:
for (i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf(" %d", &a[i][j]);
DisplayArray(a);
}
and
for(i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("%d \n" , a[i][j]);
if(j==2)
printf("\n");
}
Also, note that if(j==2) will never evaluate to true. When j is equal to 2, the loop is over. You should change this to 1. I would also suggest to remove \n in the innermost printf statement.
Complete code:
#include <stdio.h>
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf(" %d", &a[i][j]);
DisplayArray(a);
}
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("%d " , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==1)
printf("\n");
}
}

Reading a 2-dimensional array of char in C

I'm trying to read an M*N array of chars, but I get some weird characters when printing the array. I couldn't figure out what's wrong with the code, what am I missing here?
int i,j,m,n;
char mat[10][10]
printf("N=");
scanf("%d", &n);
printf("M=");
scanf("%d", &m);
fflush(stdin);
// Read array elements, one by one
for (i=0; i<n; i++)
for (j=0; j<m; j++)
{
printf("mat[%d][%d]=", i+1, j+1);
scanf("%c", &mat[i][j]);
fflush(stdin);
}
// Print matrix
putchar('\n');
for (i=0; i<n; i++);
{
for (j=0; j<m; j++)
printf("%2c", mat[i][j]);
putchar('\n');
}
Example input/output:
N=2
M=2
mat[1][1]=1
mat[1][2]=A
mat[2][1]=2
mat[2][2]=B
╥ ⌠
1
char mat[10][10] -> char mat[10][10];
for (i=0; i<n; i++); -> for (i=0; i<n; i++)
/* delete
for (i=0; i<(n-1); i++)
for (j=0; j<i+1; j++)
printf("%c ", mat[i][j]);
*/
your
scanf("%c", &mat[i][j]);
is catching the newlines. add white space at the beginning of the format
scanf(" %c", &mat[i][j]);
#include<stdio.h>
#include<string.h>
int main (void)
{
int i,j,m,n;
char mat[10][10];
char ch;
printf("N=");
scanf("%d", &n);
printf("M=");
scanf("%d", &m);
// Read array elements, one by one
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
printf("mat[%d][%d]=", i, j);
//Flush standard inputs
while ((ch = fgetc(stdin)) != EOF && ch != '\n')
{
}
mat[i][j]=getchar();
printf("\n");
}
}
printf("printing %d x %d matrix", n , m);
// Print matrix
putchar('\n');
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
printf("mat[%d][%d]=", i, j);
putchar(mat[i][j]);
printf("\n");
}
}
return (0);
}

Resources