Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
when i run my code
it says Access violation at address XXXXXX. Write of address XXXXXX
en line that its
d.plus(a,b)
whats wrong plz Pleases help:(

C++
#include <iostream>
#include <conio.h>


class Polynomial
{
public:
int array[100][100];
//==================================================
Polynomial::Polynomial() // constructor
{
for ( int i = 0; i < 100; i++ )
   for( int j=0;j<100;j++)
                           {
array[i][j] = 0;
}
}
//================================================
void set ( int a , int b,int c ) //set
{
array[a][b] = c;
}
//===================================================
Polynomial plus ( Polynomial x,Polynomial y )
{
Polynomial c1;

for ( int i=0; i<100; ++i ) {
    for(int j=0; j<100 ;++j){
 c1.array[i][j] = x.array[i][j] + y.array[i][j];
         }}

 return c1;
}
//===========================================

void print()
{
for (int i=99; i>= 0; i-- ) {
for(int j= 99; j>=0;--j){
if ( array[i][j] != 0 ) {
cout <<"+("<< array[i][j] <<")"<< "x^" << i <<"y^"<<j;
				}
			}
		}
	}
};

int main(){

Polynomial a, b, d,f;
a.set(2,2,3);
b.set(2,3,2);
d.plus(a,b);
getch();
}
Posted
Updated 23-Dec-11 9:17am
v2
Comments
RaviRanjanKr 23-Dec-11 15:17pm    
It should be better to avoid short texts words like Plz instead of using Please while questioning and Answering. :)
DominoBoy 23-Dec-11 15:18pm    
:|
Chuck O'Toole 23-Dec-11 16:02pm    
Dunno dude, it doesn't blow up for me. Using VS2008.

I tested the code and did not get the exception.

You loose the result of calculation though; do something like that:
C#
f = d.plus(a,b);
_getch(); // not deprecated getch() 


The code is pretty bad though; in particular, you should not hard-code this '100', especially if you use it in different places. How can you support it if you want to change this 100 to something else? The whole idea is wrong; you should pass the rank via constructor, for example. It's pointless to carry many unused elements. You really need to allocate the arrays dynamically. Also, what's "polynomial" in this class. A polynomial expressions can be operated in Computer Algebra Systems (CAS), for example, in symbolic calculations.

Please see this useful overview of returning the class instance: http://www.informit.com/articles/article.aspx?p=25033&seqNum=3[^].

—SA
 
Share this answer
 
v5
Comments
Chuck O'Toole 23-Dec-11 16:37pm    
while that is true, since he does absolutely nothing with the value returned from the plus function, I can't see how that caused bad memory references. What is true is that he's moving around some large objects on the stack but not enough to cause a stack overflow, on my system anyway.
Sergey Alexandrovich Kryukov 23-Dec-11 16:41pm    
Well, I agree... But pay attention: neither OP nor I mentioned stack overflow. I changed the answer, thank you very much.
--SA
Chuck O'Toole 23-Dec-11 17:07pm    
Ah, you totally changed your solution so now my comment is out of context. Might as well delete it.
Sergey Alexandrovich Kryukov 23-Dec-11 17:13pm    
Maybe... Instead, I'm trying to bring it to some really useful state, step by step. Just added a link to the overview on the ways to return classes, will think a bit more...
Thank you.
--SA
Use i++ instead of ++i and j++ instead of ++j.
When you run a loop as long as i or j are smaller than 100, but you promote i and j using ++i or ++j, the last round, i or j becomes 100 and you access a cell in the array that doesn't exists (since the range of cells is 0 to 99).
But if you use i++ and j++, then when they get to 100, the program exits the loop without them having the value of 100.
 
Share this answer
 
Comments
Chuck O'Toole 25-Dec-11 20:29pm    
Nope, sorry. In the code above, all instances of unary ++ or -- are in single statements for loop control. i++ or ++i have absolutely no difference in how it affects i in those statements, same thing for j. Yes, the order of operations may be inportant in other type of uses for unary ++ or -- but not in this instance and it is not the source of whatever perceived problem this user has. Note that both SA and I have built the code the OP posted and do not have the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900