|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 
|
Quote:
Originally Posted by Narue
>If you read carefully to what i said
I did read carefully what you said. That's how I knew it was wrong, and a cursory glance at the code was solid proof.  You don't guess when it comes to correctness, you test and verify. And of course, you only post untested code in places like this (where people like me reside) if you're supremely confident that it's perfect. 
|
I post untested code because i don`t have an internet connection at home. And here where i live the internet connection costs. So when i see a post i reply first with what i think and then as i said to you, that you haven`t read carefully to what i said : "I WILL TEST IT AT HOME". This means that i wasn`t at home in the moment i made a post. What do you think of my previous post and about this one :
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;
/*
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
*/
const int NMAX = 200;
/*int table[5][5] =
{{1, 0, 0, 0, 0},
{2, 3, 0, 0, 0},
{4, 5, 6, 0, 0},
{7, 8, 9, 10, 0},
{11, 12, 13, 14, 15}};
{{7, 0, 0, 0, 0},
{3, 8, 0, 0, 0},
{8, 1, 0, 0, 0},
{2, 7, 4, 4, 0},
{4, 5, 2, 6, 5}};//*/
int table[NMAX][NMAX] = {0};
int sum = 0;
int find_max();
bool open_file(string fileName)
{
ifstream inStream(fileName.c_str(), ios::in);
int size = 0, read = 0, count = 0;
while (inStream)
{
inStream >> read;
table[size][count] = read;
if (count == size)
{
size++;
count = 0;
}
else
count++;
}
inStream.close();
return true;
}
void solve_recursive(int i, int j, int sum_temp)
{
if (i >= j && i < NMAX && j < NMAX)
{
sum_temp += table[i][j];
solve_recursive(++i ,j , sum_temp);
solve_recursive(i, ++j, sum_temp);
}
else if(i == NMAX)
{
if (sum < sum_temp) sum = sum_temp;
}
}
void solve_iterative()
{
for (int i = 1; i < NMAX; i++)
for (int j = 0; j <= i; j++)
{
if (j == i)
table[i][j] += table[i-1][j-1];
else if (j == 0)
table[i][j] += table[i-1][j];
else
table[i][j] += (table[i-1][j-1] > table[i-1][j]) ? table[i-1][j-1] : table[i-1][j];
}
sum = find_max();
}
int find_max()
{
const int I = NMAX - 1;
int temp = table[i][0];
for (int j = 1; j < NMAX; j++)
if (table[i][j] > temp) temp = table[i][j];
return temp;
}
int main()
{
int temp = 0, i = 0, j = 0;
string file("INPUT.TXT");
open_file(file);
solve_recursive(i, j, temp);
cout << "Recursive sum : " << sum << endl;
solve_iterative();
cout << "Iterative sum : " << sum << endl;
getchar();
}
Again i tell that if ANYONE sees a bug please report it here.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
|