I am trying to code a project in c, that displays a fractal called Sierpinski fractal, (where the nodes are represented by '#'). So a 1-sierpinski triangle looks like :
##
#
a 2-sierpinski triangle
####
# #
##
#
and so on... Here's a link to find what it looks like : http://fr.wikipedia.org/wiki/Triangle_de_SierpiĆski
I was told it could be done without any loop, just by recursive method. So I tried something like :
//extracting the power of two's index
int puiss_2(int N){
int i=0,j=1;
for(i=0;i<N;i++){
j=j*2;
i++;
}
return j;
}
//the recursive method
void fractal(int N)
{
int M;
M= puiss_2(N);
if(M==0){
printf("##\n");
printf("# ");
}
else{
fractal(N-1);
fractal(N-1);
printf("\n");
fractal(N-1);
printf(" ");
}
}
int main()
{
int N;
scanf("%d",&N);
fractal(N);
}
Of course it didn't work because, when I jump to a line, I can't reverse it. So when I call it two times :
fractal(N-1);
fractal(N-1);
two contiguous motives are not gathered one aside the other... Does anyone has an idea on how to
make that ? Or perhaps I went completely wrong in my algo's design?
Here's some code that is perhaps complicated but recursive !
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sierpinsky(int N, char c[1000]){
int i=0,j,k,l,born;
for(i=0;i<N;i++){printf("%c",c[i]);}
printf("\n");
if(N==1){}
else{
if((c[0]=='#')&&(c[1]=='#')&&(c[2]=='#')){
for (j=0;2*j<N;j++){
if(c[2*j]=='#'){
c[2*j]='#';c[2*j+1]=' ';
}
else{
c[2*j]=' ';c[2*j+1]=' ';
}
}
}
else if ((c[0]=='#')&&(c[1]!='#')&&(c[2]=='#')){
for (j=0;4*j<N;j++){
if(c[4*j]=='#'){
c[4*j]='#';c[4*j+1]='#';c[4*j+2]=' ';c[4*j+3]=' ';
}
else{
c[4*j]=' ';c[4*j+1]=' ';c[4*j+2]=' ';c[4*j+3]=' ';
}
}
}
else if ((c[0]=='#')&&(c[1]!='#')&&(c[2] !='#')){
k=0;
while(c[k+1] !='#'){k++;}
born = k+1;
j=0;
while(j<N){
if((c[j]=='#')&&(c[j+born]=='#')){
for(l=0;l<born;l++){
c[j+l]='#';
}
j=j+born+1;
}
else if ((c[j]!='#')&&(c[j-1+born]=='#')&&(c[j-1+2*born] !='#'))
{
c[j-1]='#';
for(l=0;l<born;l++){
c[j+l]='#';
}
j=j+born+1;
}
else{
c[j-1]= ' ';
c[j]=' ';
j++;
}
}
}
else if ((c[0] =='#')&&(c[1] =='#')&&(c[2] !='#')){
for (j=0;4*j<N;j++){
if(c[4*j]=='#'){
c[4*j]='#';c[4*j+1]=' ';c[4*j+2]=' ';c[4*j+3]=' ';
}
else{
c[4*j]=' ';c[4*j+1]=' ';c[4*j+2]=' ';c[4*j+3]=' ';
}
}
}
else{}
sierpinsky(N-1, c);
}
}
int main()
{ int i,size;
scanf("%d",&size);
char c[1000];
for(i=0;i<size;i++){c[i]='#';}
for(i=size;i<1000;i++){c[i]='a';}
sierpinsky(size, c);
}
I think you dont need recursion for this. Let a triplet of # be 1 set. So The value of n = No. of levels of the set you're supposed to print one below the other. In the first line, print the set n times. In the next line, n-1 times, and so on. Try it iteratively.
Edit : If you were looking for a recursive solution, kindly ignore my answer.
you can probably code this using the pascal triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
If you can print out this triangle as a whole with a loop, then , perhaps you can skip over even numbers.
to print the plain triangle just count the number of spaces in relation to the number of lines you want and code using a for loop(or a couple of them).Check which (pascal) number corresponds to which printing and skip over the even ones.
Related
I've tried to write a program that removes the duplicate values from an array. I've partly managed to do so since my program is able to remove any ONE of the numbers which are repeated TWICE in the array. So the problem is that if a number is repeated thrice only one of the number is removed, i.e. the other two is still left in the array, also if more than one number is repeated even then only the number which comes first in the array is removed. I really cannot understand what's wrong with my code and why is it unable to remove numbers that are repeated more than two times. I've already surfed through the internet regarding this issue and though I got different ways to remove the duplicate elements, I still don't know what's wrong with my code.
#include <stdio.h>
#include <stdlib.h>
int dup(int [],int);
int main()
{
int i,n,index,a[20];
printf("Enter n value \n");
scanf("%d",&n);
printf("Enter array values \n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(i=index;i<n;i++)
a[i]=a[i+1];
n-=1;
}
}
printf("Output: \n");
for(i=0;i<n;++i)
printf("%d\n",a[i]);
return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
int i,j,pos=-1;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
pos=j;
return pos;
}
}
}
if(pos==-1)
return pos;
}
OUTPUT
Enter n value
5
Enter array values
12
24
3
12
24
Output:
12
24
3
24
It clearly fails to remove the other repeated element "24". Also if a number was repeated thrice only one of the number would be removed.
for(i=0;i<n;++i) // <-------------------------------------- for i
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(i=index;i<n;i++) // <--------------------------- for i
a[i]=a[i+1];
n-=1;
}
}
You are using the same loop variable for two loops, one nested inside the other. This cannot work. Use different variables. Live demo.
The Problem seem to lie in the if condition in second loop.
for (k = j; k < size; k++) {
arr[k] = arr[k + 1];
}
Simply put this piece of code after your if condition
if(a[i]==a[j])
and it will work.
My mistake, at first glence I thought you had problem with n after running this it worked.
#include <stdio.h>
#include <stdlib.h>
int dup(int [],int);
int main()
{
int i,n,index,a[20], count;
printf("Enter n value \n");
scanf("%d",&n);
count = n;
int j;
printf("Enter array values \n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(j=index;j<n;j++)
a[j]=a[j+1];
n-=1;
}
}
printf("Output: \n");
for(i=0;i<n;++i)
printf("%d\n",a[i]);
return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
int i,j,pos=-1;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
pos=j;
return pos;
}
}
}
if(pos==-1)
return pos;
}
OUTPUT
Enter n value
5
Enter array values
12
24
3
12
24
Output:
12
24
3
You should name your iterator variables better so you might not confuse them in nested loops, or as you do, use the same twice in a nested loop.
This skips all variables after your first removal.
and you don't have to do this
if(pos==-1)
return pos;
skip the if as it is not necessary and if at this position posis not -1then you would have no return which would be UB I think.
well I've got it how to construct the pascal triangle and the code
below is flawless but...in this code i am making the 1 to appear in
first row by creating a new for loop especially for it... is there a
way to generate pascal triangle without using an exclusive for loop
for the 1 to appear in first... any help is much appreciated :)
//pascal triangle with ncr function
#include <stdio.h>
#include <conio.h>
int ncr(int i,int j);
int main()
{
int i,j,v,n,f,s;
printf("Enter the number of rows required\n");
scanf("%d",&n);
f=n;
//this is what i am exclusively using for printing 1 in 1st row
for(;f>0;f--)
{
printf(" ");
}
printf("1\n");
//is there a way to generate the above 1 using only the below for loop
for(i=1;i<=n;i++)
{
for(s=n-i;s>0;s--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
v=ncr(i,j);
printf("%d ",v);
}
printf("\n");
}
}
int ncr(int i,int j)
{
int k;
float ans=1;
for(;j>=1;j--)
{
ans=((ans*i)/j);
i--;
}
k=ans;
return(k);
}
If you look carefully, you'll notice that the ncr function is defined inside the main method. Move the implementation of ncr outside of main.
Also, noticed by #BLUEPIXY, your implementation of ncr has an excess ;:
int ncr(int i,int j); //<<right here
{
//...
EDIT Solution to second problem (see Pascal's Triangle on Wikipedia)
The "first" row of the triangle is actually the zeroth row. Your outer loop starts with i = 1 and therefore the "first" row contains 1C0 and 1C1. The "first" or zeroth row should actually contain only 0C0. New loop:
//notice how the i here has changed to 0
for(i=0;i<=n;i++)
{
for(s=n-i;s>0;s--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
v=ncr(i,j);
printf("%d ",v);
}
printf("\n");
}
Please check the problem with this code.
It is showing compilation error when I'm running it on mycodeschool IDE.
problem statement is
Problem statement
Given a two dimensional array, print its mirror image if mirror is placed along one of the sides of the array.
Input
First line of input will contain a number T = number of test cases. Each test case will contain two positive integers n and m (1<=n, m<=50) on a single line separated by space. Next n lines will each contain a string of exactly m characters. Next line will contain a character 'V' or 'H'. If character is V, mirror is placed vertically along the right-most column. If the character is H, the mirror is placed horizontally along the bottom-most row.
Output
For each test case, print the n*m mirror image - n lines with strings of m character each. Print an extra empty line after output for each test case.
Sample Input
2
3 3
abc
def
ghi
V
3 4
1234
5678
9876
H
Sample Output
cba
fed
ihg
9876
5678
1234
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char a[10][10];
int i,j,t,m,n;
char s;
scanf("%d\n",&t);
for(j=0;j<t;j++)
{
scanf("%d%d\n",&m,&n);
for(i=0;i<m;i++)
{
scanf("%s",&a[i]);
}
scanf("\n%c",&s);
if(s=='V')//for image along rightmost vertical line
{
for(i=0;i<m;i++)
{
strrev(a[i]);
}
}
else if(s=='H')//for image along lowermost horizontal line
{
int t=0;
int b=m-1;
while(t<b)
{
char *temp = (char *)malloc((strlen(a[t]) + 1) * sizeof(char));//temporary variable to swap
strcpy(temp, a[t]);
strcpy(a[t], a[b]);
strcpy(a[b], temp);
free(temp);
t++;
b--;
}
}
for(i=0;i<m;i++)
{
printf("%s\n",a[i]);
}
printf("\n");}
return 0;
}
I think there is problem with taking input character array n line with exactly m characters...here is my code ........
int main()
{
int tt,i,j,r,c,t,ch;
char a[20][20];
scanf("%d",&t);
for(tt=1;tt<=t;t++)
{
scanf("%d\t%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%c",&a[i][j]);
}
printf("\n");
}
scanf("%c",&ch);
if(ch=='V')
{
for(i=0;i<r;i++)
{
for(j=(c-1);j>=0;j--)
{
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
}
}
else if(ch=='H')
{
for(i=(r-1);i>=0;i--)
{
for(j=0;j<c;j++)
{
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
}
}
else
printf("error");
}
printf("");
getchar();
return 0;
}
i am working in a university project that i should write a minesweaper game with some array ...
i have written my program
the program will print the minesweaper table some cell in the table has bomb the program should print * in that cell.
and other cell should print number of bomb in eight surrounding cell
I don't know how to print this double sub-scripted array that can print character and number both.
#include <stdio.h>
#define max 100
int main()
{
int a,row,column,n,x,y,counter,i,j;
char table[max][max]={0};
scanf("%d%d",&row,&column);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
table[x-1][y-1]='*';
}
for(counter=0,i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if(table[i-1][j-1]=='*')
counter++;
if(table[i-1][j]=='*')
counter++;
if(table[i-1][j+1]=='*')
counter++;
if(table[i][j-1]=='*')
counter++;
if(table[i][j+1]=='*')
counter++;
if(table[i+1][j-1]=='*')
counter++;
if(table[i+1][j]=='*')
counter++;
if(table[i+1][j+1]=='*')
counter++;
if(table[i][j]!='*')
table[i][j]=counter;
counter=0;
}
}
for(counter=0,i=0;i<row;i++)
{
for(j=0;j<column;j++)
printf("%d ",(char)table[i][j]);
printf("\n");
}
}
Not an answer, but your code will not work because
if(table[i][j]!='*')
table[i][j]=counter;
^^^^^^^^^^^^^^^
Will overwrite what is in the location at i,j and the later passes will not see a '*' there. So basically your counts are going to be all messed up. You need one array for the '*' and another for the neighbor counts.
I can't figure out what is going wrong with it.
If input: 4,56,5,2 then output shown is: 2,4,0,1304.
If input: 27,54,43,26,2 then output shown is: 2,26,0,1304,0
If input: 34,87,54,4,34 then output shown is: 4,34,0,1304,0
Basically, only first two sorted nos are being shown in output and on other places either 1304 or 0 is showing for any set of input.
#include <conio.h>
#include <stdio.h>
void main()
{
int a[10],b[10];
int i,size,j,k;
clrscr();
printf("please tell how many nos you want to enter");
scanf("%d",&size);
printf("Enter the nos");
for (i=0;i<size;i++) scanf("%d",&a[i]);
b[0]=a[0];
//insertionSort algo ---->
for (j=1;j<size;j++)
{
for (k=j-1;k>=0;k--)
//handling comparision with b[0]
if (k==0&&(a[j]<b[0])) {
b[1]=b[0];
b[0]=a[j];
}
//handling comparison with b[1:size-1]
if (k>0&&(a[j]<b[k])) { b[k+1]=b[k]; }
if (k>=0&&(a[j]>=b[k])) { b[k+1]=b[k]; break; }
}
for (i=0;i<size;i++) printf("%d\n",b[i]);
getch();
}
Use a algorithm that's more simple:
After reading the numbers, copy array A to B to keep the original input.
For ascending sort, set i = 0, j = i + 1
loop j until end of array, if B[j] < B[i] then exchange the two numbers.
Increase i, set j = i + 1, go to step 3. unless i >= size.
print arrays A and B
The algorithm can be optimized later.
Here are the minimal changes with /* comments */ to make your program work:
#include <conio.h>
#include <stdio.h>
void main()
{
int a[10],b[10];
int i,size,j,k;
clrscr();
printf("please tell how many nos you want to enter");
scanf("%d",&size);
printf("Enter the nos");
for (i=0;i<size;i++) scanf("%d",&a[i]);
b[0]=a[0];
//insertionSort algo ---->
for (j=1;j<size;j++)
for (k=j-1;k>=0;k--)
{ /* the inner loop must contain all the if statements */
//handling comparision with b[0]
if (k==0&&(a[j]<b[0])) {
b[1]=b[0];
b[0]=a[j];
break; /* done; don't mess with b[0+1] below */
}
//handling comparison with b[1:size-1]
if (k>0&&(a[j]<b[k])) { b[k+1]=b[k]; }
if (k>=0&&(a[j]>=b[k])) { b[k+1]=a[j]; break; } /* =a[j] */
}
for (i=0;i<size;i++) printf("%d\n",b[i]);
getch();
}