I don't see why this doesn't work - c

The goal of this code was to print a pyramid. First I print a certain number of spaces then print some stars to eventually make a pyramid.
For example to print a pyramid of 5 first it would print a star after 4 spaces then the start and end variables would be changed so the new start would 3 and the new end would be six and it would print 3 stars.
#include <stdio.h>
#include <stdlib.h>
void printSpaces(int num){
int i;
for(i=0; i<num;i++)
{
printf(" ");
}
}
void pyramid(int n){
int start=n,end=n+1;
int k;
while(start>0 && end<2*n)
{
printSpaces(start);
for (k=start; k<end;k++)
{
printf("*");
}
printf("\n");
start=n-1;
end=n+1;
}
}
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
pyramid(5);
return 0;
}
The only thing it seems to be doing printing a row of 2 stars over and over.

you set start to n-1, but the value of n never changes. That means that start will continuously be set to the same value, n-1(4). Same for end, your loop will never terminate.
void pyramid(int n){
int start=n,end=n+1;
int k;
while(start>0 && end<2*n)
{
printSpaces(start);
for (k=start; k<end;k++)
{
printf("*");
}
printf("\n");
start=n-1;
end=n+1;
}
}
Also, on first invocation, k will be 4 and end will be 6, hence two stars.

Your problem is right here:
start=n-1;
end=n+1;
it should be something like start = start + 1 ?
I don't understand your program perfectly but I can tell that this is your error.

Matt McNabb was very close.
The following code contains the correct fix
The main change is to step 'start' and 'end' rather than re-initializing them to the passed parameter
#include <stdio.h>
#include <stdlib.h>
void printSpaces(int num){
int i;
for(i=0; i<num;i++)
{
printf(" ");
}
}
void pyramid(int n){
int start=n,end=n+1;
int k;
while(start>0 && end<2*n)
{
printSpaces(start);
for (k=start; k<end;k++)
{
printf("*");
}
printf("\n");
start--; //<< corrected
end++; //<< corrected
}
}
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
pyramid(5);
return 0;
}

the procedure that you are using to decrement the value of the variables end and start are wrong
I found this code about patterns in c in this site
http://www.programmingsimplified.com/c-program-print-stars-pyramid
#include <stdio.h>
#include <stdlib.h>
void pyramid(int end){
int k,c;
for (k=1; k< end; k++) {
printf(" ");
end= end -1;
for (c=1; c<= 2 * k-1; c++)
printf("*");
printf("\n");
}
}
int main() {
pyramid(15);
return 0;
}

Related

why there is no output of following c code? is there any error of continue keyword

Here I have provided the C code and it is not print any thing
#include <stdio.h>
int main(){
int i=0;
for(;;){
if(i==10)
continue;
printf("%d ",++i);
}
return 0;
}
I believe that you want to stop the loop when i is 10 not to have an indefinite loop
int main(){
int i=0;
for(;;){
if(i==10)
break;
printf("%d ",++i);
}
printf("\n");
return 0;
}
``
The loop executes printf() 10 times before it goes into an infinite busy loop when i==10. stdout is line buffered by default (see stdout(3)) so it's not being flushed implicitly based on the small size. The cleanest fix is to call fflush():
#include <stdio.h>
int main() {
for(int i = 0;;) {
if(i==10) {
fflush(stdout);
continue;
}
printf("%d ",++i);
}
return 0;
}
You could also change the program behavior by moving the increment into the for() statement, and it will cause the size of the output to flush stdout:
#include <stdio.h>
int main() {
for(int i = 1;; i++) {
if(i!=10)
printf("%d ", i);
}
return 0;
}

How to make this program considered as a recursive rfunciton

I have created the program that functionally works the way it is supposed to but I have to make it recursive and I was told that function void expand() is not considered as a recursive function. Any tips?
#include <stdio.h>
#include <string.h>
#define max_size 300
int main(){
char defstr[max_size]="The universe is ever expanding!";
int num, buff_num;
printf("Input how much to expand: ");
scanf("%d",&num);
buff_num=num;
expand(defstr, num, 0, 0);
return 0;
}
void expand(char str[max_size], int n, int dash, int i){
if(n>-1){
if(str[i+1]!='\0'){
printf("%c",str[i]);
for(int j=dash; j>0; j--)
printf("-");
expand(str, n, dash, i+1);
}
else if(str[i+1]=='\0'){
printf("%c", str[i]);
printf("\n");
expand(str, n-1, dash+1, 0);
}
}
}
The program is built to output this type of text depending on the user input of how many dashes.
T-h-e- -u-n-i-v-e-r-s-e- -i-s- -e-v-e-r- -e-x-p-a-n-d-i-n-g-!
T--h--e-- --u--n--i--v--e--r--s--e-- --i--s-- --e--v--e--r-- --e--x--p--a--n--d--i--n--g--!
T---h---e--- ---u---n---i---v---e---r---s---e--- ---i---s--- ---e---v---e---r--- ---e---x---p---a---n---d---i---n---g---!
It is a recursive function in that it calls itself. However, my guess is that is is supposed to print the entire string with dashes each time.
void expand(char *str, int num_dashes)
{
// End recursion
if (num_dashes < 0) return;
// Call recursively
expand(str, num_dashes - 1);
// Print entire string with dashes
for (int i = 0; str[i]; i++) {
if (i != 0) {
for (int j = 0; j < num_dashes; j++) {
fputc('-', stdout);
}
}
fputc(str[i], stdout);
}
puts("");
}

