I'm a student and this is one of my assignments. My professor basically confirmed that my code is correct but all he grades on is if I finished the assignment correctly but not the coding style. I think I did poorly in terms of code style and implementation. I'm very frustrated and would like some guidance on how to improve and think about problems in a better way.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using std::cout;
using std::cin;
using std::ifstream;
using std::endl;
const int MAX_ROW = 100;
const int MAX_COL = 100;
//Function Prototypes
void mapData(int Map[100][100], int rowCount, int colCount);
int findMax(int Map[100][100], int rowCount, int colCount);
int findMin(int Map[100][100], int rowCount, int colCount);
void drawMap(int Map[100][100], int rowCount, int colCount);
int drawLowestElevPath(int Map[100][100], int rowCount, int colCount, int startingRow);
int main() {
int Map[100][100];
int startingRow, max, min, elevChange;
mapData(Map, MAX_ROW, MAX_COL);
max = findMax(Map, MAX_ROW, MAX_COL);
min = findMin(Map, MAX_ROW, MAX_COL);
drawMap(Map, MAX_ROW, MAX_COL);
// Taking the starting row input from the user
cout << "What is the starting row? 0-99" << " ";
cin >> startingRow;
while (true)
{
if (startingRow < 0 || startingRow > 99) {
cout << "That is not a valid row. Try again.";
cin >> startingRow;
} else {
break;
}
}
elevChange = drawLowestElevPath(Map, MAX_ROW, MAX_COL, startingRow);
return 0;
}
// Inputs a list of integers from a text file into a 2D array in row major
void mapData(int Map[100][100], int rowCount, int colCount)
{
ifstream myIn;
myIn.open("mapdata.txt"); //opening the text file
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
myIn >> Map[i][j]; //reading each integer into an index in the array
}
}
}
// Determines the largest integer in the array
int findMax(int Map[100][100], int rowCount, int colCount)
{
int max = Map[0][0];
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
if (Map[i][j] > max)
{
max = Map[i][j];
}
}
}
return max;
}
// Determines the smallest integer in the array
int findMin(int Map[100][100], int rowCount, int colCount)
{
int min = Map[0][0];
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
if (Map[i][j] < min)
{
min = Map[i][j];
}
}
}
return min;
}
// Prints each integer in the array creating a map
void drawMap(int Map[100][100], int rowCount, int colCount)
{
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
cout << Map[i][j] << " ";
}
cout << endl;
}
}
int drawLowestElevPath(int Map[100][100], int rowCount, int colCount, int startingRow)
{
int elevChange1, elevChange2, elevChange3, currentRow, currentCol, totalElevChange = 0;
currentRow = startingRow;
currentCol = 0;
for (int i = 0; i < colCount; i++)
{
if (currentRow == 0)
{
elevChange2 = abs(Map[currentRow][currentCol] - Map[currentRow][currentCol + 1]);
elevChange3 = abs(Map[currentRow][currentCol] - Map[currentRow + 1][currentCol + 1]);
Map[currentRow][currentCol] = 1;
if (elevChange2 == elevChange3)
{
currentCol++;
totalElevChange += elevChange2;
} else if (elevChange2 < elevChange3)
{
currentCol++;
totalElevChange += elevChange2;
} else {
currentRow++;
currentCol++;
totalElevChange += elevChange3;
}
} else if ( currentRow == 99)
{
elevChange1 = abs(Map[currentRow][currentCol] - Map[currentRow - 1][currentCol + 1]);
elevChange2 = abs(Map[currentRow][currentCol] - Map[currentRow][currentCol + 1]);
Map[currentRow][currentCol] = 1;
if (elevChange1 == elevChange2)
{
currentCol++;
totalElevChange += elevChange2;
} else if (elevChange1 < elevChange2)
{
currentCol++;
currentRow--;
totalElevChange += elevChange1;
} else {
currentCol++;
totalElevChange += elevChange2;
}
} else {
elevChange1 = abs(Map[currentRow][currentCol] - Map[currentRow - 1][currentCol + 1]);
elevChange2 = abs(Map[currentRow][currentCol] - Map[currentRow][currentCol + 1]);
elevChange3 = abs(Map[currentRow][currentCol] - Map[currentRow + 1][currentCol + 1]);
Map[currentRow][currentCol] = 1;
if (elevChange1 == elevChange2 && elevChange2 == elevChange3)
{
currentCol++;
totalElevChange += elevChange2;
} else if (elevChange2 == elevChange1 && elevChange2 < elevChange3)
{
currentCol++;
totalElevChange += elevChange2;
} else if (elevChange2 == elevChange3 && elevChange2 < elevChange1)
{
currentCol++;
totalElevChange += elevChange2;
} else if (elevChange1 == elevChange3 && elevChange1 < elevChange2)
{
int randNum = rand() % 2;
if (randNum == 0)
{
currentRow--;
currentCol++;
totalElevChange += elevChange1;
} else
{
currentRow++;
currentCol++;
totalElevChange += elevChange3;
}
} else if (elevChange1 < elevChange2 && elevChange1 < elevChange3)
{
currentRow--;
currentCol++;
totalElevChange += elevChange1;
} else if (elevChange2 < elevChange1 && elevChange2 < elevChange3)
{
currentCol++;
totalElevChange += elevChange2;
} else if (elevChange3 < elevChange1 && elevChange3 < elevChange2)
{
currentRow++;
currentCol++;
totalElevChange += elevChange3;
}
}
}
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
if (Map[i][j] != 1)
{
Map[i][j] = 0;
}
cout << Map[i][j] << " ";
}
cout << endl;
}
return totalElevChange;
}