Square with a diagonal, asterix pattern in C - c

I have to write a program that will print a square made of asterisks with one diagonal, with the side of n asterisks. If n is 5, the pattern would be
*****
* **
* * *
** *
*****
My erroneous code is:
#include<stdio.h>
main(){
int n,row,i,star;
scanf("%d",&n);
for(row=1;row<=n;row++){
if(row==1||row==n){
for(i=1;i<=n;i++){
printf("*");
}
}
else { for(star=1;star<=n;star++){
if((star==1)||(star=n-row+1)||(star==n))
printf("*");
else printf(" ");
}
}
printf("\n");
}
}
Instead of doing its job, it prints infinite asterisks.

You are assigning instead of comparing.
if((star==1)||(star=n-row+1)||(star==n))
^

Try this code.
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter the number of lines to be printed:");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("*");
printf("\n");
for(i=0;i<n-2;i++)
{
printf("*");
for(j=0;j<n-2;j++)
{
if(j==(n-3-i))
{
printf("*");
}
else{
printf(" ");
}
}
printf("*\n");
}
for(i=0;i<n;i++)
printf("*");
printf("\n");
}

Related

I wrote a program to add two matrices but it's not working. Any suggestions for what's wrong?

This program takes two arrays from the user and adds them and prints the sum. The program uses nested for loops to take the values and print the values. The compiler is returning the error array1 not declared in this scope. The program also stops working if I remove the sum printing part. Any suggestions to shorten the program appreciated.
#include<stdio.h>
int a,b;
int i,j;
main()
{
printf("Enter the size of the array \n Rows : ");
scanf("%d",&a);
printf("Columns : ");
scanf("%d",&b);
int array[a][b];
printf("Enter the values of the %dx%d array : \n",a,b);
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
scanf("%d",&array[i][j]);
}
}
printf("The values of the First Matrix are :\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("%d\t",array[i][j]);
}
printf("\n");
}
int input;
printf("If you want to do further operations on Matrices press 1\n");
scanf("%d",&input);
if(input==1)
{
printf("Enter the size of the array \n Rows : ");
scanf("%d",&a);
printf("Columns : ");
scanf("%d",&b);
int array1[a][b];
printf("Enter the values of the %dx%d array : \n",a,b);
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
scanf("%d",&array1[i][j]);
}
}
printf("The values of the Second Matrix are :\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("%d\t",array1[i][j]);
}
printf("\n");
}
}
input = 0;
printf("If you want to add the two matrices press 1 \n");
scanf("%d",input);
int array2[a][b];
if(input==1)
{
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
array2[i][j] = array[i][j]+array1[i][j];
}
}
}
printf("The Sum of the first and Second array is : \n ");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("%d\t",array2[i][j]);
}
printf("\n");
}
}
You have lot of problem in the code, advise you to use curly braces {..} properly. Also use int main(void) { } instead of just main() { }.
The compiler is returning the error array1 not declared in this scope ? Because array1 is declared inside if(input==1) block and you are accessing outside that scope.
Also statement scanf("%d",input); is wrong, it gives the warning, compile your program with -Wall flag.
And finally avoid using global variable for this small tasks or use #define to define row and column value.
Here is modified code
int main() {
printf("Enter the size of the array \n Rows : ");
int a = 0,b = 0;
scanf("%d",&a);
printf("Columns : ");
scanf("%d",&b);
int array[a][b];
printf("Enter the values of the %dx%d array : \n",a,b);
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) {
scanf("%d",&array[i][j]);
}
}
printf("The values of the First Matrix are :\n");
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) {
printf("%d\t",array[i][j]);
}
printf("\n");
}
int input;
printf("If you want to do further operations on Matrices press 1\n");
scanf("%d",&input);
if(input==1) {
printf("Enter the size of the array \n Rows : ");
scanf("%d",&a);
printf("Columns : ");
scanf("%d",&b);
int array1[a][b];
printf("Enter the values of the %dx%d array : \n",a,b);
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) {
scanf("%d",&array1[i][j]);
}
}
printf("The values of the Second Matrix are :\n");
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) {
printf("%d\t",array1[i][j]);
}
printf("\n");
}
input = 0;
printf("If you want to add the two matrices press 1 \n");
scanf("%d",&input);/* use &input */
int array2[a][b];
if(input==1) {
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) {
array2[i][j] = array[i][j]+array1[i][j];
}
}
}
printf("The Sum of the first and Second array is : \n ");
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) {
printf("%d\t",array2[i][j]);
}
printf("\n");
}
}
return 0;
}
You declare array1 in block beginning with:
if(input==1)
Then you try to use it outside that block, which gives you the compiler error.
array2[i][j] = array[i][j]+array1[i][j];
The fastest solution would be to nest the code following the blok inside it, so array1 stays in scope.

