#include #include #include #include using namespace std; bool isit_prime (int temp); // Function for finding maximum sum int maxPathSum(vector> vect, int m, int n) { // loop for top to botom calculation for (int i=m-1; i>=0; i--) { for (int j=0; j<=i; j++) { //Consider the path only if it is not a prime number. if(!isit_prime(vect[i][j])){ // for each element, check both elements just below the number and below right to the number add the maximum of them to it. if (vect[i+1][j] > vect[i+1][j+1]){ cout << vect[i][j] << endl; vect[i][j] += vect[i+1][j]; } else{ cout << vect[i][j] << endl; vect[i][j] += vect[i+1][j+1]; } } } } // return the top element // which stores the maximum sum return vect[0][0]; } //A function that checks whether input is prime or not. bool isit_prime (int temp){ // 1 and 0 are not prime numbers. if(temp == 1 || temp == 0){ return false; } // 2,3,5,7 are prime numerals. else if(temp == 2 || temp == 3 || temp == 5 || temp == 7 ){ return true; } // These are the numeral factors, if a number can't be divisible without remainder by anyone of these numbers then it is a prime number. else if(temp %2 != 0 && temp %3 != 0 && temp %5 != 0 && temp %7 != 0 ){ return true; } else return false; } /* Driver program to test above functions */ int main() { vector> vect; vector temp_vect; int row = 0; int column = 0; ifstream triangle_file; cout << "Please enter a file name which contains an orthogonal triangle." << endl; string filename = ""; cin >> filename; triangle_file.open(filename); int a; // I read the file and store the data to a temporary vector. while (triangle_file >> a){ temp_vect.push_back(a); } //This will show the index of the element from the temp vect that I am currently at. int current_element = 0; //In this loop I transfer the numbers from temp vect to our 2D vect/matrix for our maxPathSum function. for (row ; row <= column; row++ ){ //I push an empty vector when I get to next row of the triangle because my vects dimension are not defined. vect.push_back(vector()); for (int i=0 ; i <= column; i++ ){ vect[row].push_back(temp_vect[current_element]); //Counts every element transfer. current_element++; // When we go through all the elements temp_vect have, we call the maxPathSum function for our result and print it. if(current_element == temp_vect.size()){ cout << maxPathSum(vect, column , column ); cin >> a; return 0; } } column++; } cout << "There was a problem! Please try again, if not fixed then I did something wrong that causes the program to not work how it is intended in you PC." << endl; return 0; }