How to create a modular function in C to print stars.
Basic C program.
Date Created:Wednesday March 07th, 2007 06:30 AM
Date Modified:Saturday August 02nd, 2008 03:16 PM
#include <stdio.h>
void printstar(int num, char ch);
void printspace(int x, int s, char ch);
void printLight();
int main() {
int i;
for (i = 0; i<=9; i++) printLight(i);
return 0;
}
void printstar(int num, char ch) {
int i;
for (i=0; i<num; i++) {
printf("%c", ch);
}
printf("\n");
}
void printspace(int numSpaces, int numberStars, char ch) {
int i;
for (i=0; i<numSpaces; i++) printf(" ");
printstar(numberStars, ch);
}
void printLight(int type) {
int i;
if (type ==0) for (i = 0;i<25;i++) printspace(i, i, 'd');
if (type ==1) for (i = 0;i<25;i++) printspace(25-i, i, 'd');
if (type ==2) for (i = 0;i<25;i++) printspace(i, i, 'd');
if (type ==3) for (i = 0;i<25;i++) printspace(i, 25-i, 'd');
if (type ==4) for (i = 0;i<25;i++) printspace(25-i, i, 'd');
if (type ==5) for (i = 0;i<25;i++) printspace(i, 25-i, 'd');
if (type ==6) for (i = 0;i<25;i++) printspace(25-i, i, 'd');
if (type ==7) for (i = 0;i<25;i++) printspace(i, 25-i, 'd');
if (type ==8) for (i = 0;i<25;i++) printspace(25-i, i, 'd');
if (type ==9) for (i = 0;i<25;i++) printspace(i, 25-i, 'd');
}
#include <stdio.h>
#define max 9
void printstar(int num, char ch);
void printspace(int x, int s, char ch);
void printLight(int type);
int main() {
int i;
char yes=0;
do {
for (i = 0; i <= max; i++) {
printLight(i);
if (i != max) system("pause");
}
printf("Go Again? Press y to repeat. Any other key to quit.");
scanf("%c",&yes);
} while (yes == 'y');
return 0;
}
void printstar(int num, char ch) {
int i;
for (i=0; i<num; i++) {
printf("%c", ch);
}
printf("\n");
}
void printspace(int numSpaces, int numberStars, char ch) {
int i;
for (i=0; i<numSpaces; i++) printf(" ");
printstar(numberStars, ch);
}
void printLight(int type) {
int i;
if (type ==0) for (i = 0;i<25;i++) printspace(i, 25-i, 'd');
if (type ==1) for (i = 0;i<25;i++) printspace(25-i, i, 'd');
if (type ==2) for (i = 0;i<25;i++) printspace((i/2)-i, 25-i, 'd');
if (type ==3) for (i = 0;i<25;i++) printspace(i, i, 'd');
if (type ==4) for (i = 0;i<25;i++) printspace(i-(i/2), i, 'd');
if (type ==5) for (i = 0;i<25;i++) printspace(i-(i/5), i, 'd');
if (type ==6) for (i = 0;i<25;i++) printspace(i/2, i-(i/16), 'd');
if (type ==7) for (i = 0;i<25;i++) printspace(i/4, i/4, 'd');
if (type ==8) for (i = 0;i<25;i++) printspace(i/2, i, 'd');
if (type ==9) for (i = 0;i<25;i++) printspace(i+(12-i/2), 12-i, 'd');
}
Downloads:
Download: printstars.c 1 KB
Download: stars.c 1 KB
Please login or Click Here to register for downloads
Print Stars by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