My code is not running properly not printing all the rows it should

#include <stdio.h>
int main(void) {
int i,j,n,m;
printf("enter the numbr of rows : ");
scanf("%d",&n);
m=(2*n)+1;
printf("%d, %d",n,m);
for(i=0;i<=m;i++)
{
for(j=0;j<m;j++)
{
if(i==n)
{
printf("*");
}
else
{
printf(" ");
}
}
for(j=0;j<i;j++)
{
if((i+j)>=m)
break;
printf("*");
}
printf("\n");
}
return 0;
}
My code is not running properly not printing all the rows it should. It is the code to print a full arrow like
*
**
********
**
*
its just an example.
Your problem is:
m=(2*n)+1;
It needs to be
m=(2*n);
see: Online gcc compiler example draw arrow
https://onlinegdb.com/rk9U4nUFf
It got fixed!!! i was running the loop one time less so it was running but not printing anything as the last loop was skipping a loop which it should not. Above is the correct code.
#include <stdio.h>
int main(void) {
int i,j,n,m;
printf("enter the numbr of rows : ");
scanf("%d",&n);
m=(2*n);
printf("%d, %d",n,m);
for(i=0;i<=m;i++)
{
for(j=0;j<m;j++)
{
if(i==n)
{
printf("*");
}
else
{
printf(" ");
}
}
for(j=0;j<i;j++)
{
if((i+j)>=m)
break;
printf("*");
}
printf("\n");
}
return 0;
}
Source Link: https://www.onlinegdb.com/H1gogfIHYz

How to print the given ZIG ZAG pattern using C?

I'm scratching my head from the morning and I didn't even reach something which looks like given pattern. Any amount of help is appreciated.
Thanks in advance!
This is not my home work
EDIT 1: Thank You all! I finally arrived at the solution.
`#include<stdio.h>
void main(){
int n,i,count;
scanf("%d",&n);
int prev=n,next=(n*2)+(n-2),tc=1;
for(int i=1;i<=n;i++){
if(i==1) {
for(count=1;count<=(n*2)+(n-2);count++) {
if(count==prev || count==next) printf("*");
else printf(" ");
}
}
else {
for(count=1;count<=2*n*n;count++){
if(count==prev-tc || count==prev+tc || count==next-tc|| count==next+tc)
printf("*");
else printf(" ");
}
tc++;
}
printf("\n");
}
}`
to solve questions of this type you must find a formula for spaces and a formula for stars. these formulas tell the computer how many spaces and stars must be printed in each line.
that's all these types of questions need.
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int x=n;x>=i;x--){
printf(" ");
}
int y=2*i; int f=1;
while(y!=1){
if(f!=1&&f!=2*i-1){
printf(" ");
}
else if(i==n&&f==2*i-1){
printf("");
}
else{
printf("*");
}
f++;
y--;
}
if(i==n){
printf("*");
}
for(int xk=n;xk>i;xk--){
printf(" ");
}
int y1=2*i; int f2=1;
while(y1!=1){
if(f2==2*i-1){
printf("");
}
if(f2!=1&&f2!=2*i-1){
printf(" ");
}
else if(i==n&&f2==1){
printf(" ");
}
else{
printf("*");
}
f2++;
y1--;
}
printf("\n");
}
}

How to stop my process if I find the list in sorted in any intermediate point-IN BUBBLE SORT?

