Printing a two dimensional array in C - c

I want to print a two dimensional array which I get from another method, see code:
int** getPrint(){
const int EIGHT[7][5] = {
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 }
};
return EIGHT;
}
int main(){
int **ptr;
ptr = getPrint();
return 0;
}
What would be the best method to print this?

Something as the following
#include <stdio.h>
#define N 7
#define M 5
const int ( * getPrint( void ) )[M]
{
static const int EIGHT[N][M] =
{
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 }
};
return EIGHT;
}
int main( void )
{
const int ( *ptr )[M];
int i, j;
ptr = getPrint();
for ( i = 0; i < N; i++ )
{
for ( j = 0; j < M; j++ ) printf( "%d ", ptr[i][j] );
printf( "\n" );
}
return 0;
}
Or you could use a typedef. For example
#include <stdio.h>
#define N 7
#define M 5
typedef const int ( *ArrayPtr )[M];
ArrayPtr getPrint( void )
{
static const int EIGHT[N][M] =
{
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 }
};
return EIGHT;
}
int main( void )
{
ArrayPtr ptr;
int i, j;
ptr = getPrint();
for ( i = 0; i < N; i++ )
{
for ( j = 0; j < M; j++ ) printf( "%d ", ptr[i][j] );
printf( "\n" );
}
return 0;
}

Related

warning: excess elements in array initializer

I'm trying to create 2D array.
int main() {
int stalagmite[10][6] = { { 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 },
{ 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 0, 1, 1, 1, 0, 1, 0, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 } };
printf("%d\n", stalagmite[3][3]);
}
Output as follows:
deneme.c:9:51: note: (near initialization for ‘stalagmite[5]’)
deneme.c:9:54: warning: excess elements in array initializer
9 | { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 } };
Trying to define an array with initializer. When I try to compile it always return a error stated above. I tried to change size, data type but it had no effects.
int stalagmite[10][6] defines an array of 10 arrays of 6 int, whereas your initializer specifies 6 arrays of 10 integers.
The definition should probably be changed to int stalagmite[6][10] = {...} or possibly int stalagmite[][10] = {...}.

print equally spaced elements of a 2D array with printf

