Printing 2d array box - c

I am new to programming. Figuring on how can I print out the box using for loop so it makes a big box? I had attached the sample below. I really need help.
#include <stdio.h>
int main()
{
int a;
printf("\n --- \n");
for(a=1;a<=1;++a)
printf("\n| |\n");
printf("\n --- ");
return 0;
}
Example output:

Something like this could work. You need basic understanding of nested loops to be able to do this question.
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char const *argv[]) {
int rows, cols, i, j;
printf("Enter rows for box: ");
if (scanf("%d", &rows) != 1) {
printf("Invalid rows\n");
exit(EXIT_FAILURE);
}
printf("Enter columns for box: ");
if (scanf("%d", &cols) != 1) {
printf("Invalid columns\n");
exit(EXIT_FAILURE);
}
printf("\n2D Array Box:\n");
for (i = 1; i <= rows; i++) {
for (j = 1; j <= cols; j++) {
printf(" --- ");
}
printf("\n");
for (j = 1; j <= cols; j++) {
printf("| |");
}
printf("\n");
}
/* bottom "---" row */
for (i = 1; i <= cols; i++) {
printf(" --- ");
}
return 0;
}

first character(' ') and repeat string ("--- ")
first line and repeat contents line and bar line.
#include <stdio.h>
#define MARK "X O"
//reduce code
#define DRAW_H_BAR()\
do {\
putchar(' ');\
for(int i = 0; i < cols; ++i)\
printf("%s ", h_bar);\
puts("");\
}while(0)
void printBoard(int rows, int cols, int board[rows][cols]){
const char *h_bar = "---";
const char v_bar = '|';
DRAW_H_BAR();//first line
for(int j = 0; j < rows; ++j){
//contents line
putchar(v_bar);
for(int i = 0; i < cols; ++i)
printf(" %c %c", MARK[board[j][i]+1],v_bar);
puts("");
DRAW_H_BAR();//bar line
}
}
int main(void){
int board[8][8] = {
{1,0,1,0,1,0,1,0},
{0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,-1,0,-1,0,-1,0,-1},
{-1,0,-1,0,-1,0,-1,0},
{0,-1,0,-1,0,-1,0,-1}
};
int rows = sizeof(board)/sizeof(*board);
int cols = sizeof(*board)/sizeof(**board);
printBoard(rows, cols, board);
}

Related

Print half left pyramid/triangle and half right pyramid/triangle together

Newb Here!
Trying some things with C Language,
I tried to print Half left triangle/pyramid and Half right triangle/pyramid together in one print.
heres the code!
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, rows, k;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 1; i <= rows; ++i) // outer loop
{
for (j = 1; j <= i; ++j) // inner loop
{
printf ("*"); // print the Star
}
printf ("\n");
}
{
for (i = 1; i <= rows; i++)
{
for (j = i; j < rows; j++)
{
printf(" ");
}
for (k = 1; k <= i; k++)
{
printf("*"); // print the Star
}
printf ("\n");
}
}
}
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, rows, k;
int hollow_spacing = 2;
int left_padding = 10;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 0; i < rows; i++)
{
int curr_left_pad = left_padding + rows - i;
while(curr_left_pad-- > 0){
printf (" ");
}
for (int j = i; j >= 0; j--)
printf ("*");
for (int j = 0; j < hollow_spacing; ++j)
printf (" ");
for (int j = i; j >= 0; j--)
printf ("*");
printf ("\n");
}
}

Star pyramid program in c

