below is c++ program for decimal to hexadecimal conversion!!
// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
string d2h(int n) {
string s, t;
int i = 0;
while (n >= 1) {
t[i] = n % 16;
switch (t[i]) {
case 10:
t[i] = 'A';
break;
case 11:
t[i] = 'B';
break;
case 12:
t[i] = 'C';
break;
case 13:
t[i] = 'D';
break;
case 14:
t[i] = 'E';
break;
case 15:
t[i] = 'F';
break;
default:
break;
}
s[i] = t[i];
n /= 16;
i++;
}
return s;
}
int main() {
int n;
cin >> n;
cout << d2h(n);
return 0;
}
I am getting nothing as output!!
PS C:\Users\anmol\Desktop\c++projectwork> g++ .\dec2hexadec.cpp
PS C:\Users\anmol\Desktop\c++projectwork> ./a
479
PS C:\Users\anmol\Desktop\c++projectwork>
What should i change??
You are not allocating space in s or t for memory. Use s.push_back() instead of s[i].
You'll probably need to reverse the string as well.
t[i] = n % 16; This does not assign the character '0', but rather the number 0. Why don't you just create 16 cases in your switch statement? Or use the ASCII quirk '0' + (n % 16).
// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
string d2h(int n) {
string s, t;
string Sign;
int i = 0;
// n is equal to 0? <0 or >0?
if (n == 0){
return "0";
}
if (n < 0){
Sign = "-";
n *= -1;
}
while (n ) {
t.push_back(n % 16) ;
switch (t[i]) {
case 0:
t[i] = '0';
break;
case 1:
t[i] = '1';
break;
case 2:
t[i] = '2';
break;
case 3:
t[i] = '3';
break;
case 4:
t[i] = '4';
break;
case 5:
t[i] = '5';
break;
case 6:
t[i] = '6';
break;
case 7:
t[i] = '7';
break;
case 8:
t[i] = '8';
break;
case 9:
t[i] = '9';
break;
case 10:
t[i] = 'A';
break;
case 11:
t[i] = 'B';
break;
case 12:
t[i] = 'C';
break;
case 13:
t[i] = 'D';
break;
case 14:
t[i] = 'E';
break;
case 15:
t[i] = 'F';
break;
default:
break;
}
s.push_back(t[i]);
n /= 16;
i++;
}
reverse(s.begin(), s.end());
return Sign+s;
}
int main() {
int n;
cin >> n;
cout << d2h(n);
return 0;
}
I am making a console application that will print all the paths. But I am having a hard time thinking on how to display all paths from source to destination.
Here is my code:
#include<iostream>
using namespace std;
int arr[8][8] = {{50,30,45,120,0,7,0,0},{30,45,28,4,70,0,0,0},
{50,20,0,38,0,0,0,0},{0,4,30,0,52,0,3,0},{0,75,0,27,0,2,0,3},
{70,0,0,0,2,0,2,0},{0,80,0,73,0,2,0,0},{60,0,90,0,30,0,0,0}};
char vertex[8] = {'A','B','C','D','E','F','G','H'};
void displayPath()
{
system("cls");
int start, end;
char from, to;
cout << "From vertex: ";
cin >> from;
cout << "To: ";
cin >> to;
switch(from)
{
case 'a':case 'A': start = 0; break;
case 'b':case 'B': start = 1; break;
case 'c':case 'C': start = 2; break;
case 'd':case 'D': start = 3; break;
case 'e':case 'E': start = 4; break;
case 'f':case 'F': start = 5; break;
case 'g':case 'G': start = 6; break;
case 'h':case 'H': start = 7; break;
}
switch(to)
{
case 'a':case 'A': end = 0; break;
case 'b':case 'B': end = 1; break;
case 'c':case 'C': end = 2; break;
case 'd':case 'D': end = 3; break;
case 'e':case 'E': end = 4; break;
case 'f':case 'F': end = 5; break;
case 'g':case 'G': end = 6; break;
case 'h':case 'H': end = 7; break;
}
int temp = 0;
int current = start;
if(arr[start][end] > 0)
{
cout << vertex[start] << "->" << vertex[end];
}
else
cout << "No path";
}
void computeDistance()
{
system("cls");
int start,end;
char from, to;
cout << "From vertex: ";
cin >> from;
cout << "To: ";
cin >> to;
switch(from)
{
case 'a':case 'A': start = 0; break;
case 'b':case 'B': start = 1; break;
case 'c':case 'C': start = 2; break;
case 'd':case 'D': start = 3; break;
case 'e':case 'E': start = 4; break;
case 'f':case 'F': start = 5; break;
case 'g':case 'G': start = 6; break;
case 'h':case 'H': start = 7; break;
}
switch(to)
{
case 'a':case 'A': end = 0; break;
case 'b':case 'B': end = 1; break;
case 'c':case 'C': end = 2; break;
case 'd':case 'D': end = 3; break;
case 'e':case 'E': end = 4; break;
case 'f':case 'F': end = 5; break;
case 'g':case 'G': end = 6; break;
case 'h':case 'H': end = 7; break;
}
if(arr[start][end] > 0)
{
cout << arr[start][end] << " meters" << endl;
}
else
cout << "No path";
}
int main()
{
int choice;
cout << "Menu\n\n[1] Display Path\n[2] Compute Distance\n\nChoice: ";
cin >> choice;
switch(choice)
{
case 1: displayPath(); break;
case 2: computeDistance(); break;
}
system("pause");
return 0;
}
This program only gives the source to destination, not all the vertices passed through. This should be the sample output:
From: A
To: F
A -> B -> D -> F
Also, it should follow the concept of shortest path. And will give the total distance. I hope you could help me with this one. Thank you so much in advance.
First of all you need to implement some algorithm which will find shortest path.
Basically there are couple choices:
Dijkstra's algorithm
A* search algorithm
I don't see you have implemented anything which do such search.
The number of various paths between 2 nodes in graph is exponential. That means that there can be a lot of them.
I am not sure if your graph is always built of 8 nodes. In the following solution I have assumed that it is.
Having such a small data you can use very slow and not efficient solutions.
Example: Lets generate every possible permutation of nodes and check all which start from source and check them until they reach destination.
Getting shortest path after checking all paths is pretty easy - just count total distance of each path while checking them.
Some code:
#include <bits/stdc++.h>
using namespace std;
int Graph[8][8];
map <int, bool> used; // to not print same path multiple times
int valid_path(int src, int dest, vector<int>& path)
{
int pathHash=0;
if (path[0]!=src) return -1;
for (size_t i=1; i<path.size(); i++)
{
pathHash= pathHash*10+path[i]; // such solution works only because of small size of data
if (Graph[path[i-1]][path[i]]==0) return -1; // there is no edge between these nodes
if (path[i]==dest) break;
}
return pathHash;
}
void print_path (int dest, vector<int>& path)
{
for (size_t i=0; i<path.size(); i++)
{
cout << path[i] << " ";
if (path[i]==dest) break;
cout << " --> ";
}
}
void generate_paths(int src, int dest)
{
vector<int> perm {0, 1, 2, 3, 4, 5, 6, 7};
do
{
int pathHash= valid_path(src, dest, perm);
if (pathHash!=-1 && used[pathHash]==false)
{
used[pathHash]=true;
print_path(dest, perm);
cout << "\n";
}
} while (next_permutation(perm.begin(), perm.end()));
}
int main () {
for (size_t i=0; i<8; i++)
{
for (size_t j=0; j<8; j++)
{
cin >> Graph[i][j];
}
}
generate_paths(0, 7);
}
/* example input
0 1 0 1 0 1 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
*/
I ran this code and entered the values "P", "Q" and "101" and it seems to start hanging. I would like to know what kind of coding and thinking do some of you use to help optimize your code to make it run as quickly as possible and the changes you would make to my code. I believe the main problem lies in the "toSequencePos" sub routine as it takes too long for it to be carried out.
Thanks in advance.
[Edit] This code is in answer to the BIO 2011 which can be found here (Question 1 A)
#include <iostream>
using namespace std;
int toNumber(char letter1)
{
long long int position;
switch (letter1)
{
case 'A': position = 1; break;
case 'B': position = 2; break;
case 'C': position = 3; break;
case 'D': position = 4; break;
case 'E': position = 5; break;
case 'F': position = 6; break;
case 'G': position = 7; break;
case 'H': position = 8; break;
case 'I': position = 9; break;
case 'J': position = 10; break;
case 'K': position = 11; break;
case 'L': position = 12; break;
case 'M': position = 13; break;
case 'N': position = 14; break;
case 'O': position = 15; break;
case 'P': position = 16; break;
case 'Q': position = 17; break;
case 'R': position = 18; break;
case 'S': position = 19; break;
case 'T': position = 20; break;
case 'U': position = 21; break;
case 'V': position = 22; break;
case 'W': position = 23; break;
case 'X': position = 24; break;
case 'Y': position = 25; break;
case 'Z': position = 26; break;
default: position = 0; break;
}
return position;
}
int toLetter(long long int finalPosition)
{
char letter;
switch (finalPosition)
{
case 1: letter = 'A'; break;
case 2: letter = 'B'; break;
case 3: letter = 'C'; break;
case 4: letter = 'D'; break;
case 5: letter = 'E'; break;
case 6: letter = 'F'; break;
case 7: letter = 'G'; break;
case 8: letter = 'H'; break;
case 9: letter = 'I'; break;
case 10: letter = 'J'; break;
case 11: letter = 'K'; break;
case 12: letter = 'L'; break;
case 13: letter = 'M'; break;
case 14: letter = 'N'; break;
case 15: letter = 'O'; break;
case 16: letter = 'P'; break;
case 17: letter = 'Q'; break;
case 18: letter = 'R'; break;
case 19: letter = 'S'; break;
case 20: letter = 'T'; break;
case 21: letter = 'U'; break;
case 22: letter = 'V'; break;
case 23: letter = 'W'; break;
case 24: letter = 'X'; break;
case 25: letter = 'Y'; break;
case 26: letter = 'Z'; break;
}
return letter;
}
int toSequencePos(long long int n1, long long int letterPos1, long long int letterPos2)
{
long long int finalPosition = 0;
for(long long int x = 1; x <= n1 - 2; x++)
{
finalPosition = letterPos1 + letterPos2;
letterPos1 = letterPos2;
letterPos2 = finalPosition;
}
while (finalPosition > 26)
{
finalPosition = finalPosition - 26;
}
return finalPosition;
}
int main()
{
char letter1;
char letter2;
long long int letterPos1 = 0;
long long int letterPos2 = 0;
long long int sequenceLetterPos = 0;
long long int n1;
char finalAnswer;
cout << "Please enter your first letter: ";
cin >> letter1;
letterPos1 = toNumber(letter1);
cout << "Please enter your second letter: ";
cin >> letter2;
letterPos2 = toNumber(letter2);
cout << "Please enter the position number that you wish to find in this sequence";
cin >> n1;
sequenceLetterPos = toSequencePos(n1, letterPos1, letterPos2);
finalAnswer = toLetter(sequenceLetterPos);
cout << "The letter in position " << n1 << " is " << finalAnswer << endl;
}
Here are some optimisation points
int toNumber(char letter1)
{
long long int position = lettter1 - 'A' + 1;
return position;
}
int toLetter(long long int finalPosition)
{
char letter = 'A' + finalPosition - 1;
return letter;
}
in toSequencePos function
// instead of following thing
while (finalPosition > 26)
{
finalPosition = finalPosition - 26;
}
// use following
finalPosition = finalPosition % 26;
UPDATE::
Above solution will not work if the system's character encoding scheme is somewhat non-sequential. i.e. EBCDIC, BCD (character encoding) etc.
in that case, you have to maintain one hash-map & one array. Hash-map contains char as key & int as values. Arrays of char will suffice for this matter. Following given such a encoding independent implementation
#include <stdio.h>
#include <string.h>
#define NUMBER_OF_CHARACTER 26
char int2charMapping[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
//
// suppose we spare 8 bit for character representation
//
unsigned int char2IntMapping[256];
void initiateTheChar2IntMapping()
{
memset(char2IntMapping, 0, sizeof(char2IntMapping));
char2IntMapping['A'] = 1;
char2IntMapping['B'] = 2;
char2IntMapping['C'] = 3;
char2IntMapping['D'] = 4;
char2IntMapping['E'] = 5;
char2IntMapping['F'] = 6;
char2IntMapping['G'] = 7;
char2IntMapping['H'] = 8;
char2IntMapping['I'] = 9;
char2IntMapping['J'] = 10;
char2IntMapping['K'] = 11;
char2IntMapping['L'] = 12;
char2IntMapping['M'] = 13;
char2IntMapping['N'] = 14;
char2IntMapping['O'] = 15;
char2IntMapping['P'] = 16;
char2IntMapping['Q'] = 17;
char2IntMapping['R'] = 18;
char2IntMapping['S'] = 19;
char2IntMapping['T'] = 20;
char2IntMapping['U'] = 21;
char2IntMapping['V'] = 22;
char2IntMapping['W'] = 23;
char2IntMapping['X'] = 24;
char2IntMapping['Y'] = 25;
char2IntMapping['Z'] = 26;
}
char toLetter(long long int index)
{
if(index > 0 && index < 27)
{
return int2charMapping[index-1];
}
return '\0';
}
int toNumber(char letter)
{
return char2IntMapping[letter];
}
int main()
{
//initiate the mapping
initiateTheChar2IntMapping();
printf("--> %c\n", toLetter(26));
printf("~~> %d\n", toNumber('A'));
return 0;
}
you're essentially mapping letters to numbers of your own choosing. you could use a map data structure to do this, but much more efficiently. HTH
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want my function to read one char[] and split it into 2 arrays. Both arrays would be one big number, first array is for that number int value and second one is for double values. I must use 2 arrays because this number can be binary, decimal, octal or hex.
void Read(int place[], int &size, int &type, int placedot[], int &sizedot, int &typedot)
{
char reader[limit];
cin >> reader;
size = 1;
while (reader[size] == '.' || size != strlen(reader))
{
size++;
cout << "LOL";
}
cin >> type;
cout << size;
typedot = type;
for (int i = 0; i<size; i++)
{
switch (reader[i])
{
case '0': place[i] = 0; break;
case '1': place[i] = 1; break;
case '2': place[i] = 2; break;
case '3': place[i] = 3; break;
case '4': place[i] = 4; break;
case '5': place[i] = 5; break;
case '6': place[i] = 6; break;
case '7': place[i] = 7; break;
case '8': place[i] = 8; break;
case '9': place[i] = 9; break;
case 'A': place[i] = 10; break;
case 'B': place[i] = 11; break;
case 'C': place[i] = 12; break;
case 'D': place[i] = 13; break;
case 'E': place[i] = 14; break;
case 'F': place[i] = 15; break;
}
}
}
#include <iostream>
#include <string>
int main()
{
std::string str = "3423432432.32445654576654578978905";
std::string strInt;
std::string strDec;
int i = 0;
while('.' != str[i])
strInt += str[i++];
for(++i; i < str.length(); i++)
strDec += str[i];
std::cout << strInt << std::endl << strDec << std::endl;
// now you have the integer part and the decimal part as strings so convert each of them to int
std::cout << std::endl;
return 0;
}
I looked through the other questions that also contained these errors. I was not able to find a solution from those questions. Some answers said certain functions weren't implemented but my functions are. I only get the title error when I try to run the program from the VS2012 command line. When I run the program from the IDE, I have no errors and everything is fine.
Visual Studio 2012 Project
TTH.zip
Imgur
Capture_CMD.png
useTTH.cpp
#include <iostream>
#include <string>
#include "tth.h"
using namespace std;
int main()
{
string message;
string choice;
do
{
cout << "Please enter a message: ";
getline(cin, message);
tth tthObject;
tthObject.getMessage(message);
tthObject.encryptMessageOnlyLetters();
cout << endl;
cout << "Encrypt another message? (Yes/No)" << endl;
cout << "> ";
getline(cin, choice);
cout << endl;
}
while(choice.substr(0, 1) == "Y" || choice.substr(0, 1) == "y");
system("PAUSE");
return 0;
}
tth.h
#include <string>
#include <vector>
using namespace std;
class tth
{
public:
tth();
void getMessage(string messageP);
void setMessageOnlyLettersNumberOf16Blocks();
void encryptMessageOnlyLetters();
void messageOnlyLettersToFourByFourLetters();
void matchLetterToNumber();
void calculateAndShowRunningTotalFourByFourNumbers();
void shiftRowsAccordingly();
void calculateAndShowRunningTotalTemp();
void matchNumberToLetter();
void showFourLetterHashNumbers();
private:
string message;
vector<char> messageOnlyLetters;
int runningTotal[4];
char fourLetterHash[4];
int messageLength;
int messageOnlyLettersNumberOf16Blocks;
char fourByFourLetters[4][4];
int fourByFourNumbers[4][4];
int temp[4][4];
};
tth.cpp
#include <iostream>
#include <string>
#include <cctype>
#include "tth.h"
//--------------------------------------------------------------------------------------------------------------
// tth()
//--------------------------------------------------------------------------------------------------------------
// Constructor for the tth class.
// Initializes the elements of the private integer array 'runningTotal' to 0.
// Initializes the elements of the private char array 'fourLetterHash' to 'A'.
//--------------------------------------------------------------------------------------------------------------
tth::tth()
{
for(int i = 0; i < 4; i++)
{
runningTotal[i] = 0;
fourLetterHash[i] = 'A';
}
}
//--------------------------------------------------------------------------------------------------------------
// getMessage(string messageP)
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the value of the private string variable 'message' to the value of the string 'messageP' parameter.
// Gives the private char vector 'messageOnlyLetters' only letter elements of the 'message' variable.
//--------------------------------------------------------------------------------------------------------------
void tth::getMessage(string messageP)
{
message = messageP;
messageLength = message.length();
for(int i = 0; i < messageLength; i++)
{
if(isalpha(message[i]))
{
messageOnlyLetters.push_back(message[i]);
}
}
}
//--------------------------------------------------------------------------------------------------------------
// setMessageOnlyLettersNumberOf16Blocks()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Pads the messageOnlyLetters vector with a number 'A' elements equal to modulo 16 of the size of the vector.
// Sets the private int variable 'messageOnlyLettersNumberOf16Blocks' to the quotient of the vector size and 16.
//--------------------------------------------------------------------------------------------------------------
void tth::setMessageOnlyLettersNumberOf16Blocks()
{
int messageOnlyLettersModulo = messageOnlyLetters.size() % 16;
while(messageOnlyLettersModulo != 0)
{
messageOnlyLetters.push_back('A');
messageOnlyLettersModulo = messageOnlyLetters.size() % 16;
}
messageOnlyLettersNumberOf16Blocks = messageOnlyLetters.size() / 16;
}
//--------------------------------------------------------------------------------------------------------------
// encryptMessageOnlyLetters()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Executes the 'setMessageOnlyLettersNumberOf16Blocks()' once.
// Executes other functions of the tth class a number of times equal to 'messageOnlyLettersNumberOf16Blocks'.
//--------------------------------------------------------------------------------------------------------------
void tth::encryptMessageOnlyLetters()
{
setMessageOnlyLettersNumberOf16Blocks();
for (int i = 0; i < messageOnlyLettersNumberOf16Blocks; i++)
{
messageOnlyLettersToFourByFourLetters();
matchLetterToNumber();
calculateAndShowRunningTotalFourByFourNumbers();
cout << endl;
shiftRowsAccordingly();
calculateAndShowRunningTotalTemp();
cout << endl;
matchNumberToLetter();
showFourLetterHashNumbers();
cout << endl;
}
}
//--------------------------------------------------------------------------------------------------------------
// messageOnlyLettersToFourByFourLetters()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private char 2D array 'fourByFourLetters' to the elements of 'messageOnlyLetters'.
// Reduces the size of a non-empty messageOnlyLetters by 16 elements.
//--------------------------------------------------------------------------------------------------------------
void tth::messageOnlyLettersToFourByFourLetters()
{
int count = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
fourByFourLetters[i][j] = messageOnlyLetters[count];
count++;
}
}
if(!(messageOnlyLetters.empty()))
{
messageOnlyLetters.erase(messageOnlyLetters.begin(), messageOnlyLetters.begin() + 16);
}
}
//--------------------------------------------------------------------------------------------------------------
// matchLetterToNumber()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private int 2D array 'fourByFourNumbers' to a number 0 - 25.
// The number is determined by the ASCII value of a 'fourByFourLetters' element.
//--------------------------------------------------------------------------------------------------------------
void tth::matchLetterToNumber()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
switch(static_cast<int>(fourByFourLetters[i][j]))
{
case 65:
case 97:
fourByFourNumbers[i][j] = 0;
break;
case 66:
case 98:
fourByFourNumbers[i][j] = 1;
break;
case 67:
case 99:
fourByFourNumbers[i][j] = 2;
break;
case 68:
case 100:
fourByFourNumbers[i][j] = 3;
break;
case 69:
case 101:
fourByFourNumbers[i][j] = 4;
break;
case 70:
case 102:
fourByFourNumbers[i][j] = 5;
break;
case 71:
case 103:
fourByFourNumbers[i][j] = 6;
break;
case 72:
case 104:
fourByFourNumbers[i][j] = 7;
break;
case 73:
case 105:
fourByFourNumbers[i][j] = 8;
break;
case 74:
case 106:
fourByFourNumbers[i][j] = 9;
break;
case 75:
case 107:
fourByFourNumbers[i][j] = 10;
break;
case 76:
case 108:
fourByFourNumbers[i][j] = 11;
break;
case 77:
case 109:
fourByFourNumbers[i][j] = 12;
break;
case 78:
case 110:
fourByFourNumbers[i][j] = 13;
break;
case 79:
case 111:
fourByFourNumbers[i][j] = 14;
break;
case 80:
case 112:
fourByFourNumbers[i][j] = 15;
break;
case 81:
case 113:
fourByFourNumbers[i][j] = 16;
break;
case 82:
case 114:
fourByFourNumbers[i][j] = 17;
break;
case 83:
case 115:
fourByFourNumbers[i][j] = 18;
break;
case 84:
case 116:
fourByFourNumbers[i][j] = 19;
break;
case 85:
case 117:
fourByFourNumbers[i][j] = 20;
break;
case 86:
case 118:
fourByFourNumbers[i][j] = 21;
break;
case 87:
case 119:
fourByFourNumbers[i][j] = 22;
break;
case 88:
case 120:
fourByFourNumbers[i][j] = 23;
break;
case 89:
case 121:
fourByFourNumbers[i][j] = 24;
break;
case 90:
case 122:
fourByFourNumbers[i][j] = 25;
break;
default:
cout << "Hmmmm";
}
}
}
}
//--------------------------------------------------------------------------------------------------------------
// calculateAndShowRunningTotalFourByFourNumbers()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Calculates and sets the elements of 'runningTotal' using an accumulator with 'fourByFourNumbers'.
// Displays a command line message with each element of 'runningTotal'.
//--------------------------------------------------------------------------------------------------------------
void tth::calculateAndShowRunningTotalFourByFourNumbers()
{
cout << endl;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
runningTotal[i] = runningTotal[i] + fourByFourNumbers[j][i];
}
runningTotal[i] = runningTotal[i] % 26;
cout << runningTotal[i] << " ";
}
}
//--------------------------------------------------------------------------------------------------------------
// shiftRowsAccordingly()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private int 2D array 'temp' to a rearranged version of 'fourByFourNumbers'.
// Elements of rows 1 - 3 are shifted in a circular pattern.
// Row 1 elements are shifted to the left by 1 index. Row 2 elements are shifted to the left by 2 indicies.
// Row 3 elements are shifted to the left by 3 indicies. Row 4 elements are swapped: 1 <> 4 and 2 <> 3.
//--------------------------------------------------------------------------------------------------------------
void tth::shiftRowsAccordingly()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(i == 0)
{
if(j == 3)
{
temp[i][j] = fourByFourNumbers[i][j - 3];
}
else
{
temp[i][j] = fourByFourNumbers[i][j + 1];
}
}
else if(i == 1)
{
if(j == 2)
{
temp[i][j] = fourByFourNumbers[i][j - 2];
}
else if(j == 3)
{
temp[i][j] = fourByFourNumbers[i][j - 2];
}
else
{
temp[i][j] = fourByFourNumbers[i][j + 2];
}
}
else if(i == 2)
{
if(j == 1)
{
temp[i][j] = fourByFourNumbers[i][j - 1];
}
else if(j == 2)
{
temp[i][j] = fourByFourNumbers[i][j - 1];
}
else if(j == 3)
{
temp[i][j] = fourByFourNumbers[i][j - 1];
}
else
{
temp[i][j] = fourByFourNumbers[i][j + 3];
}
}
else
{
if(j == 1)
{
temp[i][j] = fourByFourNumbers[i][j + 1];
}
else if(j == 2)
{
temp[i][j] = fourByFourNumbers[i][j - 1];
}
else if(j == 3)
{
temp[i][j] = fourByFourNumbers[i][j - 3];
}
else
{
temp[i][j] = fourByFourNumbers[i][j + 3];
}
}
}
}
}
//--------------------------------------------------------------------------------------------------------------
// calculateAndShowRunningTotalTemp()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Calculates and sets the elements of 'runningTotal' using an accumulator with 'temp'.
// Displays a command line message with each element of 'runningTotal'.
//--------------------------------------------------------------------------------------------------------------
void tth::calculateAndShowRunningTotalTemp()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
runningTotal[i] = runningTotal[i] + temp[j][i];
}
runningTotal[i] = runningTotal[i] % 26;
cout << runningTotal[i] << " ";
}
}
//--------------------------------------------------------------------------------------------------------------
// matchNumberToLetter()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private char 2D array 'fourLetterHash' to a letter A - Z.
// The letter is determined by the value of a 'runningTotal' element.
//--------------------------------------------------------------------------------------------------------------
void tth::matchNumberToLetter()
{
for(int i = 0; i < 4; i++)
{
switch(runningTotal[i])
{
case 0:
fourLetterHash[i] = 'A';
break;
case 1:
fourLetterHash[i] = 'B';
break;
case 2:
fourLetterHash[i] = 'C';
break;
case 3:
fourLetterHash[i] = 'D';
break;
case 4:
fourLetterHash[i] = 'E';
break;
case 5:
fourLetterHash[i] = 'F';
break;
case 6:
fourLetterHash[i] = 'G';
break;
case 7:
fourLetterHash[i] = 'H';
break;
case 8:
fourLetterHash[i] = 'I';
break;
case 9:
fourLetterHash[i] = 'J';
break;
case 10:
fourLetterHash[i] = 'K';
break;
case 11:
fourLetterHash[i] = 'L';
break;
case 12:
fourLetterHash[i] = 'M';
break;
case 13:
fourLetterHash[i] = 'N';
break;
case 14:
fourLetterHash[i] = 'O';
break;
case 15:
fourLetterHash[i] = 'P';
break;
case 16:
fourLetterHash[i] = 'Q';
break;
case 17:
fourLetterHash[i] = 'R';
break;
case 18:
fourLetterHash[i] = 'S';
break;
case 19:
fourLetterHash[i] = 'T';
break;
case 20:
fourLetterHash[i] = 'U';
break;
case 21:
fourLetterHash[i] = 'V';
break;
case 22:
fourLetterHash[i] = 'W';
break;
case 23:
fourLetterHash[i] = 'X';
break;
case 24:
fourLetterHash[i] = 'Y';
break;
case 25:
fourLetterHash[i] = 'Z';
break;
default:
cout << "Hmmmm";
}
}
}
//--------------------------------------------------------------------------------------------------------------
// showFourLetterHashNumbers()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Displays a command line message with each element of 'fourLetterHash'.
//--------------------------------------------------------------------------------------------------------------
void tth::showFourLetterHashNumbers()
{
for(int i = 0; i < 4; i++)
{
cout << fourLetterHash[i] << " ";
}
}
EDIT
Gah. I put so much effort into this post just to find out it was a simple problem with command line compiling. Thanks for the answers.
Answer
Use the command below to compile the definition file and main program file.
cl tth.cpp useTTH.cpp
You should compile both tth.cpp and useTTH.cpp
with command line:
cl tth.cpp useTTH.cpp
You forgot to include all your source files in your compiler command. As tth.cpp is not included, it cannot find the symbols corresponding to the functions/variables defined there.