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();
}
Related
I am trying to get an output like this for the given n variables. If n=5 the output should be as follows:
*
***
*****
*******
*********
In my code, when I print I get the output reversed and without spaces. I have tried many ways to achieve this but couldn't find a solution. The output I receive for n=5 is:
*********
*******
*****
***
*
The following is the code:
#include <stdio.h>
int main() {
int n=0;
int b=0;
puts("Please Enter an Integer");
scanf("%d",&n);
for(n;n>0;n--){
b = 2*n - 1;
for(b;b>0;b--){
printf("*");
}
printf("\n");
}
return 0;
}
After some intense brainstorming I have managed to find the solution.
#include <stdio.h>
int main() {
int n=0;
int a=0;
int b=1;
int c=0;
puts("Please Enter an Integer");
scanf("%d",&n);
for(n;n>0;n--){ // Loop indicating that the pattern will run n times.
c=n-1; // Loop for the spaces to align the pattern to the centre.
for(c;c>0;c--){
printf(" ");
}
for(a;a<b;a++){ // Loop for the pattern.
printf("*");
}
printf("\n");
b=b+2;
a=0;
}
return 0;
}
The following code is more steady and comprised per contra to the previous.
#include <stdio.h>
int main() {
int n,a,b;
puts("Please Enter an Integer");
scanf("%d",&n);
for(b=1;n>0;n--, b += 2){
if(n!=1) {
printf("%*s", n - 1, " ");
}else{break;}
for(a=0;a<b;a++){
printf("*");
}
printf("\n");
}
return 0;
}
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.
I have to write code that displays the Fibonacci sequence to the user desired number of terms and must also use a while loop. I'm not sure why this code isn't working.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int max;
printf("Enter the max term of the Fibonacci Sequence:\n");
scanf("%i", &max);
int a=0;
int b=0;
a=2;
while(a<max) {
if((a==0||a==1))
{
printf("%i\n", &a);
++a;
}
else if(a>1)
{
a=(a-1)+(a-2);
printf("%i\n", &a);
++a;
}
}
return 0;
}
You can try this.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int max;
printf("Enter the max term of the Fibonacci Sequence:\n");
scanf("%i", &max);
int n=0;
int a=0;
int b=1;
int next;
while(n<max) {
if ( n <= 1 )
{
next = n;
n++;
}
else
{
next = a + b;
a = b;
b = next;
n++;
}
printf("%d\n", next);
}
return 0;
}
issues with your code:
following declaration & initialisation, you set a=2 => it won't take the true branch of the if statement -- '0' will not be printed in your result.
a=(a-1)+(a-2); i.e a = 1
then you are doing ++a; => a == 2. thus it again else statement with same a==2.
hence it will print the same value and loop executes infinitely.
In the very beginning of your program (before the while loop) a is 2 (see the line a=2).
And in the while loop you do following:
a=(a-1)+(a-2); // a = 2-1+2-2 i.e. a = 1
and right after it
++a; // a == 2
So, after it a==2 again. This loop never ends.
But it is technical problem. More important is that you are trying to calculate not Fibonacci Sequence. In the Fibonacci Sequence each subsequent number is the sum of the previous two. But in your code there is adding of not two previous numbers of Fibonacci Sequence, but previous two Natural numbers.
You have variable b, because someone told you to add it. And it was right! Just remember previous found element of the Fibonacci Sequence in b. When you know previous one element and current one, it is possible to calculate next one.
Task is to display the array that has no repetitions based on some user generated input.
I'm trying to compare the number with every number before it, if the equality happens, a=1, it should skip it. Code doesn't return anything.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int X[30],Y[30],i,j,k=0,a,N;
printf("Length of the vector: ");
scanf("%d",&N);
printf("Input the numbers: ");
for(i=0;i<N;i++)
scanf("%d",X+i);
Y[0]=X[0];
for(i=1;i<N;i++){
for(j=i-1;j>=0;j--)
if(X[i]=X[j])
a=1;
if(a==0){
k++;
Y[k]=X[i];
}
a=0;
}
for(i=0;i<k;i++)
printf("%d",Y[i]);
}
Three separate issues in your code block:
a is not initialized the first time through your loop. Add a line a = 0; above your loop.
Your if block reads if(X[i]=X[j]); it should be if(X[i] == X[j]) (you're missing one =)
Your final value of k is going to be one less than the total number of elements that you have. Change your final for loop to i = 0; i <= k; i++
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.