The pyramid

hey guys i need help here i can't create the exact same code as my friend here is the code
#include <stdio.h>

int main() {
int height;
int a;
int pager;
int spacel;
do {
printf("Height: ");
scanf_s("%d", &height);
} while (!(height > 0) || !(height < 24));
for (a = 0; a < height; a++) {
for (spacel = height - a; spacel > 0; spacel--) {
printf(" ");
}
for (pager = 0; pager < (a + 1); pager++) {
printf("#");
}
printf(" ");
for (pager = 0; pager < (a + 1); pager++) {
printf("#");
}
printf("\n");
}
return 0;
}
and this is my version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include <stdio.h>
int main()
{
	int a;
	int i;
	int space1;
	int pager;

	printf("Insert the height: ");
	scanf_s("%d", &a);
	{
		if ((!(a > 23) & !(0 > a)))  
			printf("Enjoy");
		else
			printf("Invalid (Range: 0-23)");
	}
	

	for (i = 0; i < a; i++);

	{
		
			for (space1 = a - i; space1 > 0; space1--) 
			printf(" ");
		
			for (pager = 0; pager < (i + 1); pager++) 
			printf("#");
		
			printf("  ");
			for (pager = 0; pager < (i + 1); pager++) 
			printf("#");
		
		printf("\n");
	}
	
	getchar ();
	getchar ();
	getchar ();
	return (0);
}
You did not explain the "hole". Is it:
    #
   ###
  ## ##
 #######
#########


Overall, "your" and "friend" versions seem identical (renaming variables does not count).
Main issues:
1. C . It's 2018, why C unless absolutely forced....
2. Your bounds check of "a" doesn't do what you think it does. & is a binary operation. && is "and".
3. You put a semicolon at line 19, so the for loop basically does nothing.

example code (C++):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using std::cout;
using std::cin;

int main()
{
  int height;
  cout << "Enter integer height (1-23): ";
  cin >> height;
  if ( !(1<=height && height<=23) )
  {
    cout << "Invalid range\n";
    return -1;
  }
  cout << "\nEnjoy\n";

  for (int i=0; i<height; ++i)
  {
    for (int space1 = height - i; space1 > 0; space1--) 
      cout << " ";
    
    for (int pager = 0; pager < (i + 1); pager++) 
      cout << "#";
    
    cout << "  ";
    for (int pager = 0; pager < (i + 1); pager++) 
      cout << "#";
    
    cout << "\n";
  }
  
  return 0;
}


In action: https://repl.it/repls/ImportantCheerfulMetric
Last edited on
Topic archived. No new replies allowed.