Skip to content

Instantly share code, notes, and snippets.

@Taresin
Last active June 15, 2020 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Taresin/d6f5c030fbf788dd18450937bd7d392c to your computer and use it in GitHub Desktop.
Save Taresin/d6f5c030fbf788dd18450937bd7d392c to your computer and use it in GitHub Desktop.
COSC2138 - Week 2 Lab exercises
Left Aligned
*
**
***
****
*****
******
*******
********
*********
**********
Right Aligned
*
**
***
****
*****
******
*******
********
*********
**********
Center Aligned
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
Center Aligned Diamond
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Grid
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
Process finished with exit code 0
#include "stdio.h"
#include "stdlib.h"
void printStarLine(int spaces, int stars);
int main() {
int rows = 10;
/* Question 1
* Left aligned stars
* */
printf("Left Aligned\n");
for (int i = 1; i <= rows; ++i) {
for (int j = 0; j < i; ++j) {
printf("*");
}
printf("\n");
}
printf("\n");
/* Question 2
* Right aligned stars
* */
printf("Right Aligned\n");
for (int i = 1; i <= rows; ++i) {
int spaces = rows - i;
int stars = i;
printStarLine(spaces, stars);
printf("\n");
}
printf("\n");
/* Question 3
* Center aligned stars
* */
printf("Center Aligned\n");
int triangleWidth = 2 * rows;
int triangleSpaces = triangleWidth / 2 - 1;
int triangleStars = 1;
for (int i = 1; i <= rows; ++i) {
printStarLine(triangleSpaces, triangleStars);
printf("\n");
triangleSpaces--;
triangleStars += 2;
}
printf("\n");
/* Question 4
* Center aligned diamond
* */
printf("Center Aligned Diamond\n");
int diamondWidth = 2 * rows;
int diamondSpaces = diamondWidth / 2 - 1;
int diamondStars = 1;
for (int i = 0; i <= rows; ++i) {
printStarLine(diamondSpaces, diamondStars);
printf("\n");
int delta;
if (i < rows/2) {
delta = 2;
} else {
delta = -2;
}
diamondSpaces -= delta / 2;
diamondStars += delta;
}
printf("\n");
/* Question 5
* Grid
* */
printf("Grid\n");
for (int i = 0; i < rows; ++i) {
char *unit;
if (i % 2 == 0) {
unit = "* ";
} else {
unit = " *";
}
for (int j = 0; j <= rows; ++j) {
printf("%s", unit);
}
printf("\n");
}
printf("\n");
return EXIT_SUCCESS;
}
void printStarLine(int spaces, int stars) {
for (int j = 0; j < spaces; ++j) {
printf(" ");
}
for (int k = 0; k < stars; ++k) {
printf("*");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment