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

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

Related

Why am I experiencing an infinite loop here?

My code never stops asking for input so I think I must've made an infinite loop, but I can't find where the error is. I've also noticed that when inserting the input line by line, it prints the result of one loop after inserting the first line of the second loop, which seems incorrect to me. Please help me debug. (For further context, the code is supposed to receive a number we'll call n, and then scan 3n more lines, which are basically n bundles of 3 similar lines. The 2nd and 3rd lines are two words with the same num of characters, and the 1st line is that num. The output is whether or not these words are anagrams.)
#include <stdio.h>
int main() {
int n, l;
scanf("%d\n", &n);
for (int i = 1; i <= n; i++) {
scanf("%d\n", &l);
char A[l], B[l];
for (int j = 0; j < l; j++) {
scanf("%c", &A[j]);
scanf("\n");
}
for (int j = 0; j < l; j++) {
scanf("%c", &B[j]);
scanf("\n");
}
for (int k = 0; k < l; k++) {
int result = 0;
for (int j = 0; j < l; j++) {
if (A[k] == B[j]) {
result = 1;
}
}
if (!result) {
printf("\nNO\n");
return 0;
}
}
printf("\nYES\n");
}
}
Example:
input:
2
6
listen
silent
4
Evil
live
output:
YES
NO
It's because you are asking for input over and over and over again,
Not sure what you are trying to achieve, but start by removing the extra scanfs
try this,
#include <stdio.h>
int main() {
int n, l;
scanf("%d\n", &n);
for (int i = 1; i <= n; i++) {
char A[l], B[l];
for (int k = 0; k < l; k++) {
int result = 0;
for (int j = 0; j < l; j++) {
if (A[k] == B[j]) {
result = 1;
}
}
if (!result) {
printf("\nNO\n");
return 0;
}
}
printf("\nYES\n");
}
}
Your scanf calls wait for an extra \n which requires more input to enter.
To fix this, remove the \n from your scanf calls. Also remove the extra calls when you enter A and B:
I have added some debug code to demonstrate where you are in your program execution while you enter your input.
#include <stdio.h>
int main() {
int n, l;
int res;
res=scanf("%d", &n);
printf ("res=%d, n=%d\n", res, n);
for (int i = 1; i <= n; i++) {
res = scanf("%d", &l);
printf ("res=%d, l=%d\n", res, l);
// char A[l], B[l];
char A[l+1], B[l+1];
for (int j = 0; j < l; j++) {
scanf(" %c", &A[j]);
}
printf ("A done\n");
for (int j = 0; j < l; j++) {
scanf(" %c", &B[j]);
}
printf ("B done\n");
A[l] = 0; B[l] = 0;
printf ("A=\"%s\" - B=\"%s\"\n", A, B);
...
Now your input should work properly.
But your code also contains another error.
You will treat "12344" and "11234" as correct match wchich is wrong.
To fix this you need to remove each matching character from B:
for (int k = 0; k < l; k++) {
int result = 0;
// We compare A[k] with the remaining characters in B only
for (int j = k; j < l; j++) {
if (A[k] == B[j]) {
result = 1;
B[j] = B[k]; // Replace matching character with non-matching character we checked earlier.
break;
}
}
if (!result) {
printf("\nNO\n");
return 0;
}
}
printf("\nYES\n");
This code stops as soon as the first match is found and removed that character from B.
The entries in B are rearranged to keep the unused entries at the end.
The full code looks like this:
#include <stdio.h>
int main() {
int n, l;
int res;
res=scanf("%d", &n);
printf ("res=%d, n=%d\n", res, n);
for (int i = 1; i <= n; i++) {
res = scanf("%d", &l);
printf ("res=%d, l=%d\n", res, l);
// char A[l], B[l];
char A[l+1], B[l+1];
for (int j = 0; j < l; j++) {
scanf(" %c", &A[j]);
}
printf ("A done\n");
for (int j = 0; j < l; j++) {
scanf(" %c", &B[j]);
}
printf ("B done\n");
A[l] = 0; B[l] = 0;
printf ("A=\"%s\" - B=\"%s\"\n", A, B);
for (int k = 0; k < l; k++) {
int result = 0;
for (int j = k; j < l; j++) {
if (A[k] == B[j]) {
result = 1;
B[j] = B[k];
break;
}
}
if (!result) {
printf("\nNO\n");
return 0;
}
}
printf("\nYES\n");
}
}
output:
~/stackoverflow$ ./test
4
res=1, n=4
2
res=1, l=2
12
A done
21
B done
A="12" - B="21"
YES
6
res=1, l=6
liver1
A done
evil1r
B done
A="liver1" - B="evil1r"
YES
3
res=1, l=3
111
A done
111
B done
A="111" - B="111"
YES
4
res=1, l=4
abcd
A done
dcbb
B done
A="abcd" - B="dcbb"
NO

C language 2d array fill diagonal with numbers from 1 to n

I have a 2d array filled with 0's and i'm trying to fill the main diagonal with numbers from 1 to n, this is the main code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int m, n, i, j;
printf("Number of rows and columns:");
scanf("%d", &n);
int a[n][n];
for (i = 0; i < n; i++)
for(j = 0; j < n; j++)
a[i][j] = rand() % 1;
printf("The matrix is:\n");
for (i = 0; i < n; i++)
{
printf(" \n ");
for(j = 0; j < n; j++)
{
printf(" %d\t ", a[i][j]);
}
}
}
What I've tried to do is to fill the diagonal manually, but that's not what I want to do. I want to make it fill itself automatically. I need to do it without using any functions.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i][j] = ((i == j) * (i + 1));
}
}
The simplest way is to add this part after you fill the matrix with zeros.
for (i = 0; i < n; i++)
arr[i][i] = i + 1;

Add or substract negative numbers in C

So i've been trying to do a code that adds the integers of a row in an array (this integers can be negative for example)
So you will have something like this:
input:
3 2 (Number of rows and columns)
12 7
-3 6
-2 -5
output:
19
3
-7
What I've done is this:
#include <stdio.h>
int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("%d\n",sum);
sum = 0;
}
}
I can't find a way to "add" negative numbers, what should I change/add to this code? Thanks again :)
#include <stdio.h>
int main (){
int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
sum = sum + array[i][j] ;
}
printf("%d\n",sum);
sum = 0;
}
return 0;
}

Newtons divided difference

The problem is this http://www.spoj.com/problems/CMPLS/
I thought of implementing newtons divided difference and find out the polynomial. But I am not able to get the correct answer. Some basic test cases are passing but some are not.
# include <stdio.h>
int main()
{
int i, j, k, t, m, x[100][100], s, c, n;
long ans, mul;
scanf ("%d", &t);
while (t--) {
scanf ("%d%d", &s, &c);
n = s;
for (i = 0; i < n; i++)
scanf ("%d", &x[i][0]);
for (j = 1; j < n; j++)
for (i = 0; i < n; i++) {
x[i][j] = x[i+1][j-1] - x[i][j-1];
}
k = n;
printf("\n");
for (i = 0; i < n; i++) {
for(j = 0; j < k; j++)
printf ("%d ", x[i][j]);
printf ("\n");
k--;
}
printf ("\n\n");
for (m = 1; m <= c; m++) {
ans = x[0][0];
for ( i = 1; i < n - 1; i++) {
mul = x[0][i];
for (j = 1; j <= i; j++)
mul = mul * (s + m - j);
ans = ans + mul;
}
printf ("%ld ", ans);
}
printf ("\n");
}
return 0;
}

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