I am trying to print a 2d array that has a maximum of 3 digit numbers that are aligned when printed. For example, with a simple printf, it looks like this:
[0, 232, 20, 96, 176, 0, 0]
[0, 0, 24, 0, 0, 176, 0]
[0, 0, 0, 0, 0, 0, 0]
I would like it to be printed with all the commas aligned along the columns with additional whitespace, like this:
[ 0, 232, 20, 96, 176, 0, 0]
[ 0, 0, 24, 0, 0, 176, 0]
[ 0, 0, 0, 0, 0, 0, 0]
How can I do this with printf?
You can use the width prefix to specify the minimum width for the printf conversions: printf("%4d", x); will print int variable x padded to the left with enough spaces to produce at least 4 characters.
If you know the maximum width of any number in the array, you can hardcode this number in the format string. Otherwise you can compute the required width and use %*d and pass an extra argument to specifying the computed width.
Here is a modified version:
#include <stdio.h>
int main(void) {
#define M 3
#define N 7
int a[M][N] = {
{ 0, 232, 20, 96, 176, 0, 0 },
{ 0, 0, 24, 0, 0, 176, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
};
int width = 0;
/* compute the required width */
for (size_t i = 0; i < M; i++) {
for (size_t j = 0; j < N; j++) {
int w = snprintf(NULL, 0, "%d", a[i][j]);
if (width < w) {
width = w;
}
}
}
/* print the arrays */
for (size_t i = 0; i < M; i++) {
printf("[");
for (size_t j = 0; j < N; j++) {
if (j != 0) printf(", ");
printf("%*d", width, a[i][j]);
}
printf("]\n");
}
return 0;
}
Output:
[ 0, 232, 20, 96, 176, 0, 0]
[ 0, 0, 24, 0, 0, 176, 0]
[ 0, 0, 0, 0, 0, 0, 0]
Here you are.
#include <stdio.h>
int main(void)
{
enum { M = 3, N = 7 };
int a[M][N] =
{
{ 0, 232, 20, 96, 176, 0, 0 },
{ 0, 0, 24, 0, 0, 176, 0 },
{ 0, 0, 0, 0, 0, 0, 0 }
};
int width = 4;
for ( size_t i = 0; i < M; i++ )
{
putchar( '[' );
for ( size_t j = 0; j < N; j++ )
{
if ( j != 0 ) putchar( ',' );
printf( "%*d", width, a[i][j] );
}
printf( "]\n" );
}
putchar( '\n' );
return 0;
}
The program output is
[ 0, 232, 20, 96, 176, 0, 0]
[ 0, 0, 24, 0, 0, 176, 0]
[ 0, 0, 0, 0, 0, 0, 0]
You can change the value of the variable width if you want for example to output larger values than values consisting from 3 digits. That is the output is enough flexible.
If you want to place the array output in a separate function then the corresponding function can look the following way provided that the compiler supports variable length arrays.
#include <stdio.h>
void format_output( size_t m, size_t n, int a[m][n], int width )
{
for ( size_t i = 0; i < m; i++ )
{
putchar( '[' );
for ( size_t j = 0; j < n; j++ )
{
if ( j != 0 ) putchar( ',' );
printf( "%*d", width, a[i][j] );
}
printf( "]\n" );
}
}
int main(void)
{
enum { M = 3, N = 7 };
int a[M][N] =
{
{ 0, 232, 20, 96, 176, 0, 0 },
{ 0, 0, 24, 0, 0, 176, 0 },
{ 0, 0, 0, 0, 0, 0, 0 }
};
int width = 4;
format_output( M, N, a, width );
putchar( '\n' );
return 0;
}
The program output is the same as shown above that is
[ 0, 232, 20, 96, 176, 0, 0]
[ 0, 0, 24, 0, 0, 176, 0]
[ 0, 0, 0, 0, 0, 0, 0]
use this format of printf printf("%4d", arr[i][j]);
int main()
{
int arr[3][7] = { {0, 232, 20, 96, 176, 0, 0}
,{0, 0, 24, 0, 0, 176, 0}
,{0, 0, 0, 0, 0, 0, 0} };
for (int i = 0; i < 3; i++)
{
printf("[");
for (int j = 0; j < 7; j++)
{
if (j < 6)
printf("%4d,", arr[i][j]);
if (j == 6)
printf("%4d", arr[i][j]);
}
printf("]");
printf("\n");
}
}
PS:amount of space can be changed as needed ,with changing "%4d".

How to copy and assign an array of arrays?

int zero[5][4] = {
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
};
int m1[5][4] = {
{ 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 }
};
//errors here
m1 = zero;
m1 = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
m1[0] = { 0, 0, 0, 0 };
Is there no syntax for this? Do I have to use a loop with indexing to achieve this?
In C, arrays are not assignable or can't be on the left side of assignment (=) operator. Use memcpy.
memcpy(m1, zero, sizeof(zero)); // Assuming size of 'm1' is no less than the size of 'zero'
You can use memset to set an array with a constant value like 0 or -1
memset(m1, 0, sizeof(m1));

How to tag taken seats in Cinema. C Programming

First I want to say that I am a beginner in C and in programming at all.
C is my first language and I find it very interesting.
I am writing a program that will simulate a cinema software. I mean you choose movie, time and you choose seats.
I am done with selecting the movie and time but I have a problem with the seats.
The thing I am trying to do is when you select Row and Column, somehow to print out in the console which seat is taken (changing the color, increasing the font or something like that)
Here is my code:
void SeatSelection()
{
int row = 0;
int column = 0;
int i, j;
printf("\t\t\t\tSCREEN\n\n\n\n\n\n\n");
int A[11][11] = {
{ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 7, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
};
for (i = 0; i < 10; i++)
{
for (j = 0; j < 11; j++)
printf("%d\t", A[i][j]);
printf("\n\n");
}
do
{
printf("Choose seat: Row and Column\n");
scanf("%d %d", &row, &column);
if ((row<1 || row>10) && (column<1 || column>10)) printf("Wrong choice, try again\n");
} while ((row<1 || row>10) && (column<1 || column>10));
}
Thanks for the help in advance :)
Try using a second table taken[][]. For a seat (row, column) taken[row][column] is 1 if the seat is taken, else it is 0. Code:
#include <stdio.h>
void SeatSelection()
{
int row = 0;
int column = 0;
int i, j;
int A[11][11] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
};
int taken[11][11] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
printf("============= The Cinema ==============\n");
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10; j++)
printf(" %d ", A[i][j]);
printf("\n");
}
fflush(stdout);
do
{
printf("Choose seat: Row and Column\n");
fflush(stdout);
scanf("%d %d", &row, &column);
if ((row<1 || row>10) && (column<1 || column>10)) printf("Wrong choice, try again\n");
if(taken[row][column]) printf("This seat is taken, try again\n");
else {
taken[row][column] = 1;
printf("======== The Cinema =========\n");
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10; j++) {
if(taken[i][j] == 0)
printf(" %d ", A[i][j]);
else
printf("[%d] ", A[i][j]);
}
printf("\n");
}
}
fflush(stdout);
} while (true);
}
int main() {
SeatSelection();
return 0;
}
Edit
I have made some alterations to your code, but i think you get the idea :) If you don't understand something just tell me...

