Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 15th, 2006, 3:23 PM   #1
backstabber
Newbie
 
Join Date: Oct 2006
Posts: 9
Rep Power: 0 backstabber is on a distinguished road
Unhappy help with assignment code returns no answer

i have been on this particular challenge for quite a while now and i want to finish it but i do not know what is wrong with my source.

i would appreciate help and thanks in advance.

this program is trying to do the following

Number Triangles

7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

The above figure shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right. In the sample shown above, the route from 7 to 3 to 8 to 7 to 5 produces the highest sum: 30.

my triangle is in the zip file.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   int num, rows;
   int maxsum[200][200] = {0};
   int best = 0;
    //cout << "it works here: definitions ";
   ifstream infile("INPUT.TXT");
   ofstream outfile("OUTPUT.TXT");
    //cout << "it works here: filestreams ";
   infile >> rows;
   //cout << "it works here: infilestream rows ";
   for(int b = 0; b < 200; b++)
   {
		maxsum[0][b] = 0;
		//cout << "it works here: first for loop: iter:" << b<<" \n";
		for(int r = 1; r < rows; r++)
		{
			//cout << "it works here: second for loop: iter:"<<r<<" ";
			//maxsum[r][0] = 0;
			for(int c = 1; c < r; c++)
			{
				//cout << "it works here: third for loop: iter:"<<c<<" ";
				infile >> num;
				// cout << num << " ";
				maxsum[r][c] = (maxsum[r-1][c-1] > maxsum[r-1][c]) ?
                        (maxsum[r-1][c-1] + num) :
                        (maxsum[r-1][c]   + num);
				// cout << maxsum[r][c] << " " << maxsum[r-1][c-1] << " " << maxsum[r-1][c] << " ";
			}
			maxsum[r][r+1] = 0;
		}

		for(int k = 1; k < rows; k++)
		{
			if(best < maxsum[rows][k]) 
				best = maxsum[rows][k];
			//cout << "it works here: fourth for loop: iter:"<<b<<" ";
		}
	}
	outfile << best;
    cout << best<<endl;
	cout << "it works here: last thing\n";
	return 0;
}

the input file is included
Attached Files
File Type: zip INPUT.zip (22.7 KB, 32 views)
backstabber is offline   Reply With Quote
Old Dec 15th, 2006, 3:49 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>i would appreciate help
I'm sure you would, but until you ask an actual question, you won't get actual help.

>and thanks in advance.
"Thanks in advance" is used by bill collectors as a way to create a feeling of dominance with the expectation of compliance. Some people are very offended by it, so I would recommend saving your thanks until after you've been helped (but never forget it).

I also find your name to be humorously ironic after watching your thread on Daniweb.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Dec 16th, 2006, 6:57 PM   #3
backstabber
Newbie
 
Join Date: Oct 2006
Posts: 9
Rep Power: 0 backstabber is on a distinguished road
i would have thought the name of the thread was enough.

and as to my name i did not stab daniweb in the back by moving into programmingforums because they are friends or allies or something
this thread is to help me complete the assignment the daniweb one was to get rid of the "unknown error".
backstabber is offline   Reply With Quote
Old Dec 16th, 2006, 7:15 PM   #4
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>i would have thought the name of the thread was enough.
It's not. 'No answer' and 'incorrect answer' are two very different things. From what I've seen of your other thread(s), the problem is that the result is a very large negative number, which suggests that you're overflowing the data type.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Dec 19th, 2006, 11:25 AM   #5
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
I think this will work :
int sum = 0;
int table[5][5] = {.....};

void solve(int i, int j, int &sum_temp)
{
     if (i =< j and i <= 5)
     {
            sum_temp += table[i][j];
            solve(i++ ,j , sum_temp);
            solve(i++, j++, sum_temp);
     }
     else if(i == 6)
     {
              if (sum < sum_temp) sum = sum_temp;
     }
}
But be careful that this program if it works correctly (i will try at home) for very large matrixes will need a lot of time solving.
pegasus001 is offline   Reply With Quote
Old Dec 19th, 2006, 11:45 AM   #6
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>I think this will work
If by "work" you mean stack overflow then yes, it works perfectly. Tell me, what are the values of i and j after these recursive calls?
solve(i++ ,j , sum_temp);
solve(i++, j++, sum_temp);
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Dec 20th, 2006, 7:43 AM   #7
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
sorry to every body here is the revised code :
#include <iostream>
#include <conio.h>

using namespace std;

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 sum = 0;

void solve(int i, int j, int sum_temp)
{
     if (i >= j && i < 5 && j < 5)
     {
            sum_temp += table[i][j];
            solve(++i ,j , sum_temp);
            solve(i, ++j, sum_temp);
     }
     else if(i == 5)
     {
              if (sum < sum_temp) sum = sum_temp;
     }
}


int main()
{
	int temp = 0, i = 0, j = 0;

	solve(i, j, temp);
	cout << "Sum is : " << sum << endl;
	getchar();
}

I will work today to solve it iteratively.
pegasus001 is offline   Reply With Quote
Old Dec 20th, 2006, 7:51 AM   #8
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Quote:
Originally Posted by Narue View Post
>I think this will work
If by "work" you mean stack overflow then yes, it works perfectly.
If you read carefully to what i said :
"I THINK THIS WILL WORK", it doesn`t mean that i was 100% sure of it working correctly but as you can see now i think that the new post will work. I only tested it with those two tables, if you see a bug please tell me.

Last edited by pegasus001; Dec 20th, 2006 at 8:09 AM.
pegasus001 is offline   Reply With Quote
Old Dec 20th, 2006, 8:38 AM   #9
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Dec 21st, 2006, 7:46 AM   #10
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Thumbs up

Quote:
Originally Posted by Narue View Post
>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.
pegasus001 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Code compiled without error, but gets the wrong answer; Calculating Volume Fall Back Son C 15 Oct 21st, 2006 6:51 PM
EXECryptor software protection Jean5 C++ 35 Oct 10th, 2006 7:10 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 11:53 AM
How to post a question nnxion C++ 0 Jun 3rd, 2005 8:55 AM
How to post a question nnxion C 0 Jun 3rd, 2005 8:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC