Let us (Turbo) C will help you to learn C Programs in different flavors.
Conditions, Loops, functions, arrays, constants, header files, file handlings etc. All the program samples/example are executed/tested in Turbo C only.
Description:
Print Pascal Triangle using C Program
/*
C Program full source code for Pascal Triangle
Pascal Triangle using C program
Coding for Pascal Triangle
*/
#include <stdio.h>
#include <stdlib.h>
/* Global Declaration */
int n, x[25][25];
int main()
{
/* Get Input value from User i.e. N */
printf("Please enter the N value:\n");
scanf("%d", &n);
/*
Function #1
-----------
Validate the Input value i.e N
*/
void validateInput() {
if (n<1 || n > 25) {
printf("Input is beyond this program Assumption !! \nPlease enter input value {1 to 25}\n");
exit(0);
}
}
/*
Function #2
-----------
Initialize default values
*/
void intializeDefaultValues(){
int i;
/* Fill default values in Row wise */
for (i=1; i<=n; i++){
x[i][1] = 1;
}
/* Fill default values in Column wise */
for (i=1; i<=n; i++){
x[1][i] = 1;
}
/*
Note:
In above code, we can use single FOR LOOP to initialize the Row/Column wise values.
Just making it simplified; It is your wish to simply this code further.
*/
}
/*
Function #3
-----------
Fill remaining values
*/
void fillRemainingValues(){
int i, j;
for(i=2; i<=n; i++){
for (j=2; j<=n; j++){
/*
Skip the x[i][j] position Summation
if (i-1) is zero (or)
if (j-1) is zero
*/
Description:
Music Visualization Simulation using C Program. Dynamically it will pick the Themes.
/*
Music Visualizer Simulation using C Program
Music Visualization Dynamic Effect using C Program
Track Music using Simple Animation in C
Music Visualize Print using C Program
Vertical Progress Bar Print using C Program
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
/* Defined Constants */
#define TRACK_DELAY 15;
/* Global Function Prototype Declaration */
void clearColumnTrack();
void setColor();
void setTrackCharacter();
void printBaseLine();
/* Global Variables */
int trackChar, cols[25], rows[15];
int main(){
/* Local Function Prototype Declaration */
void printRandomTrack();
void setColumnPosition();
void setRowPosition();
/* Clear Specific Column */
void clearColumnTrack(int column){
int i, row=20;
for (i=1;i<=10;i++){
gotoxy(column, row--);
printf(" ");
}
}
/* Set a Track Color Randomly */
void setColor(){
int color;
color = random(15);
/* Avoid BLACK color */
if (color == 0) color = 1;
/* Set Color */
textcolor(color);
}
/* Get Random Track Character */
int getTrackCharacter(){
int rands, trackChars[]={176, 177, 178, 179, 220, 223};
randomize();
/* Get a random Character */
rands = random(6);
/* Set Random Track Characters */
return trackChars[rands];
}
/* Get Track Character */
void setTrackCharacter(){
trackChar = getTrackCharacter();
}
/* Set Column Position */
void setColumnPosition(){
int i, randCols=0;
/* Column Array values */
for (i=31; i<=51; i++){
cols[randCols]=i;
randCols++;
}
}
/* Set Row Position */
void setRowPosition(){
int i, randRows=0;
/* Row Array values */
for (i=20; i<=30; i++){
rows[randRows]=i;
randRows++;
}
}
/* Draw Base Line */
void printBaseLine(){
int i, colVal=26;
for (i=1; i<=30; i++){
gotoxy(colVal++, 20);
printf("-");
}
}
Description:
15 Puzzle Game with full source source. By Default program will load 1-15 number in randomly in 4x4 matrix. User can use Arrow Keys (Up, Left, Right and Down) to arrange the Number in a proper order starts from 1,1 to 4,3. Once all the values are arranged then Program will ends.
Help Option !! Free Moves option introduced to help user when user stuck!! By pressing ENTER key system will arrange next SEQUENCE number and use can continue the game!!. Free Move is present in Defined constant and By default as 3 free moves.
Invalid Arrow Option!! Program will dynamically display the Current Keypress in a User friendly manner and if user press invalid Arrow Key it will display it accordingly !!
Move Count!! Program will count the Total number of Valid Key press and it will show in a User friendly manner and in case Invalid Key pressed, system will not counter it!! In case user uses Free Move then only the Move Counter will get incremented to 1.. That's not a Problem!!
/*
15 Puzzle Problem in C
15 Puzzle Game Online
15 Puzzle Game Source Code in C
15 Puzzle Game using C Program
15 Puzzle Simple Animation Program using C
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
/* Defined Constants */
#define true 1;
#define false 0;
#define FREE_MOVES 3;
/* Global Variable Declaration */
int puzArr[5][5], row=1, column=1;
int curRow=4, curCol=4, totalMoves=0, freeMoves=0;
/* Global Prototype Declaration */
int getRandVal();
int setRandVal(int);
void clearScreen(int);
void draw15PuzzleBox();
void movePosition(int);
void swapValue(int,int);
void printPosition(char *);
int IsDone(void);
void main(){
char inputKey, arrowKey;
/* Local Prototype Declaration */
int loadDefaultData();
void resetRowColumn();
void printTotalMoves();
void printTotalFREEMoves();
int doFREEMove();
/* Clear the Screen */
clrscr();
/* Randomize */
randomize();
/* Set Free Moves */
freeMoves = FREE_MOVES;
/* Load Default Data */
gotoxy(1,1);
printf("Please wait.. Loading Data !!");
loadDefaultData();
/* Print Loaded Data */
draw15PuzzleBox();
/* Detect Arrow Keys in C Program
Press ESCAPE - 27
Press ENTER - 13
Get out from the Indefinite loop
*/
while ( (inputKey = getch()) != 27){
/* Set Default Text color as WHITE */
textcolor(15);
clrscr();
/* Find Arrow Key or not */
if (inputKey == '\0'){
/* Extract actual Arrow Key */
arrowKey = getch();
switch(arrowKey){
case 72:
movePosition(72);
draw15PuzzleBox();
printTotalMoves();
printTotalFREEMoves();
break;
case 80:
movePosition(80);
draw15PuzzleBox();
printTotalMoves();
printTotalFREEMoves();
break;
case 75:
movePosition(75);
draw15PuzzleBox();
printTotalMoves();
printTotalFREEMoves();
break;
case 77:
movePosition(77);
draw15PuzzleBox();
printTotalMoves();
printTotalFREEMoves();
break;
}
} else if (inputKey == 13){
//printf("Enter key pressed..");
clrscr();
if (freeMoves > 0){
doFREEMove();
/* Increment Moves */
totalMoves++;
draw15PuzzleBox();
printTotalMoves();
printTotalFREEMoves();
} else {
draw15PuzzleBox();
printTotalMoves();
printTotalFREEMoves();
}
}
/* Check 15-Puzzle Completion */
if (IsDone()) {
/* Clear the Screen and Announce the Result */
clrscr();
/* Print Total Moves */
printTotalMoves();
gotoxy(35, 13);
textcolor(2);
printPosition("Great!!");
getch();
/* Smoothly End the Program Execution ;) */
exit(1);
}
}
}
/* Assign Random Value */
int loadDefaultData(){
int i=1, j=1, randVal;
/* Custom Clear Screen
0 - Row Wise Clear
1 - Column Wise Clear
*/
randVal = random(1);
clearScreen(randVal);
for (; i<=4;) {
for (; j<=4;) {
/* Draw Puzzle Box */
draw15PuzzleBox();
/* get Random value */
randVal = getRandVal();
if ( setRandVal(randVal) ){
/* Set the Next Value */
puzArr[i][j] = randVal;
/* Increment next Column */
column = j;
j++;
/* Check Is all 15 values set */
if (i==4 && j==4) {
/* Set Zero to Final Position */
puzArr[i][j] = 0;
movePosition(0);
return 1;
}
}
}
/* increment to Next Row */
row = i;
i++;
/* Re-initialize the Column value */
j=1;
}
/* Normal Data Load Completion */
return 1;
}
/* Get a Random Number from 1 to 15 */
int getRandVal(){
int i, randVal;
int dataArr[15];
for (i=0; i<15; i++){
dataArr[i] = i+1;
}
randVal = random(15);
randVal = dataArr[randVal];
return randVal;
}
/* Check and Set a Random Value to next EMPTY position */
int setRandVal(int checkVal){
int i, j;
for (i=1; i<=4; i++){
for (j=1; j<=4; j++) {
/* Is this value already present and Allocated */
if (puzArr[i][j] == checkVal) {
return false;
}
}
}
/* Yes, We got a next Rand value for Next position */
return true;
}
/* Draw the Current Data */
void draw15PuzzleBox() {
int i, j, charPos=177, row=3;
//int charSmile=2;
/* Make Position to Draw the 15-Puzzle Box */
gotoxy(3, row++);
for (i=1; i<=20;i++) printf("%c",charPos);
gotoxy(3, row);
printf("%c", charPos);
gotoxy(4, row++);
for (i=1; i<=18;i++) printf(" ");
printf("%c", charPos);
for(i=1; i<=4; i++){
gotoxy(3, row++);
printf("%c", charPos);
for(j=1; j<=4; j++){
if (puzArr[i][j]){
printf("%4d", puzArr[i][j]);
} else {
//printf("%4c",charSmile);
printf(" ");
}
}
printf(" %c", charPos);
}
gotoxy(3, row);
printf("%c", charPos);
gotoxy(4, row++);
for (i=1; i<=18;i++) printf(" ");
printf("%c", charPos);
gotoxy(3, row++);
for (i=1; i<=20;i++) printf("%c",charPos);
/* Show Current Position Information */
gotoxy(35, 13);
}
/* Clear the Screen */
void clearScreen(int rowOrColumn){
int i, j;
for(i=1; i<= 25; i++){
for (j=1; j<=80; j++){
/* Row Wise - 0 */
if (rowOrColumn == 0){
gotoxy(i, j);
}
/* Column Wise - 1 */
else if (rowOrColumn == 1){
gotoxy(j, i);
}
printf(" ");
}
delay(10);
}
/* Show Current Position Information */
gotoxy(35, 13);
}
/* Reset Global Row & Column */
void resetRowColumn(){
row = 1;
column = 1;
}
/* Move position */
void movePosition(int pos){
int iRow, iCol;
/* Keep Current Row & Column Positions for SWAP */