Star pyramid program in c - 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;
}

Related

how can I make a loop print out missing elements in my int array?

I've been trying to solve an assignment where you:
Enter the size N of an array of ints
Enter the max value M of the ints that you want to populate the array with
Enter N values that are of a value between 1 and M, into a second array.
compare these two and print out the missing numbers...
Like this:
Size of array? 10 // => N = 10
Max value in array? 8 // => M = 8
Please enter 10 values between 1 and 8:
4 1 3 1 7 3 4 4 6 1
Missing values: 2 5 8
for some reason, my for loop just prints out all the numbers between 1 and M instead, no matter what I try... What am I missing??
code:
#include <stdio.h>
int main(void)
{
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int table[aSize];
int values[mValue];
for (int i = 0; i < aSize; i++)
{
table[i] = i+1;
if ((i+1) > mValue)
{
table[i] = 0;
}
}
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < mValue; i++)
{
scanf(" %d", &values[i]);
}
for(int i = 0; i < aSize; i++)
{
for (int j = 0; j < mValue; j++)
{
if(table[i] != values[j] && table[i] != 0)
{
printf("%d ", table[i]);
break;
}
}
}
}
#include <stdio.h>
int main()
{
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int table[aSize];
int values[aSize]; // not 'mSize' because it is just max value not size of array
for (int i = 0; i < aSize; i++)
{
table[i] = i+1;
if ((i+1) > mValue)
{
table[i] = 0;
}
}
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < aSize; i++)
{
scanf(" %d", &values[i]);
}
for(int i = 0; i < aSize; i++)
{
int flag=0;
for (int j = 0; j < aSize; j++)
{
if(table[i] == 0 || table[i] == values[j]) // numbers in common or zero
{
flag=1;
break;
}
}
if(flag == 0) printf("%d",table[i]); // missing numbers
}
}
In this code, I'm creating an extra array to store the occurance of the variable, then printing it. 1 means present 0 means not present.
#include <stdio.h>
int main(void) {
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int table[aSize];
int values[mValue+1];
for (int i = 0; i <= mValue; i++)
{
values[i]=0;
}
values[0]=1;
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < aSize; i++)
{
scanf(" %d", &table[i]);
if(table[i]>mValue || table[i]<1){
printf("Enter numbers in given range");
return 0;
}
}
for(int i = 0; i < aSize; i++)
{
values[table[i]]=1;
}
printf("Missing values:\n");
for(int i = 1; i <= mValue; i++)
{
if(!values[i])
printf("%d\n",i);
}
return 0;
}
This is better version of the above logic.
#include <stdio.h>
int main(void) {
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int temp;
int values[mValue+1];
values[0]=1;
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < aSize; i++)
{
scanf(" %d", &temp);
if(temp>mValue || temp<1){
printf("Enter numbers in given range");
return 0;
}
values[temp]=1;
}
printf("Missing values:\n");
for(int i = 1; i <= mValue; i++)
{
if(!values[i])
printf("%d\n",i);
}
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

8 Queens diagonal checking

At the moment I am creating a program where the user is asked to place the queens for the 8 Queens Problem. Right now I have nearly created the program, but I'm stuck on how to make the program check diagonally.
This is the (unfinished)code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int check_r_c(int**chess,int*p,int N,int M)
{
int times=0;
for(int i=0; i<N; i++)
{
times=0;
for(int j=0; j<M; j++)
{
if(chess[i][j] == 1)
times++;
}
if( times != 1)
{
*p=1;
return 1;
}
}
for(int j=0; j<M; j++)
{
times=0;
for(int i=0; i<N; i++)
{
if(chess[i][j] == 1)
times++;
}
if( times != 1)
{
*p=1;
return 1;
}
}
*p=0;
return 0;
}
int main()
{
int N,M;
printf("Give the number of rows: \n");
scanf("%d",&N);
printf("Give the number of columns: \n");
scanf("%d",&M);
int**chess = malloc(N*sizeof(int));
int i,j,ch;
int*ptr;
ptr=&ch;
if(chess==NULL)
{
printf("\nMemory cannot be allocated\n");
return 1;
}
for(i=0; i<N; i++)
{
if((chess[i] = malloc(M*sizeof(int)))==NULL)
{
printf("\nMemory cannot be allocated\n");
return 1;
}
}
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
chess[i][j]= 0;
for(int k=0; k<N; k++)
{
printf("Give the position of the %d queen\n",k+1);
scanf("%d",&i);
scanf("%d",&j);
if(chess[i][j] == 1)
{
printf("You cant put 2 queens in the same place!!!\n" );
return 0;
}
chess[i][j] = 1;
}
check_r_c(chess,ptr,N,M);
if(ch == 0)
printf("Solution is correct!");
else
printf("Solution is incorrect!");
for(int i=0; i<N; i++)
free(chess[i]);
free(chess);
}
The logic for checking diagonal for a queen placed is (p,q) is to check for all the places located at (p+i,q+i) for all i, where both p+i and q+i are within the board. For negative side, use (p-i,q-i).

Printing 2d array box

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);
}

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");
}
}

Resources