I have output in image, but code is not proper
i want code for the given output
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
The question was a little difficult to understand, I'm assuming you're asking how to get the output as described in the image. This code does that:
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i=0; i<rows; ++i) {
for(int j=0; j<rows*2-1; ++j) {
if (j <= i || j >= rows*2-2-i) {
printf("* ");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}

Converting matrix to an array in C

How can I copy the elements from a matrix,entered by user,to an array? I tried this, but it didn't work:
#include<stdio.h>
int main(){
int m[20][20],a[400],c=0;//max dimensions;
scanf("%d %d",&M,&N);//dimensions of matrix;
for(i=0;i<M;i++{
for(j=0;j<N;j++{
scanf("%d", &m[i][j]);
for(c=0;c<M*N;c++)
a[c]=m[i][j];
}}}
Don't know why you want to store both the matrix format and the array format, but anyway here is a code that should do the trick with also the data output to show the result:
#include <stdio.h>
#include <stdlib.h>
#define R 20 //max rows
#define C 20 //max columns
int main() {
int m[R][C]; //matrix
int a[R*C]; //array
int r, c; //user matrix size
int i, j; //iterators
printf("insert row size: ");
scanf("%d", &r);
printf("insert column size: ");
scanf("%d", &c);
if(r > R || c > C) {
printf("Invalid sizes");
return -1;
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("insert value for row %d column %d: ", i + 1, j + 1);
scanf("%d", &m[i][j]);
a[(c * i) + j] = m[i][j];
}
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("%d ", m[i][j]);
}
printf("\n");
}
for(i = 0; i < r * c; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Note that I also added some checks for the user data, avoiding to generate a matrix bigger that the maximum size. Also you don't need separate loops but it can be done all together.
Also please post a code that is compilable and can be run, as explained here: https://stackoverflow.com/help/how-to-ask

how to make a christmas tree using loop in c program

I'm a freshmen student and we have an activity in intro pro.. We were tasked to create a Christmas tree using a loop...
I have my code here:
#include<stdio.h>
int main ()
{
int rows,a,b,space;
clrscr();
printf("Enter a number of rows:");
scanf("%d",&rows);
space=rows-1
for(b=space;b>=1;b--)
{
for(a=rows;a>=1;a--)
space--;
printf("");
for(a=2*(rows-b)-1;a>=1;a--)
printf("*",a);
printf("\n");
space = space-1;
}
getche();
return 0;
}
This code was given to us by our professor... the program runs, but the output is wrong. Can you help me?
when i run this program, the output was like this:
*
***
*****
******
*******
You have to find a pattern. Say you want a tree with n rows. Last row is going to have 2n-1 stars. Row before it will have 2n-3 and so on. To print a row, first you print a number of spaces, then a number of stars. For last row, you print 0 spaces and 2n-1 stars. For row before it, you print 1 space and 2n-3 stars and so on.
for(int i = 0; i < n; i++)
{ for(int j = i + 1; j < n; j++)
printf(" ");
for(int j = 0; j <= 2*i; j++)
printf("*");
if(i < n - 1) puts("");
}
The Code is a little bit to messed up for me, but this should work:
#include<stdio.h>
int main() {
/*Variables*/
int rows, starNumber, spaceNumber;
int rowCount, spaceCount, starCount, treeTrunkCount, treeTrunkSpaceCount;
printf("Enter Rows:\n>");
scanf("%d",&rows);
for(rowCount = 1; rowCount <= rows; rowCount++) {
starNumber = rowCount * 2 - 1;
spaceNumber = rowCount + rows - starNumber;
for(spaceCount = 0; spaceCount < spaceNumber; spaceCount++)
printf(" ");
for(starCount = 0; starCount < starNumber; starCount++)
printf("%c",'*');
printf("\n");
}
for(treeTrunkCount = 0; treeTrunkCount < 3; treeTrunkCount++) {
for(treeTrunkSpaceCount = 0; treeTrunkSpaceCount < (rows * 2 + 1)/2; treeTrunkSpaceCount++)
printf(" ");
printf("%c\n",'*');
}
}
This is the simplest solution to your program..
#include <stdio.h>
int main()
{
int i=-1,j=0,rows;
printf("Enter Rows:\n");
scanf("%d",&rows);
while(j++<rows) // Moving pointer for the first '*'
{
printf(" ");
}
printf("*"); // This prints the first '*'
while(++i<rows)
{
for(j=-2;++j<rows-i;) // This loop will print Spaces before '*' on each row
printf(" ");
for(j=0;++j<2*i;) // This loop will print * on each row
{
printf("*");
}
printf("\n"); // This printf will take you to the next Line
}
}
This is the shortest and simplest solution for your question:
#include<stdio.h>
#include<conio.h>
void main(){
int count;
int i,j;
printf("enter the numbers of line");
scanf("%d",&count);
for(i=1;i<=count;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
getch();
}
You forgot a space between "".
for(a=rows;a>=1;a--)
space--;
printf("");
should be
for(a=rows;a>=1;a--)
space--;
printf(" ");
#include <stdio.h>
int main() {
int n = 50;
for (int i = 0; i <= n; ++i) {
for (int k = i; k < n; ++k)
printf(" ");
for (int j = 0; j < i; ++j)
printf("*");
for (int j = 1; j < i; ++j)
printf("*");
printf("\n");
}
for (int l = 1; l < n/2; ++l) {
for (int i = 1; i < n; ++i)
printf(" ");
printf("[|]\n");
}
return 0;
}
A simple tree can be made up with for loop, Christmas may need more symbol...
//Linux C program to print a tree
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
int pcenter(char *s) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
int ct = w.ws_col;
int sl = strlen(s) / 2;
printf("%*s%*s\n", ct / 2 + sl, s, ct / 2 - sl, "");
return 0;
}
int ptree(char s, char t, int l, int r) {
int i;
for (i = 1; i <= l; i++) {
int j = 2 * i - 1;
char *p = malloc(j);
memset(p, s, j);
pcenter(p);
free(p);
}
for (i = 1; i <= r; i++) {
int j = 1;
char *p = malloc(j);
memset(p, t, j);
pcenter(p);
free(p);
}
return 0;
}
int main() {
// system("clear");
ptree('*', '|', 10, 5);
return 0;
}
#include<stdio.h>
main()
{
int n,i, j, space=1;
printf("Enter the number of rows: ");
scanf("%d",&n);
space=n-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=space;j++)
{
printf(" ");
}
space--;
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
for(i=1;i<=n-3;i++)
{
for(j=1;j<=10;j++)
{
printf(" ");
}
for(j=1;j<=1;j++)
{
printf("*");
}
for(j=1;j<=1;j++)
{
printf("*");
}
for(j=1;j<=1;j++)
{
printf("*");
}
printf("\n");
}
}
#include<stdio.h>
int main()
{
int i,j,k,l=1,a,b;
for(i=8;i>=0;i--)
{
for(j=0;j<=i;j++)
{
printf(" ");
}
k=0 ;
while(k<l)
{
printf("*");
k=k+1;
}
l=l+2;
printf("\n");
}
i=8;
for(b=0;b<=3;b++)
{
for(a=0;a<=i-1;a++)
{
printf(" ");
}
printf("***");
printf("\n");
}
}

How to print a 2d array with a function in C?

I'm trying to print a 2d array with a function, but I keep getting the error "pointer expected"
I'm trying to make a battleship-type grid. I'm fine with printing out the co-ordinate row and column, but I can't actually get the 2d array (which contains "." in every element) to print at all.
Any help would be appreciated, I'm very new to this. Thanks! :)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int length;
int width;
int i;
int j;
char invisible_board;
void board_setup(int *rows, int *columns){
char *invisible_board[*rows][*columns];
char *player_board[*rows][*columns];
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
invisible_board[i][j] = "."; //Sets all elements in hidden board to water
}
}
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
player_board[i][j] = ".";
}
}
}
void display(int *rows, int *columns, char *invisible_board){
printf(" ");
for (i=1; i < *rows +1;i++){
printf("%d ",i);
}
printf("\n"); //Prints top row of co-ordinates
for (i=1; i < *columns+1;i++){
printf("%d ",i);
for (j=0;j < *columns;j++){ //Prints left column of co- ordinates and rows of game board
printf(" %c ",invisible_board[i-1][j]);
}
printf("\n");
}
}
int main(void){
printf("Please enter the amount of rows in your board\n");
scanf("%d",&length);
printf("Please enter the amount of columns in your board\n");
scanf("%d",&width);
board_setup(&length,&width);
display(&length,&width,&invisible_board);
return (0);
}
this is the simplest changes I could make to your code to get you to working code.... now.... this isn't good code yet. But gets you started.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int length;
int width;
int i;
int j;
char invisible_board[100][100]; // dynamically allocate....
char player_board[100][100]; // dynamically allocate....
void board_setup(int *rows, int *columns){
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
invisible_board[i][j] = '.'; //Sets all elements in hidden board to water
}
}
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
player_board[i][j] = '.';
}
}
}
void display(int *rows, int *columns){
printf(" ");
for (i=1; i < *rows +1;i++){
printf("%d ",i);
}
printf("\n"); //Prints top row of co-ordinates
for (i=1; i < *columns+1;i++){
printf("%d ",i);
for (j=0;j < *columns;j++){ //Prints left column of co- ordinates and rows of game board
printf(" %c ",invisible_board[i-1][j]);
}
printf("\n");
}
}
int main(void){
printf("Please enter the amount of rows in your board\n");
scanf("%d",&length);
printf("Please enter the amount of columns in your board\n");
scanf("%d",&width);
board_setup(&length,&width);
display(&length,&width);
return (0);
}

Resources