Write a C code that will print out the following pattern using nested for loops

I am not getting the output that I am looking for. Could someone help me with spotting the problem? The output that I am supposed to get is:
OXXX
XOXX
XXOX
XXXO
My code is:
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
int i;
int j;
int counter_1= 0;
int counter_2= 0;
for (i=0;i<4;i++) {
for (j=0;j<4;j++) {
if (counter_1==counter_2) {
printf("O");
} else {
printf("X");
}
counter_2++;
}
counter_1++;
printf("\n");
}
return 0;
}
Out of wrong why searching for something so complicated ? The initial cause of your bug is the complexity of your solution.
The goal is to draw a diagonal, from the definition of the diagonal when the column number and the line number are equal put a O else a X :
#include <stdio.h>
int main() {
unsigned line, column;
const unsigned size = 4; /* any square size you want */
for (line = 0; line < size; ++line) {
for (column = 0; column < size; ++column) {
putchar((column == line) ? 'O' : 'X');
}
putchar('\n');
}
return 0;
}
Compilation and execution :
pi#raspberrypi:/tmp $ gcc -Wall -g c.c
pi#raspberrypi:/tmp $ ./a.out
OXXX
XOXX
XXOX
XXXO
pi#raspberrypi:/tmp $
You are forgetting to reset counter_2 back to 0 after each inner loop completes and also, if you want the patterns to be on the same line, replace the printf("\n"); with printf(" ");
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
int i;
int j;
int counter_1= 0;
int counter_2= 0;
for (i=0;i<4;i++) {
for (j=0;j<4;j++) {
if (counter_1==counter_2) {
printf("O");
} else {
printf("X");
}
counter_2++;
}
counter_1++;
counter_2 = 0; // Reset counter back to 0
printf(" "); // Print space between each pattern
}
return 0;
}

Difference in answers while implementing insertion sort

I tried to implement insertion sort in C using while and for loop as follows:
#include <stdlib.h>
int main()
{
int a[]={4,7,8,2,16,21,12,3,1};
int n=sizeof(a)/sizeof(a[0]);
int j,k,i,x;
/*for(j=0;j<n;j++)
{
k=a[j];
for(i=j-1;i>=0 && k<a[i];i--)
a[i+1]=a[i];
a[i+1]=k;
}*/
for(i=1;i<n;i++)
{
k=a[i];
j=i-1;
while(j>=0 && a[j]>k)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=k;
}
printf("The elements of the array are: ");
for(x=0;x<n;x++)
printf("%d ", a[x]);
return 0;
}
And it works totally fine. But, When I tried to remove the use of variable 'k'. The answer prints as follows:
My latter code as follows:
#include <stdlib.h>
int main()
{
int a[]={4,7,8,2,16,21,12,3,1};
int n=sizeof(a)/sizeof(a[0]);
int j,k,i,x;
/*for(j=0;j<n;j++)
{
k=a[j];
for(i=j-1;i>=0 && k<a[i];i--)
a[i+1]=a[i];
a[i+1]=k;
}*/
for(i=1;i<n;i++)
{
//k=a[i];
j=i-1;
while(j>=0 && a[j]>a[i])
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=a[i];
}
printf("The elements of the array are: ");
for(x=0;x<n;x++)
printf("%d ", a[x]);
return 0;
}
Can somebody explain this?. Does anything changes the value of the variable 'i' during while loop.
k=a[i] saves the value because the while loop
while (...) {
a[j+1] =a[j];
}
overwrites a[i] at first iteration, because j starts at i-1 so j+1 is i at the first iteration.

Drawing a triangle with one loop (Revised)

The triangle should look like this
1
11
111
1111
11111
111111
the number of rows is entered by the user and then transforms to the function
Note: Without arrays, must be with one loop(while or for) and not nested loops
the closest I got is a code with 2 loop (but can`t think about how to do it with less loops)
int i,k;
for(i=1;i<=x;i++)
{
for(k=1;k<=i;k++)
{
printf("1");
}
printf("\n");
}
The above question was asked by someone but it was marked as off topic I don't know why..?
I came up with this solution tell me if it's correct or not?
int i=1,k=1;
while (i<=x)
{
if(k<=i)
{
printf("1");
k++;
continue;
}
i++;
k=1;
printf("\n");
}
thanks
You can replace loops(iteration) with recursion, try this(with one loop):
#include <stdio.h>
void draw(int depth)
{
int i;
if(depth <= 0) return;
draw(depth-1);
for(i = 0; i < depth; i++)
printf("1");
printf("\n");
}
int main()
{
draw(5);
return 0;
}
You can do it even without loop
#include <stdio.h>
void draw_num_char(int num)
{
if(num <= 0) return;
printf("1");
draw_num_char(num-1);
}
void draw(int depth)
{
int i;
if(depth <= 0) return;
draw(depth-1);
draw_num_char(depth);
printf("\n");
}
int main()
{
draw(5);
return 0;
}
Recursive solution, one loop, no arrays.
#include <stdio.h>
void liner(int line, int limit) {
int i;
if (line > limit)
return;
for(i=1; i<=line; i++)
printf("1");
printf("\n");
liner (line + 1, limit);
}
int main() {
int limit;
printf ("Enter number of lines ");
scanf ("%d", &limit);
liner (1, limit);
return 0;
}

Resources