Friday, November 18, 2016

Array, Enum, Function, Typedef



In this example, we have everything we learned in chapters 7 and 8. 

You should have the breakpoints set as shown below and run the code in debugger to visualize how the array is filled and how it is processed one element at a time.

 

#include<iostream>
#include<time.h>
#include<string>
#include<iomanip>

using namespace std;

namespace zoltan{
    enum models{ TOYOTA, FORD, CHEVY, MAZDA, PORSCHE };
    enum colors{ RED, YELLOW, BLUE, BLACK };
    typedef int dealerships[PORSCHE + 1][BLACK + 1];
}
using namespace zoltan;

void fillArray(int [][BLACK+1]);
int totalColor(dealerships,colors);
int totalModel(dealerships,models);
string decodeColor(colors);
string decodeModel(models);

int main(){
    cout << "Hello Enum based Arrays" << endl;
    int dealership[5][4] = { { 2, 3, 4, 5  },
                           { 3, 4, 5, 6  },
                           { 5, 6, 7, 8  },
                           { 7, 8, 9, 10 },
                           { 1, 2, 3, 4  } };
    zoltan::dealerships richland;
    fillArray(richland);
    int color=totalColor(dealership,BLUE);
    int model=totalModel(richland,PORSCHE);
  
    cout << setw(11) << " ";
    for (colors col = RED; col <= BLACK; col = static_cast <zoltan::colors>(col + 1))
        cout <<setw(8)<< decodeColor(col);
    cout << endl;

    for (models row = TOYOTA; row <= PORSCHE; row = static_cast <models>(row + 1)){
        cout << setw(10) << decodeModel(row) << ":";
        for (colors col = RED; col <= BLACK; col = static_cast <zoltan::colors>(col + 1)){
            cout << setw(8)<<richland[row][col];
        }
        cout << endl;
    }

    return 0;
}
void fillArray(dealerships passedArray){
    srand(time(NULL));
    for (models row = TOYOTA; row <= PORSCHE; row = static_cast <models>(row + 1))
    for (colors col = RED; col <= BLACK; col = static_cast <zoltan::colors>(col + 1))   //you can use the namespace or not
        passedArray[row][col] = rand() % 50;
}
int totalColor(dealerships d, colors c){  //Process by column
    int total = 0;
    for (models row = TOYOTA; row <= PORSCHE; row = static_cast <models>(row + 1))
        total+=d[row][c];
    return total;
}
int totalModel(dealerships d, models m){  //process by row
    int total = 0;
    for (zoltan::colors col = RED; col <= BLACK; col = static_cast <zoltan::colors>(col + 1))
        total+=d[m][col];
    return total;
}

string decodeColor(colors c){
    switch (c){
        case RED: return "red";
        case YELLOW: return "yellow";
        case BLUE: return "blue";
        case BLACK: return "black";
    };
}

string decodeModel(models m){
    switch (m){
    case TOYOTA: return "toyota";
    case FORD: return "ford";
    case CHEVY: return "chevy";
    case MAZDA: return "mazda";
    case PORSCHE: return "porsche";
    }
}

Wednesday, November 9, 2016

Place value of Integer digits

The process of program development starts with understanding the problem.
Problem: Find the highest place value exponent in a Base-10 integer.
Pre-requisite: i.e. 9,448,320,743 You may say, what does the 9 represent? (billion) what does the 4 next to the 9 represent? (hundred million) what number is in the hundred thousand's place?(3)
Base-10 123456 number is written as 1(100,000)+2(10000)+3(1000)+4(100)+5(10)+6(1) and that is 1(10^5)+2(10^4)+3(10^3)+4(10^2)+5(10^1)+6(10^0)
Analysis: In order to find the largest place value, we need to find the largest exponent of base 10.
We can find the highest exponent if we divide the integer by 10^exp where exp = {0,1,2,3,...}

i.e. 46578


num exp 10exp num/10exp
123456 0 1 123456
1 10 12345
2 100 1234
3 1000 123
4 10000 12
5 100000 1
6 1000000 0
7 10000000 0
As you can see, we can find the highest exponent as soon as the result of the division is between 1 and 9.  So, we could solve this problem by iterating in a loop with an increment exp until the division result is between 1 and 9.

Design: Design involves creating a pseudo code or flow chart in order to develop the algorithm.

 start
   output "Welcome"
   output "Enter a number"
   input as num
   if input not a number then
       output  "Error: Better luck next time."
       return 1
   endif
   call findExp use num
   output "The exponent in: "
   output "Thanks for using my program."
stop

start findExp reference myNum
   call abs using myNum as myNum
   declare exp = 0
   while myNum / 10^exp > 10 then
      exp=exp+1
   endhile
   myNum = exp
return


Code: Now, that we have the basic idea, you can pick the language and code the problem.

#include<iostream>
#include<cmath>

using namespace std;
void findExp(int& num);

int main(){
    int num,exp;
    cout << "Welcome" << endl;
    cout << "Enter a number: ";
    cin >> num;
    if (!cin){              //validating user input
        cout << "Error: Better luck next time." << endl;
        return 1;
    }

    findExp(num);          //function call
    cout << "The exponent in: " << num << endl;
    cout << "Thanks for using my program." << endl;

    return 0;
}
void findExp(int& myNum){
    myNum = abs(myNum);
    int exp = 0;

    while (myNum / pow(10, exp) > 10){
        exp++;
    }
    myNum= exp;      //setting the new value to the referenced value
}