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);
}
Related
Firstly, a number N has to be input, and the matrix is of NxN dimensions then. The diagonal of the matrix has to be all 0, the elements of the matrix above the diagonal have to be from 1 to N diagonally, and the elements under the diagonal need to be filled from -1 to -N also diagonally. It should be something like this (if N=5):
But the problem that I have is that I print it out like this:
and I don't know how to fix it.
This is the code that I have:
`#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d",&N);
int k=0;
for(i=0; i<N; i++){
for(j=0; j<N; j++){
if(i==j){
matrix[i][j]=0;
}
else if(j>i && i!=j){
for(k=0; k<N; k++){
matrix[k-1][j]=k;
}
}
else if(j<i && i!=j){
for(k=0; k<N; k++){
matrix[i][k-1]=-k;
}
}
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}`
I would really appreciate the help.
Here is you code modified, notice that 3 inner loops are removed with only one line.
second, you ask for the number N, however due to statically initialisation to maximum 50, you should as well verify that it is not. otherwise segmentation fault will happen.
or if you want to allow N >50 then better to do dynamic allocation on matrix.
#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d", &N);
if (N > 50){
printf("N should be smaller than 50, \n");
N = 50;
}
for(i=0; i<N; i++){
for(j=0; j<N; j++){
matrix[i][j]= j - i;
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}
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).
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.
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");
}
}
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;
}