HourGlass Star Pattern program in C - c

I had an interview in which I was asked to write an hourglass star pattern program. For example, if the user specifies 5 then the pattern must be like:
*****
***
*
***
*****
If 7 then the pattern must be like:
*******
*****
***
*
***
*****
*******
My coding for the pattern is:
for(i=n; i>=1; i--)
{
for(j=i; j<n; j++)
{
printf(" ");
}
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
for(i=1; i<=n; i++)
{
for(j=i; j<n; j++)
{
printf(" ");
}
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
Please help me to solve this problem.

You can use only 1 loop for printing:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
int a=0;
scanf("%d",&a);
int i=0;
char* asterisk=malloc(sizeof(char)*a);
memset(asterisk,'*',a*sizeof(char));
while(i<a)
{
if(i<=a/2)
printf("%*.*s\n",a-i,a-2*i,asterisk);
else
printf("%*.*s\n",i+1,2*(i+1)-a,asterisk);
i++;
}
free(asterisk);
return 0;
}

This could help. It's not the best that there is and it could be made more efficient, but it should give you an idea.
#include<stdio.h>
void print(int space, int star)
{
while (space--) {
printf(" ");
}
while (star--) {
printf("*");
}
printf("\n");
}
void main()
{
int star,space,i,j;
printf("Enter number:");
scanf("%d",&star);
j=star/2;
i=star;space=0;
while (space<j || star>0) {
print(space,star);
space=space+1;star=star-2;
}
space=j-1;star=3;
while(space>0 || star<=i) {
print(space,star);
space=space-1;star=star+2;
}
}
Please comment if you have any doubts.
Note:The above code is just a sample. There are many (and far more efficient) ways to implement the pattern and it is encouraged to find a way to do so.
Edit:The code now prints only 1 star in the middle. Did not notice that. Apologies.

Guys I found the mistake, What i did in that code.
#include <stdio.h>
void main()
{
int i, j, n;
printf("Enter Odd value for n : ");
scanf("%d", &n);
for(i=n/2+1; i>1; i--)
{
for(j=i; j<n; j++)
{
printf(" ");
}
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
for(i=1; i<=n/2+1; i++)
{
for(j=i; j<n; j++)
{
printf(" ");
}
for(j=1;j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
}
Thank You!!!!!!! Guys for your help.

Related

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

I'm trying to add numbers to each new line

I am trying to print out a sequence of numbers and * that form a rectangle depending on the number provided by the user. I managed to print a rectangle with * but I do not know how to incorporate numbers to print out something like:
1*****
12****
123***
1234**
12345*
123456
This is what I have:
#include <stdio.h>
int main{
int i, j;
int num;
printf("Enter a number from 1-9: ");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
for(j=1; j<=num; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Hope this helps!
#include <stdio.h>
int main(){
int i, j;
int num;
printf("Enter a number from 1-9: ");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
for(j=1; j<=i; j++)
{
printf("%d",j);
}
for(;j<=num;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Also,Instead for using 2 loops inside of Master loop,You can Use conditional operators to manipulate output!
Cheers! :)

Gaussian Elimination in C

This code works only if the system has unique solutions. When there is no solution or infinitely many solutions, it should print "Has no unique solution." but the code below prints "nan" or "inf". How can I do that?
#include<stdio.h>
int main()
{
int i,j,k,n;
double A[20][20],c,x[10];
printf("\nEnter the size of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf(" A[%d][%d]:", i,j);
scanf("%lf",&A[i][j]);
}
}
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i!=j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
printf("\nThe solution is:\n");
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
printf("\n x%d=%0.3f\n",i,x[i]);
}
return(0);
}
Perform a check for the whether the number is valid or not before printing.If not valid, print your desired message.You can modify the last for loop in your code as follows:
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
//Nan and inf check
if((A[i][i]!=A[i][i]) || (A[i][i] ==0))
break;
else
printf("\n x%d=%0.3f\n",i,x[i]);
}
printf("Has no unique solution");
return(0);
}
Handling Infinity and Nan

I want to print pattern of numbers using c program

If n=3,the output is
1*2*3
7*8*9
4*5*6
If n=5,the output is
1*2*3*4*5
11*12*13*14*15
21*22*23*24*25
16*17*18*19*20
6*7*8*9*10
CODE:
int i, j, a[50][50], k = 1, m = 0;
for (i = 0; i < n; i += 2) {
for (j = 0; j < n; j++) {
a[i][j] = k;
k++;
}
printf("\n");
}
m = k;
for (i = 1; i <= n; i += 2) {
for (j = 0; j < n; j++) {
a[i][j] = m;
m++;
}
printf("\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d", a[i][j]);
}
printf("\n");
}
I am not so good in c language, but i think it will help you.
please have a look, and you can do making some change if any syntax error occurs but logic is clear.
#include <stdio.h>
#include <math.h>
void printOutput(int n){
int k = ceil(n/2);
int m =1;
int j =1;
int l =k;
int i;
int b;
for(i=1;i<=n;i++){
for(b=m;b<=m+n;b++){
printf(b);
}
printf("\n");
if(i<k){
j= (2*j);
m =n*j+1;
} else{
int z = n-i-1;
m= n+1 +n*(2)*z;
l =z-2;
}
}
}
void main(){
int input;
printf("Enter a Value : ");
scanf(" %d",&input);
printOutput(input);
}
#include<stdio.h>
#include<conio.h>
int k=1;
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n/2+1;i++){
for(int j=1;j<=n;j++){
if(j!=1&&j!=n+1){
printf("*");
}
printf("%d",k);
k++;
}
printf("\n \n");
k=k+n;
}
k=k-3*n;
for(int i=1;i<=n/2;i++){
for(int j=1;j<=n;j++){
if(j!=1&&j!=n+1){
printf("*");
}
printf("%d",k);
k++;
}
printf("\n \n");
k=k-(n/2+1)*n;
}
}
This is a rough sketch of what you should do, there are some minor flaws with it... However, the functionality is there. Next time, if you can't understand what algorithm the code calls for, I suggest you write this array out on a sheet of paper and follow each row. Notice where each row gets placed, you should start to come up with a way to do this (there are more ways than this one...) It may seem hard at first, but if you want to be in this field, you have to have the mindset for it. I agree with the others, this is NOT a homework site, rather a site to help build off the knowledge you know, so begin to actually try to write the program, and then submit it here if you're having trouble with it.
#include <stdio.h>
void loadNprint(int size, int a[][size]){
int i,j,count=1,down=size-1, up =0;
for(i=0; i<size; i++){
for(j=0;j<size; j++){
if((i%2) == 0)a[up][j] = count;
if((i%2) == 1)a[down][j]= count;
count++;
}
if((i%2) == 0)up++;//keeping track of the rows in ascending order
if((i%2) == 1)down--;//keeping track of rows in descending order
}
for(i=0; i<size; i++){
for(j=0; j<size; j++){
printf("%4d",a[i][j]);
}
printf("\n");
}
}
void main(){
int input;
printf("Enter a Value : ");
scanf(" %d",&input);
int myarray[input][input];
loadNprint(input,myarray);
}
This program works perfect. But if you want make some changes..do it yourself.
Next time please try some coding yourself before asking. This will print a different pattern for even numbers.
#include<stdio.h>
#include<conio.h>
int n,beginnew,cpy;
int main()
{
printf("Please enter a value : ");
scanf("%d",&n);
//process
beginnew=n-n/2+1;//beginning of new pattern
cpy=n-1;
for(int i=1;i<n+1;i++)
{
if(i<beginnew)
{
for(int h=n-1;h>=0;h--)
printf("%d * ", (n*(2*i-1)-h) );
}
else
{
for(int h=n-1;h>=0;h--)
printf("%d * ",(n*(cpy)-h) );
cpy=cpy-2;
}
printf("\n");
}
getch();
return 0;
}
//this code print Diagonal Pattern if matrix is
1 2 3
4 5 6
7 8 9
output is :
1
4 2
7 5 3
8 6
9
import java.util.*;
class DiagonalPattern
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int x[][];
int i,j,row,col,p,temp=1,last=0;
System.out.println("how many array wants to create and size of array");
row=sc.nextInt();
col=sc.nextInt();
x=new int[row][col];
System.out.println("Enter " +row*col+ " elements of array of array");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
x[i][j]=sc.nextInt();
last=j;
}
}
for(i=0;i<row;i++)
{
System.out.println("");
int k=i;
for(j=0;j<=i;j++,k--)
{
if(j==col)
{
break;
}
else
{
System.out.print(x[k][j]);
System.out.print(" ");
}
}
}
for(p=x.length;p>0;p--,temp++)
{
System.out.println("");
i=x.length-1;
int k=i;
for(j=temp;j<=last;j++,k--)
{
System.out.print(x[k][j]);
System.out.print(" ");
}
}
}
}

