Question About Printing a Sequential Pattern - c

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

Related

How to take input multiple times even after the execution of program in C?

so I was writing this program to find the number of times a digit is repeated in a number.How can I ask the user to enter inputs again and again even after the execution at the first time
#include <stdio.h>
int main()
{
int rem,flag,n;
int frequency[10]={0};
printf("enter a number");
scanf("%d",&n);
while(n>0){
rem=n%10;
frequency[rem]++;
n=n/10;
}
for(int i=0;i<10;i++)
{
if(frequency[i]>1){
flag=1;
printf("%d is repeated %d times",i,frequency[i]);
}
}
if(flag==0){
printf("no number is repeated");
}
return 0;
}
One way to do this would be to wrap your code in a while (1) loop (loop forever). I've added an extra check: if a user enters the number 0, the program will break out of the loop and exit. There has to be some way to stop.
#include <stdio.h>
int main()
{
while (1){
int rem,flag=0,n;
int frequency[10]={0};
printf("enter a number");
scanf("%d",&n);
if (n==0){
break;
}
while(n>0){
rem=n%10;
frequency[rem]++;
n=n/10;
}
for(int i=0;i<10;i++)
{
if(frequency[i]>1){
flag=1;
printf("%d is repeated %d times",i,frequency[i]);
}
}
if(flag==0){
printf("no number is repeated");
}
}
return 0;
}
For a cleaner solution, you may want to create a function for everything within the while loop, and call that function.
Side note: flag has to be set to 0 at the start of the loop.

terminated by signal SIGSEGV (Address boundary error)

I am trying to query the user for first a number of inputs, then fill a dynamic list with that number of inputs. Then print them in reverse order. For some reason I get the SIGSEGV error, but I cannot find any infinite loops or address error. Help me find what is wrong.
#include <stdio.h>
#include <stdlib.h>
int main() {
int input;
printf("Enter a non-negative number: ");
scanf("%d", &input);
int *listA;
listA = (int*)malloc(sizeof(int[input]));
printf("Now, enter %d non-negative numbers: ", input);
for (int i = 0; i < input; i++) {
scanf("%d", listA[i]);
}
printf("In reverse order, your input is: ");
for (int i = input-1; i >=0; i-- ) {
printf("%d",listA[i]);
}
free(listA);
return 0;
}
Try compiling your code with all warnings enabled (-Wall with gcc). Your line:
scanf("%d", listA[i]);
should be:
scanf("%d", &listA[i]);

Program to accept an array of n elements and count number of palindromes

#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int x,d,a[10],i,sum=0,count=0,n;
cout<<"Enter no of numbers:";
cin>>n;
for(i=0;i<n;++i)
{
cout<<"Enter number"<<i+1<<":";
cin>>a[i];
}
for(i=0;i<n;++i)
{
x=a[i];
while(x!=0)
{
d=x%10;
sum=sum*10+d;
x=x/10;
}
if(sum==a[i])
count++;
}
cout<<"No of palidromes:"<<count;
}
I entered 121,134 and 1331 but the output was always 1. In fact I tried more numbers and still got only 1. Please tell me what's wrong.
Add sum = 0; after your x=a[i];

Getting an error while running following code

I wrote a code but I don't know why it does not work and print"beterek" .
also it doesn't go through my while loop and doesn't call my cam func...
please help...
the following code is one part of my code:
int main()
{
int a[100], b[100], diff=0, m=0, n=0, temp[100], s=0,z,max=0;
printf("Enter binary number 1: ");
scanf("%d", &a[100]);
printf("Enter binary number 2: ");
scanf("%d", &b[100]);
while(a!='\0')
m++;
while(b!='\0')
n++;
if(m>n)
{
printf("beterek");
max = m;
diff = m - n;
for(s=0; s<=diff; s++)
temp[s] = 0;
for(z=s; z<=n; z++)
temp[s] = b[s];
cal(a,temp);
}
else
{
printf("beterek");
max = n;
diff = n - m;
for(s=0; s<=diff; s++)
temp[s] = 0;
for(z=s; z<=m; z++)
temp[s] = a[s];
cal(b, temp);
}
}
Use fgets to read the input string till you a reach a newline character (that means till the user hits enter). Then convert each character to int.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a[100], b[100], diff=0, m=0, n=0, temp[100], s=0,z,max=0;
int iIndex=0;
char cString[100];
int i=0;
char* cpString=NULL;
memset(&cString,0,100);
printf("Enter binary number 1: ");
//scanf("%s",cString);
fgets(cString,sizeof(cString),stdin);
cpString=cString;
while(*cpString!='\n'){
a[iIndex]=*cpString-'0'; // this converts char to int
cpString++;
iIndex++;
}
printf("Count of elements in a : %d\n", iIndex);
for (i=0;i<iIndex;i++){
printf("%d\n",a[i]);
}
return 0;
}
Output:
Enter binary number 1: 1101010
Count of elements in a : 7
1
1
0
1
0
1
0
Just make sure you check wether the input number is a valid binary number!

Please help me debug my Insertion Sort Program

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

Resources