/*
title: Card Dealing Program
coder: aeriqusyairi
date:feb5 2012
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void shuffle( int wDeck[][ 13 ] );
void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[] );
int main(){
const char *suit[ 4 ] = { "Heart", "Diamonds", "Clubs", "Spades" };
const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
int deck[ 4 ][ 13 ] = { 0 };
srand( time( 0 ) );
shuffle( deck );
deal( deck, face, suit );
system("pause");
return 0;
}
void shuffle( int wDeck[][ 13 ] ){
int row, column, card;
for(card = 1; card <= 52; card++){
do{
row = rand() % 4;
column = rand() % 13;
}while( wDeck[ row ][ column ] != 0 );
wDeck[ row ][ column ] = card;
}
}
void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[] ){
int card, row, column;
for(card = 1; card <= 52; card++){
for(row = 0; row <= 3; row++){
for(column = 0; column <= 12; column++){
if( wDeck[ row ][ column ] == card ){
printf("%5s of %-8s%c", wFace[ column ], wSuit[ row ], card % 2 == 0 ? '\n' : '\t' );
}
}
}
}
}
My C Repository
C How to Program
Sweet thanks to my broski Yusuke Fiz for this Deitel book.
You light up my life dude.
Just another personal public repository.
Friday, March 16, 2012
1) Card Shuffling and Dealing: Card Dealing Program
Simply deals a deck of card :)
Total Sales
A company has four salespeople who sells five different products.Once a day, each salespeople passes in a slip for each different type of product sold.Each slip contains:
- The salespeople number
- The product number
- The total dollar value of that product sold that day
/*
title: totalSales
coder:aeriqusyairi
date: Jan29 2012
*/
#include<stdio.h>
#define PRODUCT 5
#define SALESPERSON 4
int main(){
int sales[ PRODUCT ][ SALESPERSON ] = { 0 };
int totalByProduct[ PRODUCT ] = { 0 };
int totalByPerson[ SALESPERSON ] = { 0 };
int options = 1, i, j, personNumber, productNumber, totalSold;
while( options == 1 ){
printf("*******\nOptions\n*******\n"
"1.Pass sales slip\n"
"2.Compute and tabulate overall data\n"
"Options:");
scanf("%d", &options);
if( options == 1 ){
printf("Salesperson number:");
scanf("%d", &personNumber );
printf("Product number:");
scanf("%d", &productNumber );
printf("Total sold today:" );
scanf("%d", &totalSold );
sales[ productNumber - 1 ][ personNumber - 1 ] += totalSold;
}
}
if( options == 2 ){
printf("\n\n************\nData summary\n************"
"\n\n %5d%5d%5d%5d\n\n", 1, 2, 3, 4 );
for( i = 0; i < PRODUCT; i++ ){
printf("%2d", i + 1 );
for( j = 0; j < SALESPERSON; j++ ){
printf("%5d", sales[ i ][ j ] );
totalByProduct[ i ] += sales[ i ][ j ];
}
printf("%5d\n", totalByProduct[ i ] );
}
printf("\n ");
for( i = 0; i < SALESPERSON; i++){
for( j = 0; j < PRODUCT; j++ ){
totalByPerson[ i ] += sales[ j ][ i ];
}
printf("%5d", totalByPerson[ i ] );
}
printf("\n\n");
}
system("pause");
return 0;
}
Airline Reservations System
Assign a seat in an aircraft.
[Note: This program runs one time only so its make no sense. Just to show the algorithm. ]
[Note: This program runs one time only so its make no sense. Just to show the algorithm. ]
/*
title: airlineReservationSystem
coder: aeriqusyairi
date: 29Jan 2012
*/
#include<stdio.h>
#include<stdlib.h>
#define SEATS 10
int main(){
int seat[ SEATS ] = { 0 };
int i, assignSeat, seatNumber, noSeat = 0, terminator = 0;
char switchClass;
while( terminator == 0 ){
while( noSeat == 0 ){
printf("Please type 1 for \"first class\"\n"
"Please type 2 for \"economy\"\n");
scanf("%d", &assignSeat);
noSeat += 2;
}
if( assignSeat == 1){
for(i = 0; i < SEATS / 2; i++){
if( seat[ i ] == 0 ){
seat[ i ] == 1;
noSeat = 0;
terminator = 1;
printf("*************\nBoarding Pass\n*************\n\n"
"Your seat number: %d\nClass: First\n", i + 1 );
}else{
noSeat = 1;
}
if( terminator == 1 )
break;
}
}else if( assignSeat == 2 ){
for(i = ( SEATS / 2 ); i < SEATS; i++){
if( seat[ i ] == 0 ){
seat[ i ] == 1;
noSeat = 0;
terminator = 1;
printf("*************\nBoarding Pass\n*************\n\n"
"Your seat number: %d\nClass: Economy\n", i + 1 );
}else{
noSeat = 1;
}
if( terminator == 1 )
break;
}
}else{
system("cls");
printf("Invalid input!\n");
}
if( noSeat == 1 ){
if( assignSeat == 1 ){
assignSeat = 2;
printf("The seat in first class is full.\nCan we assign you to another class?[y or n]\n");
}else if( assignSeat == 2 ){
assignSeat = 1;
printf("The seat in first class is full.\nCan we assign you to another class?[y or n]\n");
}
scanf(" %c", &switchClass );
if( switchClass == 'n' ){
printf("Next flight leaves in 3 hours.\n");
terminator = 1;
}
}
}
system("pause");
return 0;
}
Dice Rolling
Program below simulate the rolling of two dice.The dices is rolled 36000 times and the sum of two dice is calculated for each rolled.The numbers of times each possible sum appears is tallied in an array.
Dice rolling outcomes
/*
title: DiceRolling
author: aeriqusyairi
date: Jan26 2012
*/
#include<stdio.h>
#include<stdlib.h>
#define ROLL 36000
int rollDice( void );
void printFreq( const int b[], int size );
int main(){
int freq[ 13 ] = { 0 };
int i, x;
for(i = 0; i < ROLL; i++){
x = rollDice();
++freq[ x ];
}
printFreq( freq, 13 );
system("pause");
return 0;
}
int rollDice( void ){
return ( 1 + (rand() % 6) ) + ( 1 + (rand() % 6) );
}
void printFreq( const int b[], int size ){
int i;
for(i = 2; i < size; i++){
printf("%d occured %d times.\n", i, b[ i ] );
}
}
Duplicate Elimination
Read in 20 numbers, each of which is between 10 and 100, inclusive.Then print it only if it's not duplicate of a number already read.
/*
title: duplicateElimination
author: aeriqusyairi
date: Jan26 2012
*/
#include<stdio.h>
#define SIZE 20
int main(){
int array[ SIZE ] = { 0 };
int i, j, k = 0, h, number;
printf("Input 20 number between 10 and 100 (inclusive)...\n");
for(i = 0, h = 0; i < SIZE; i++, k = 0, h++){
scanf("%d", &number);
for(j = 0; j < SIZE; j++){
if(number == array[ j ])
k = 1;
}
if(k == 0){
printf("You entered an exclusive number %d\n", number );
array[ h ] = number;
}
}
system("pause");
return 0;
}
Bubble Sort Modification
Previous bubble-sort program is inefficient for large arrays.
The following is the modification to that previous program.The program is modify as follow:
The following is the modification to that previous program.The program is modify as follow:
- After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array and so on.Instead of making nine comparisons on every pass, this new program make eight comparisons on the second pass, seven on the third pass, and so on.
- Check at the end of each pass whether any swaps have been made. If none has been made, then the data must already be in the proper order, so the program should terminated.
/*
title: bubbleSortModification
author: aeriqusyairi
date: Jan25 2012
*/
#include<stdio.h>
#define SIZE 10
int main(){
int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
int pass, i, j, k, hold;
printf("Data items in original order\n");
/*output original array*/
for(i =0; i < SIZE; i++){
printf("%4d", a[i]);
}
/*bubble sort*/
for(pass = 1, j = 1; pass < SIZE; pass++, j++, k = 0 ){
for(i = 0 ; i < SIZE - j; i++){
if(a[i] > a[i+1]){
hold = a[i];
a[i] = a[i + 1];
a[i + 1] = hold;
k++;
}
}
/*check if any swap happen*/
if(k == 0)
break;
}
printf("\nData items in ascending order\n");
for(i = 0; i < SIZE; i++){
printf("%4d", a[i]);
}
printf("\n");
system("pause");
return 0;
}
Bubble Sort
Sorting an array with bubble sort.
/*
title: BubbleSort
author: aeriqusyairi
date: Jan25 2012
*/
#include<stdio.h>
#define SIZE 10
int main(){
int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
int pass, i, hold;
printf("Data items in original order\n");
/*output original array*/
for(i =0; i < SIZE; i++){
printf("%4d", a[i]);
}
/*bubble sort*/
for(pass = 1; pass < SIZE; pass++){
for(i = 0; i < SIZE - 1; i++){
if(a[i] > a[i+1]){
hold = a[i];
a[i] = a[i + 1];
a[i + 1] = hold;
}
}
}
printf("\nData items in ascending order\n");
for(i = 0; i < SIZE; i++){
printf("%4d", a[i]);
}
printf("\n");
system("pause");
return 0;
}
Subscribe to:
Posts (Atom)