#inlcude<stdio.h>
int main()
{
int arr[30],num,i,j,k,temp,l=0;
printf("Enter the number of elements :\n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("Enter element %d\n",i);
scanf("%d",&arr[i]);
}
for(k=1;k<=num;k++)
{if(arr[k]>arr[k+1])
l++;}
if(l!=0)
{
for(i=2;i<=num;i++)
{l=0;
for(k=1;k<=num;k++)
{
if(arr[k]>arrk+1])
l++;}
if(l!=0)
{for(j=1;j<num;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
printf("\nAfter pass %d elements are:",i-1);
for(k=1;k<=num;k++)
printf("%d",arr[k]);}
else
break;
}
}
printf("\nsorted list is:\n");
for(k=1;k<=num;k++)
printf("%d ",arr[k]);
retrun 0;
}
This is the Bubble sort program. My query is I need to stop my process if I find my list is sorted in any intermediate point. I even did that... But still there is some problem with this program. It's not getting accepted in portal, It triggers as the program is "Wrong Answer". I guess there may be mistakes in finding out the intermediate point. Help me out to figure out this..
You can set a flag and check if the list is sorted like this:
for(i=0; i ......
{
flag=1; // We are setting a "break point" here by setting flag=1; If any exchange of elements take place inside if loop, flag will be
//set back to zero inside the if block statements.
for(j=0; j ......
{
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=0; // flag is set back to zero because there occured an "exchange of element"
}
}
}
#include<stdio.h>
int main()
{
int array[100],i,n,c,swap,flag=0;
printf("Enter the number of elements :\n");
scanf("%d",&n);
for(c=0;c<n;c++)
{
printf("Enter element %d\n",c+1);
scanf("%d",&array[c]);
}
printf("Unsorted list is :\n");
for(i=0;i<n;i++)
printf("%d ",array[i]);
printf("\n");
for(c=0;c<n-1;c++)
{
if(flag==0)
{
flag=1;
for(i=0;i<n-1;i++)
{
if(array[i]>array[i+1])
{
swap=array[i];
array[i]=array[i+1];
array[i+1]=swap;
flag=0;
}
}
printf("After Pass %d elements are :",c+1);
for(i=0;i<n;i++)
{
printf("%d ",array[i]);
}
printf("\n");
}
}
printf("Sorted list is :\n");
for(c=0;c<n;c++)
printf("%d ",array[c]);
return 0;
}

Recursive C program to differentiation a polynomial until it stops

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10],i,n,d[10],power;
float in[10];
clrscr();
printf("Enter the order ofthe polynomial\n");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
printf("Enter the co-efficient of x^%d:",i);
scanf("%d",&a[i]);
}
printf("Given polynomial is\n");
for(i=n;i>=0;i--)
{
if(power<0)
{
break;
}
if(a[i]>0)
{
if(i!=n)
printf(" + ");
}
else if(a[i]<0)
printf(" - ");
else
printf(" ");
printf("%dx^%d",abs(a[i]),i);
}
//To find derivative
for(i=n;i>=0;i--)
d[i]=a[i]*i;
printf("\n Derivative of the given polynomial is\n");
for(i=n;i>=1;i--)
{
if(power<0)
{
break;
}
if(d[i]>0)
printf(" + ");
else if(d[i]<0)
printf(" - ");
else
printf(" ");
printf("%dx^%d",d[i],i-1);
}
getch();
}
the above program only calculate the first derivative, but i need a program which calculates all the derivatives
ex 2x^3+2x^2+3x+1
f1= 6x^2+4x+3
f2=12x+4
f3=12
like this i need to modify the program,, but am not getting how to do that,,please help me//
try this
#include <stdio.h>
#include <stdlib.h>
typedef struct polynomial {
int order;
int *coefficient;
} Polynomial;
void init_poly(Polynomial *p){
int i;
printf("Enter the order of the polynomial\n");
scanf("%d", &p->order);
p->coefficient = malloc((p->order + 1)*sizeof(int));
for(i = p->order; i >= 0; --i){
printf("Enter the co-efficient of x^%d:", i);
scanf("%d", &p->coefficient[i]);
}
}
void drop_poly(Polynomial *p){
free(p->coefficient);
}
void print_poly(Polynomial *p){
int i;
for(i = p->order; i >= 0; --i){
if(p->coefficient[i]){
if(p->order != i){
printf(" %c ", p->coefficient[i] < 0 ? '-' : '+');
printf("%d", abs(p->coefficient[i]));
} else
printf("%d", p->coefficient[i]);
if(i)
printf("x^%d", i);
}
}
printf("\n");
}
void differential_poly(Polynomial *p){
int i;
for(i = 0; i < p->order; ++i){
p->coefficient[i] = p->coefficient[i+1] * (i+1);
}
p->coefficient[p->order--] = 0;
}
void r_print_differential_poly(Polynomial *p){
int i=1;
while(p->order){
differential_poly(p);
printf("f%d = ", i++);
print_poly(p);
}
}
int main(void){
Polynomial poly;
init_poly(&poly);
printf("Given polynomial is\n");
print_poly(&poly);
r_print_differential_poly(&poly);
drop_poly(&poly);
return 0;
}

Resources