Showing posts with label Chapter 6: C Arrays. Show all posts
Showing posts with label Chapter 6: C Arrays. Show all posts

Friday, March 16, 2012

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
The program below will read all the information for last month's sales and summarize the total sales by salespeople by product and then print the result in a tabular format.
/*
   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. ]
/*
   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&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#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:
  • 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;
}

Sales Comissions

A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week.
Program below determines how many of the salespeople earned salaries in each of the following ranges.
  • $200-299
  • $300-399
  • $400-499
  • $500-599
  • $600-699
  • $700-799
  • $800-899
  • $900-999
  • $1000 and over
[ Note: The salespeoples' gross sales is stored in gross[], there is no input needed. ]
/*
   title: salesComission
   coder: aeriqusyairi
   date: Jan25 2012
*/
#include<stdio.h>
#define SALESPERSON 20
#define RANGE 9
  
void calculateSalary( int b[] );
void calculateRange( const int b[], int a[] );
 
int main(){
   int gross[ SALESPERSON ] = { 3000, 4000, 7000, 8000, 10000, 30000, 500, 6000, 4000, 7000,
                                5000, 1500, 400, 15000, 2000, 50000, 3000, 40000, 5000, 10000 };
   int salaryRange[ RANGE ] = { 0 };
   
   int i, j, k;
   
   calculateSalary( gross );
   
   calculateRange( gross, salaryRange );
   
   for(i = 0, j = 200, k = 299 ; i < RANGE; i++, j += 100, k += 100 ){
      if(k < 1000)
         printf("%d salesperson earn salary between %d and %d.\n", salaryRange[ i ], j, k );
      else
         printf("%d salesperson earn salary of %d and over.\n", salaryRange[ i ], j );      
   }
   
   system("pause");
   return 0;
}

void calculateSalary( int b[] ){
   int i;
   
   for(i = 0; i < SALESPERSON; i++){
      b[ i ] *= 0.09;
      b[ i ] += 200;      
   }     
   
}

void calculateRange( const int b[], int a[] ){
   int i;
     
   for(i = 0; i < SALESPERSON; i++ ){
      if( b[i] >= 200 && b[ i ] < 300 )
         a[ 0 ]++;
      else if( b[i] >= 300 && b[ i ] < 400 )
         a[ 1 ]++;
      else if( b[i] >= 400 && b[ i ] < 500 )
         a[ 2 ]++;
      else if( b[i] >= 500 && b[ i ] < 600 )
         a[ 3 ]++;
      else if( b[i] >= 600 && b[ i ] < 700 )
         a[ 4 ]++;
      else if( b[i] >= 700 && b[ i ] < 800 )
         a[ 5 ]++;
      else if( b[i] >= 800 && b[ i ] < 900 )
         a[ 6 ]++;
      else if( b[i] >= 900 && b[ i ] < 1000 )
         a[ 7 ]++;
      else if( b[i] >= 1000 )
         a[ 8 ]++;       
   }
        
}