Run this code in your IDE using breakpoints and monitor every variable as you progress.
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
enum colors { BLUE, WHITE, RED, GREY };
enum models { TOYOTA, FORD, CHEVY, NISSAN };
typedef int zoltan[4][4];
void print(const zoltan cars);
void print(const int favoriteColors[], colors = BLUE);
void initialize(int cars[][4]);
void initialize(int favoriteColors[]);
void printByRow(const zoltan cars, int row);
void printByCol(const zoltan cars, int col);
int sumByRow(const zoltan cars, int row);
int sumByCol(const zoltan cars, int col);
void parallelArrayPrint(string names[], int items[], float prices[], char codes[],int size);
void cStringExercise();
int main() {
int favoriteColors[4];
zoltan cars;
initialize(cars);
initialize(favoriteColors);
printByRow(cars, models::CHEVY);
printByCol(cars, colors::RED);
cout << sumByRow(cars, models::CHEVY) << endl;
cout << sumByCol(cars, colors::RED) << endl;
cStringExercise();
string names[] = { "Joe","Bob","Sam","Jane" };
int items[] = {3,5,2,7};
float prices[] = {23,34,45,56};
char codes[] = {'A','B','C','D'};
parallelArrayPrint(names,items,prices,codes,4);
return 0;
}
void initialize(int favoriteColors[]) {
int i = 0;
for (colors c = BLUE; c <= colors::GREY; c = static_cast<colors>(c + 1)) {
favoriteColors[c] = i++;
}
}
void initialize(int cars[][4]) {
for (int row = 0; row <= colors::GREY; row++)
for (int col = 0; col <= models::NISSAN; col++)
cars[row][col] = row*col;
}
void print(const int favoriteColors[], colors c) {
for (; c <= colors::GREY; c = static_cast<colors>(c + 1)) {
cout << setw(3) << favoriteColors[c];
}
cout << endl;
}
void print(const zoltan cars) {
for (int row = 0; row <= colors::GREY; row++) {
for (int col = 0; col <= models::NISSAN; col++)
cout << setw(3) << cars[row][col];
cout << endl;
}
}
void printByRow(const zoltan cars, int row) {
for (int col = 0; col <= models::NISSAN; col++)
cout << setw(3) << cars[row][col];
cout << endl;
}
void printByCol(const zoltan cars, int col) {
for (int row = 0; row <= models::NISSAN; row++)
cout << setw(3) << cars[row][col];
cout << endl;
}
int sumByRow(const zoltan cars, int row) {
int total = 0;
for (int col = 0; col <= models::NISSAN; col++)
total += cars[row][col];
return total;
}
int sumByCol(const zoltan cars, int col) {
int total = 0;
for (int row = 0; row <= models::NISSAN; row++)
total += cars[row][col];
return total;
}
void cStringExercise() {
string names[] = { "Joe","Bob","Sam","Jane" }; //just to see that a one dimensional string array is a two dimensional char array
char names2[4][4];
cin >> names2[0];
cin >> names2[1];
cin >> names2[2];
cin >> names2[3];
cout << names2[0] << endl;
cin.ignore(100,'\n');
//what's wrong with this?
cin.get(names2[0], 4);
cin.ignore(100, '\n');
cin.get(names2[1], 4);
cin.ignore(100, '\n');
cin.get(names2[2], 4);
cin.ignore(100, '\n');
cin.get(names2[3], 4);
cin.ignore(100, '\n');
cout << names2[0] << endl;
}
void parallelArrayPrint(string names[],int items[], float prices[], char codes[],int size) {
cout << showpoint << fixed << setprecision(2);
for (int index = 0; index < size; index++) {
cout <<setw(6)<< names[index] << ", you have purchased " << items[index] << " items that costs $" <<setw(7)<< prices[index] * items[index] << " item code: " << codes[index] << endl;
}
}