2 Dimensional Array printing messed up

I was making a program that will print out the periodic table with [ ] for an element, and [*] for the element you're searching for...
void printmap(int x, int y)
{
int a, b, table[9][18] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
}; // 0 - Empty, 1 - [ ], 2 - [*]
table[x][y] = 2;
b = 0;
printf("\n*******************\n\n");
for( a = 1, printf(" \t"); a < 19; a++)
{
printf("%3d", (b + a));
}
printf("\n");
for(a = 0; a < 9; a++)
{
printf("\n%2d\t", a + 1);
for(b = 0; b < 18; b++)
{
switch(table[a][b])
{
case 0 :
printf(" ");
case 1 :
printf("[ ]");
break;
case 2 :
printf("[*]");
break;
default :
printf("");
}
}
}
}
For some reason, the output is messed up... ( If I pass the argument for helium, which should be 0 and 18 this is what I get : http://pastebin.com/YjhjzSi8
What am I doing wrong?
Thank you
That's not a 2D array, just because you make it LOOK like a 2D array, doesn't mean it is one.
This what you have
table[9][18] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
...
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
};
You should add more brackets accordingly
table[9][18] = {
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1),
....
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}};
Been a while since I used C++, but, this is not how you initialize a 2D array. In fact I'm surprised it even compiles. Correct way is something like:
table[0] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
table[1] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
table[2] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
...
Another example: 2D array values C++
As the other respondents have said, you need to initialize your table array correctly.
Also, you have missed a "break" in your switch/case statemet, and you also missed printing a newline at the end of each row. I took the liberty of renaming your variables a,b to row,col and fixed your code, this should do what you want,
void printmap(int x, int y)
{
int row=0, col=0;
int was = table[x][y];
table[x][y] = 2;
col = 0;
printf("\n*******************\n\n");
printf(" \t");
row=0; //you were not initializing row
for( col = 0; col < 18; col++)
{
printf("%3d", (col+row+1));
}
printf("\n");
for(row = 0; row < 9; row++)
{
printf("\n%2d\t", row + 1);
char* rc = " ";
for(col = 0; col < 18; col++)
{
switch(table[row][col])
{
case 0 : rc=(" "); break; //was missing break
case 1 : rc=("[ ]"); break;
case 2 : rc=("[*]"); break;
default: rc=(" "); break; //was missing break
}
printf("%s",rc);
}
printf("\n"); //missing print newline
}
table[x][y] = was;
}
And here is the table, as others have suggested,
int table[9][18] = {
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }
}; // 0=Empty, 1=[ ], 2=[*]

Resources