How to compare a given string with a result

I'm very new to this and to C programming. It's my first year and things are not very clear for me. Hopefully I can get better very soon.
I have a code here where i input a number and I get results based on the table I have below.
I would like to know: If i have a given string (in the code it's: test[7]="2 B 1 C")
How can I compare the result i have to this string and see if it's the same to print=good?
it's very hard to explain so please let me know if i'm not clear with my question. You can test the code to see how it works.
#include <stdio.h>
#include <string.h>
void initialize(int poss[1296][4]);
int main()
{
int table[1296][4];
char str[5];
char tmp[5];
int i, j, k;
int bull = 0;
int cow = 0;
char test[7]={"2 B 0 C"};
initialize(table);
printf("Enter 4 digits: ");
scanf("%s", str);
for (i=0; i<1296; i++) // building this table
{
strcpy(tmp, str); // copying string
for (j=0; j<4; j++)
{
for (k=0; k<4; k++)
{
if (table[i][j]==tmp[k]-'0' && j==k) // gets the string as an integer
{
tmp[k] = -1;
bull++;
break;
}
else if (table[i][j]==tmp[k]-'0' && j!=k)
{
tmp[k] = -1;
cow++;
break;
}
}
}
printf ("%d B %d C\n\n", bull, cow);
bull = 0;
cow = 0;
}
}
//------------------------------------TABLE---------------------------------//
void initialize(int poss[1296][4])
{
int i=0;
int j, k=0;
int m;
while (i<=5)
{
for (j=0; j<216 ; j++)
{
poss[k][0]=i;
k++;
}
i++;
}
k=0;
i=0;
j=0;
while (k<1296)
{
for (m=0; m<6; m++)
{
for (j=0; j<6; j++)
{
for (i=0; i<36 ; i++)
{
poss[k][1]=j;
k++;
}
}
}
}
k=0;
i=0;
j=0;
m=0;
while (k<1296)
{
for (j=0; j<6; j++)
{
for (i=0; i<6; i++)
{
poss[k][2]=j;
k++;
}
}
}
k=0;
i=0;
j=0;
m=0;
while (k<1296)
{
for (i=0; i<6; i++)
{
poss[k][3]=i;
k++;
}
}
}
You can use sprintf to generate the result into a string instead of printing it, then use strcmp to compare that string to what you expect it to be. You can also then use printf to print the string you produced with sprintf. (When you sprintf, don't include the newlines since those aren't in your test string; only output those with printf).
What's not clear from your question, however, is that you only have one test string but print 1296 lines ... if those lines aren't all the same then you need an array of 1296 test results ... or a clearer question.
sample
#include <stdio.h>
#include <string.h>
int main(){
char test[8]={"2 B 0 C"};
char result[8];
int bull = 0;
int cow = 0;
bull = 2;//set by program
cow = 0;
sprintf(result, "%d B %d C", bull, cow);
if(strcmp(result, test)==0){
printf("match!\n");
} else {
printf("not match!\n");
}
return 0;
}

Resources