How to print diagonal star pattern in C - c

I'm confused to print this type of pattern given below:
* *
* *
*
* *
* *
I've tried this:
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of base\n>>> ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j + n; j++)
{
if (// ??? )
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
but I don't know what is the condition of if statement
Help Me to solve this please

The pattern consists of exactly n * 2 - 1 rows and columns. So you can run an outer loop to iterate through rows with structure for(i=1; i<= count; i++); where count = n * 2 - 1. Each row contains exactly n * 2 - 1 columns. So, run inner loop as for(j=1; j<=count; j++). And inside this loop we need stars to be printed when row and column number both are equal (i.e. print star whenever if(i == j)) and if(j == count - i + 1). For example:
#include <stdio.h>
int main() {
int i, j, n;
int count;
printf("Enter n: ");
scanf("%d", &n);
count = n * 2 - 1;
for (i = 1; i <= count; i++) {
for (j = 1; j <= count; j++) {
if (j == i || (j == count - i + 1)) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}

To not change any code and answer your question "what goes in the if"
n - 1 - i == j || j == i
this does

This will work fine !! :)
#include<iostream>
using namespace std;
int main()
{
int n,m=0;
int m2=0;
cin>>n;
int f=-1,l=n;
for(int i=0;i<n;i++)
{
f=f+1;
l=l-1;
for(int j=0;j<n;j++)
{
if(j==f || j==l) cout<<"*";
else cout<<" ";
}
cout<<endl;
}
}

Related

Printing a christmas tree using stars and dots in C

I'm trying to write a program that prints out a christmas tree that looks like this:
The user inputs the height, in this example the height is 6. If the input is in range from 0 to 3, the height should be 3, because otherwise it's not printable, and if the input is less than 0, the program should terminate.
My code for some odd reason is infinitely printing the 'Input height'. Where is the error?
Here's my code snippet:
#include <stdio.h>
void main(){
int i, j, n, s;
while (1){
printf("Input height: ");
scanf("%d", &n);
if (n < 0) break;
if (n == 0 || n == 1 || n == 2 || n == 3)
s == 3;
else
s == n;
for (i = 0; i < s; i++){
for (j = 0; j < 2*s - 1; j++){
if (j > s - (i - 1) && j < (s + (i - 1)) - 1)
printf("*.");
if (j == s + (i - 1))
printf("*");
else
printf(" ");
}
printf("\n");
}
for (j = 0; j < 2*s - 1; j++){
if (j == s - 1 || j == s || j == s + 1)
printf("*");
else
printf(" ");
}
}
}
The lines: s == 3; and s == n; do absolutely nothing.
== is a comparison, not an assignment.
Here is much better code:
#include <stdio.h>
int main(void) {
int n = 8;
char row[2*n];
for( int i=0; i<2*n-1; i+=2 )
{
strcpy(row+i, "*.");
}
for(int i=0; i<n; ++i)
{
printf("%*.*s\n", n+i+1, 2*i+1, row);
}
printf("%*s\n", n+2, "***");
return 0;
}
Result:
Success #stdin #stdout 0s 5464KB
*
*.*
*.*.*
*.*.*.*
*.*.*.*.*
*.*.*.*.*.*
*.*.*.*.*.*.*
*.*.*.*.*.*.*.*
***
With a little creativity, I made the program even shorter with only a single for-loop.
#include <stdio.h>
int main(void) {
int n = 8;
char row[2*n];
strcpy(row, "*");
for( int i=0; i<n; ++i )
{
printf("%*s\n", n+i, row);
strcat(row, ".*");
}
printf("%*s\n", n+1, "***");
return 0;
}
As mentioned by others there are some issues with you mixing up == and =.
I will be posting a version that prints out the christmas tree but leaves out the . that you also want to include, as you should be able to finish it yourself.
#include <stdio.h>
int main()
{
int i, j, n, s;
while (1)
{
printf("Input height: ");
scanf("%d", &n);
// if input is negative, exit
if (n < 0)
{
break;
}
// if input is 0,1,2 or 3 change to 3
if (n == 0 || n == 1 || n == 2 || n == 3)
{
s = 3;
}
else
{
s = n;
}
// loop through each row
for (i = 0; i < s; i++)
{
// loop through each column
for (j = 0; j < 2 * s - 1; j++)
{
// if column is within the tree print a star
if (j >= s - i - 1 && j <= s + i - 1)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
// print base of tree
for (j = 0; j < 2 * s - 2; j++)
{
// if column is part of base print star
if (j == s - 2 || j == s - 1 || j == s)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
A simple solution:
#include <stdio.h>
int main(){
int i=0, j=0, n=0, s=0;
while (1){
printf("Input height: ");
scanf("%d", &n);
printf("\n");
if (n < 0) break;
s = (n <= 3) ? 3 : n;
for (i=0; i < s; ++i){ // rows
for (j=0; j < s-i; ++j) // white spaces
printf(" ");
for (int k=0; k < i; ++k) // *.
printf("*.");
printf("*\n"); // always, unique or last *
}
for (i=0; i < s-1 ; ++i)
printf(" ");
printf("***\n\n");
}
return 0;
}

Finding the highest frequency of an array and all elements which have that frequency

I'm asked to find the highest frequency from an array of elements and all elements with said frequency. My code seem to work just fine but it seems to have a mistake somewhere when i submit it. Can anyone help me find the error?
Format Input:
The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤i≤N) which indicate ith element in the array.
Format Output:
Consists of T lines where each line has the format “Case #X: Y ”, where X is the test case number starting at 1 and Y is the highest frequency. Next line contains all elements which have that frequency sorted in ascending order.
Constraints:
1 ≤ T ≤ 20 | 2 ≤ N ≤ 20.000 | 1 ≤ Xi ≤ 2 × 10^5
Sample Input:
3
8
1 1 2 2 3 4 5 5
8
5 5 4 3 2 2 1 1
4
1 1 1 3
Sample Output:
Case #1: 2
1 2 5
Case #2: 2
1 2 5
Case #3: 3
1
Here is my code:
#include <stdio.h>
int main() {
int T, N[20];
scanf("%d", &T); getchar();
int A[T][20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N[i]); getchar();
for (int j = 0; j<N[i]; j++) {
scanf("%d", &A[i][j]); getchar();
}
int X = 0;
for (int j = 0; j<N[i]; j++) {
for (int k = j + 1; k<N[i]; k++) {
if (A[i][k]<A[i][j]) {
X = A[i][j];
A[i][j] = A[i][k];
A[i][k] = X;
}
}
}
}
int f[20000];
for (int i = 0; i<T; i++) {
int c = 0, mc = 0;
for (int j = 0; j<N[i]; j++) {
c = 1;
if(A[i][j] != -1) {
for (int k = j+1; k<N[i]; k++) {
if (A[i][j] == A[i][k]) {
c++;
A[i][k] = -1;
}
}
f[j]=c;
}
if (c>mc) {
mc = c;
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N[i]; j++) {
if (A[i][j] != -1) {
if (f[j] == mc) {
printf ("%d", A[i][j]);
if (j<N[i]-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
EDIT
So I made another code where instead of inputting all arrays at once and outputting everything at once, this code outputs the frequency and elements after i input the first arrays of numbers. But it seems like the code still have problems and i can't find where... P.s I'm pretty new to this, so i apologise for the lack of efficiency of my codes.
NEW CODE
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T); getchar();
int A[20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
for (int j = 0; j<N; j++) {
scanf("%d", &A[j]); getchar();
}
int X;
for (int j = 0; j<N; j++) {
for (int k = j + 1; k<N; k++) {
if (A[k]<A[j]) {
X = A[j];
A[j] = A[k];
A[k] = X;
}
}
}
int f[N], c = 0, mc = 0;
for (int j = 0; j<N; j++) {
c = 1;
if(A[j] != -1) {
for (int k = j+1; k<N; k++) {
if (A[j] == A[k]) {
c++;
A[k] = -1;
}
}
f[j]=c;
if (c>mc) {
mc = c;
}
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N; j++) {
if (A[j] != -1) {
if (f[j] == mc) {
printf ("%d", A[j]);
if (j<N-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
It took me a couple of days but i finally got how to do this. Apparently, it was not as complicated as i thought... here is the working code. Thanks to everyone who helped :)
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T);
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
//INPUT elements and counting frequncy for each element
int f[200001] = {0}, E = 0;
for (int j = 0; j<N; j++) {
scanf("%d", &E); getchar();
f[E]++;
}
//find max frequency and how many elements with max frequency
int max = 0, c = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
c ++;
}
if (f[j]>max) {
max = f[j];
c = 1;
}
}
//OUTPUT result
printf("Case #%d: %d\n", i+1, max);
int counter = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
counter ++;
if (counter<c){
printf("%d ", j);
} else {
printf("%d\n", j);
}
}
}
}
return 0;
}

Printing same asterisk pattern in new line n times in C

I have this code:
#include <stdio.h>
int main()
{
int n, i, c;
printf("Size");
scanf("%d", &n);
for (i = 1; i <= (n + 1); i++)
{
printf("*");
printf(" ");
}
printf("\n");
for (int a = 1; a <= (((n + 1) * 2)-1); a++)
{
printf("*");
}
printf("\n");
}
What I am trying to do is, after the first line with the spaces. I want to print the same line which is (((n + 1) * 2) - 1) n times in new lines which n is given from the if. For example to give a better understanding
For n = 4:
* * * * *
********* 1st
********* 2nd
********* 3rd
********* 4th
As you can see, it will do the calculation and print the line with spaces and in the next lines it will do the calculation again but it will print the line n times without spaces. I can't find out how to print them in a new line every time.
A loop is missing:
#include <stdio.h>
int main()
{
int n, i, c;
printf("Size");
scanf("%d", &n);
for (i = 1; i <= (n + 1); i++)
{
printf("*");
printf(" ");
}
printf("\n");
size_t width = (((n + 1) * 2)-1);
for (int j = 0; j < n; j++) { /* loop for lines */
for (int a = 1; a <= width; a++) /* loop * in lines */
{
printf("*");
}
printf("\n");
}
}
add another for loop
for(j= 0; j< n;++j)/*do this inner for loop and scope code n times*/
{
for ( a = 1; a <= (((n + 1) * 2)-1); a++)
{
printf("*");
}
printf("\n");
}

Wrong output when printing a tent shape with stars

I'm trying to print out a hollow, open tent shape using asterisk stars "*". The code uses two for loops, the first for the rows, and the other for the columns.
following is my code:
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
} else {
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf(" ");
}
if(j == n) {
printf("*");
for(j = 1; j <= n; j++) {
printf(" ");
}
}
}
}
}
int main()
{
printTent(4);
}
Output obtained:
* * * *
Desired output:
*
* *
* *
* *
I don't think you will need that
if (n == 1) {
printf("*");
}
We can take care of that in what you've written in the else part.
For n=4, the number of spaces to be printed at the start of each line is 3, 2, 1 & 0.
You seem to be trying to accomplish that with your first inner loop. But
for(j = 0; j < n; j++) {
printf(" ");
}
will always print n spaces. We need to reduce the number of spaces printed by 1 on each iteration of the outer loop.
Coming to your second loop,
for(j = 1; j <= n; j++) {
printf(" ");
}
This has a similar problem only difference being the incrementation of the number of spaces printed.
Try something like this
void printTentNMMod(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
for(j = i; j < n; j++) {
printf(" ");
}
printf("*");
if(i!=0)
{
for(j=0; j<2*(i-1)+1; ++j)
{
printf(" ");
}
printf("*");
}
printf("\n");
}
}
Also, you could shorten this to
void printTent(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
printf("%*c", n-i, '*');
if(i!=0)
{
printf("%*c", 2*i, '*');
}
printf("\n");
}
}
The * in %*c will set the number of places occupied by the character printed by the %c.
I've finished it and I have written annotation.
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
}
else {
for (i = 0; i < n; i++) {
for (j = 0; j < n -i; j++) {// you should use n-i instead of n because the number of spaces is decreasing
printf(" ");
}
if (j == n-i) { //
printf("*");
for (j = 1; j <= i * 2 - 1; j++)//this loop outputs spaces between two "*"
{
printf(" ");
}
if (i != 0)//the first line only needs one "*"
printf("*");
printf("\n"); //Line breaks
}
}
}
}
Another way.
#include <stdio.h>
int main() {
int i, j;
int height = 5;
for(i = height; i > 0; i--) {
for(j = 1; j < height * 2; j++) {
if(j == i || j == height * 2 - i)
printf("*");
else
printf(" ");
}
puts("");
}
return 0;
}
Output
*
* *
* *
* *
* *

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