Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
Hi every one,

I am trying to implement the Prim’s algorithm. There is no error, but when I run the program an error window appears:
"general protection exception pa.cpp
pa(2) 0x22ef:0x01ab processor fault".

And I have to run this program 3 times with different size of array: "the size is 100,1000,and 1000000".

What should I do? Is there a site that is running these sizes of programs?

This is the code:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
int cost[10][10],k,i,j,stk[10],top,v,visit[10],visited[10],u;
int o[]={30,43,56,18,33,14,40,49,53,11,58,38,54,8,6,15,13,33,39,41,20,28,50,3,59,3,8,46,36,13,
12,17,18,54,37,22,14,1,22,60,59,32,12,20,45,42,36,47,13,30,46,26,47,41,6,45,60,17,43,
18,30,9,25,45,43,1,44,21,15,8,32,18,27,25,47,35,59,22,30,8,43,12,41,12,50,39,30,48,20,
35,41,55,38,45,49,11,4,19,19,11,22};
int p[]={11,4,19,19,11,30,43,56,18,33,14,40,49,53,11,58,38,54,8,6,15,13,33,39,41,20,28,50,3,59
,3,8,46,36,13,12,17,18,54,37,22,14,1,22,60,59,32,12,20,45,42,36,47,13,30,46,26,47,41,
6,45,60,17,43,18,30,9,25,45,43,1,44,21,15,8,32,18,27,25,47,35,59,22,30,8,43,12,41,12,
50,39,30,48,20,35,41,55,38,45,49,33};
main()
{
	
	int m=100,c,n=75;     /*n is no. of vertices  &  m is no. of edges*/
	for(k=1;k<=m;k++)    /*edges cost*/
	{
		i=o[k];
		j=p[k];
		c=rand()%20;
		cost[i][j]=c;
	}
	for(i=1;i<=n;i++)
	for(j=1;j<=n;j++)
		if(cost[i][j]==0)
		cost[i][j]=31999;
	cout <<"ORDER OF VISITED VERTICES";
	k=1;
	while(k<n)
	{
		m=31999;
		if(k==1)
		{
			for(i=1;i<=n;i++)
				for(j=1;j<=m;j++)
				if(cost[i][j]<m)
				{
					m=cost[i][j];
					u=i;
				}
		}
		else
		{
			for(j=n;j>=1;j--)
			if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1)
			{
				visit[j]=1;
				stk[top]=j;
				top++;
				m=cost[v][j];
				u=j;
			}
		}
		cost[v][u]=31999;
		v=u;
		cout<<v << " ";
		k++;
		visit[v]=0; visited[v]=1;
	}
}
Posted
Updated 30-Apr-11 4:07am
v4
Comments
Albert Holguin 28-Apr-11 14:43pm    
edit: added pre tags
CPallini 28-Apr-11 15:55pm    
and I have to run this program 3 time
with diffrent size of array "the size is 100,1000,and 1000000 "

What array?
malemar 28-Apr-11 16:47pm    
o & p array
CPallini 29-Apr-11 3:05am    
Do you explicitely initialize the arrays with 1000 and 1000000 items,as you've done with the 100 case?
You should, at least, declare the arrays having such a number of items, e.g. for the '1000' case:
int o[1000];
int p[1000];

Otherwise the exception is the 'normal behaviour'.
You may consider using std::vector, that is a dynamic array, i.e. it may dynamically grow.

You should use defines or enum values for array sizes. For runtime you can enshure the indices are in array by:
MIDL
ASSERT(m<(sizeof(o)/sizeof(o[0]))); // index out of order
ASSERT(n<(sizeof(cost)/sizeof(cost[0]))); // index out of order
ASSERT(n<(sizeof(cost[0])/sizeof(cost[0][0]))); // index out of order

Otherwise you write values beyond the array size.
Regards.
 
Share this answer
 
This program compiles and runs in Release configuration in VC++ 2008. I don't know if it has any sense, but it outputs some numbers as ORDER OF VISITED VERTICES. All the advices and comments are correct, but this is obviously a sample code from primer.
Sergey Chepurin.
 
Share this answer
 
Hard to tell what size your arrays are from just looking at them (wasn't about to count) but...

for(k=1;k<=m;k++) //<-- Don't forget that array idexes start at 0, not 1, this is likely your problem


Also, I see you declared A LOT of globals, don't do that, bad coding practice.
 
Share this answer
 
v4
Comments
Manfred Rudolf Bihy 28-Apr-11 14:50pm    
Most probably the cause! 5+

Well spotted, Albert!
Albert Holguin 28-Apr-11 14:51pm    
thank you
Sergey Alexandrovich Kryukov 28-Apr-11 16:47pm    
Yes, it's a first thing to check up. A 5.
--SA
Albert Holguin 28-Apr-11 17:09pm    
thank you SA... and thanks for cleaning up after OP
Sergey Alexandrovich Kryukov 28-Apr-11 16:47pm    
OP commented:
thank you mr Albert


I want ask also about running time
this code
clock_t start, end;
start = clock(); //CODES GOES HERE end = clock();
std::cout << end - start <<"\n";
std::cout << (double) (end-start)/ CLOCKS_PER_SEC;

where should I add it


and

can I change the number in this stetment
cost[i][j]=31999;

what is the meaning if this nomber ?

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