Access Violation using Arrays

error LNK2019: unresolved external symbol "int __cdecl realReal(i

Page 1 of 1

8 Replies - 3096 Views - Last Post: 24 February 2009 - 07:25 PM Rate Topic: -----

#1 oskar132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 23-February 09

Access Violation using Arrays

Posted 24 February 2009 - 05:27 PM

the error is this:
Unhandled exception at 0x00412016 in finalfinal.exe: 0xC0000005: Access violation reading location 0x00133000.

the code that executes is this:


cout << "ingrese el primer valor de la suma\n\n";
		cin >> primerRomano;
		romanoReal(primerRomano,numeroReal); 
		realReal(numeroReal);<<<< the error goes here

this is the method  realReal

int realReal(int arregloReal[])
{
	int numeroFinal = 0;

	for(int i=0;i<300;i++)
	{
		int numeroTemporal =0;
		for(int j = i++;i<=300;j++)
		{
			int numero1 = arregloReal[i];
			int numero2 = arregloReal[j]; >>>>>>>> an arrow appears in this line
			if(numero1 < numero2) 
			{
				numeroTemporal = numero2-numero1;
				numeroFinal += numeroTemporal;
			}
			else if(numero1 > numero2)
			{
				numeroTemporal = numero1 + numero2;
				numeroFinal +=numeroTemporal;
			}


		}
	}
	return numeroFinal;
}




so i've done pretty much everything i know to solve this error but no answer :S
plz help!!!

EDIT: Removed the "HEEEEEEEEELP", that isn't going to get you helped any faster :)

Is This A Good Question/Topic? 0
  • +

Replies To: Access Violation using Arrays

#2 crazyjugglerdrummer   User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 124
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Access Violation using Arrays

Posted 24 February 2009 - 05:49 PM

Your function is designed to take an array as its argument, but I'm not sure if you did that. You inputted for the variable like it was just an int, not an array of ints. Change the argument to int from int [], or make sure that you pass an array. Also, make sure that your not trying to access stuff beyond the bounds of the array. Unless your array has a size of at least 300, your program won't work right.

Hope this helps!
and welcome to Dream in Code!!!
Was This Post Helpful? 0
  • +
  • -

#3 oskar132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 23-February 09

Re: Access Violation using Arrays

Posted 24 February 2009 - 05:53 PM

mmm no i just put 300 as a "safe value" so i would never run out of space in the array, but its not 300 the size it's taking.
im making a roman number calculator
i dont know how to create a variable size array :S could u plz help me?
Was This Post Helpful? 0
  • +
  • -

#4 crazyjugglerdrummer   User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 124
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Access Violation using Arrays

Posted 24 February 2009 - 06:19 PM

No 300 is not the size of the array, so when you try to access position 300 of the array, it won't work. Try running the program without the safeguarded values. I'm pretty sure that's the problem. If your post your entire code for the program, it can be easier for us to debug it.

Happy coding!! (and if I'm wrong, just post it so I can learn from my mistakes :) )
Was This Post Helpful? 0
  • +
  • -

#5 oskar132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 23-February 09

Re: Access Violation using Arrays

Posted 24 February 2009 - 06:22 PM

the code is this:
#include <iostream>
using namespace std;
int per(int num,int per);
int numero1;
int numero2;
int main()
{
// variable para	
char operador;
char sistemaNumeracion;
char primerRomano[300];
char segundoRomano[300];
int numeroReal[300];



int valor1;

int valor2;
	int operacion;
	int* romanoReal(char*,int*);
	int realReal(int[300]);
	
//mensaje de bienvenida al usuario que le pide el tipo de operacion que desea realizar	

	cout << "Ingrese R para sistema de numeracion Romano y \n\nP para sistema de numeracion Posicional\n\n";
	cin >> sistemaNumeracion;
	
	if( sistemaNumeracion == 'r')
	{
		cout << "ingrese el valor para la operacion deseada." << "\n\n PARA SUMA : s" << "\n\nPARA MULTIPLICACION: m" << "\n\nPARA RESTA: r" << "\n\n PARA DIVISION:d" << "\n\n PARA PORCENTAJE : p" << "\n\nPARA SUMATORIA DE LOS PRIMERMOS N NUMEROS NATURALES : z" << " \n\nPARA EL NCIMO NUMERO DE LA SERIE FIBONACCI :f\n\n" << "PARA OBTENER EL FACTORIAL DE UN NUEMOR N: h\n\n" << " PARA LA NCIMA POTENCIA DE UN NUMERO: p\n\n ";
	
	cin >> operador;

if(operador == 's')
	{
		cout << "ingrese el primer valor de la suma\n\n";
		cin >> primerRomano;
		romanoReal(primerRomano,numeroReal); 
		cout << realReal(numeroReal);
		

	}
		
		
	
	}
		
		
	else if(sistemaNumeracion == 'p')
	{
	cout << "ingrese el valor para la operacion deseada." << "\n\n PARA SUMA : s" << "\n\nPARA MULTIPLICACION: m" << "\n\nPARA RESTA: r" << "\n\n PARA DIVISION:d" << "\n\n PARA PORCENTAJE : p" << "\n\nPARA SUMATORIA DE LOS PRIMERMOS N NUMEROS NATURALES : z" << " \n\nPARA EL NCIMO NUMERO DE LA SERIE FIBONACCI :f\n\n" << "PARA OBTENER EL FACTORIAL DE UN NUEMOR N: h\n\n" << " PARA LA NCIMA POTENCIA DE UN NUMERO: p\n\n ";
	
	cin >> operador;
	

//metodo encargado de sumar 2 numero	
if(operador == 's')
	{
		cout << "ingrese el primer valor de la suma\n\n";
		cin >> valor1;
		cout << "ahora ingrese el segundo valor";
		cin >> valor2;
		operacion = valor1+ valor2;
		cout << "\n\n El resultado de la suma es :"<< operacion;
		

	}
	
//metodo encargado de hacer la multiplicacion	
else if(operador == 'm')
	{
		cout << "ingrese el primer valor de la multiplicacion\n\n";
		cin >> valor1;
		cout << "ahora ingrese el segundo valor";
		cin >> valor2;
		operacion = valor1*valor2;
		cout << "\n\n El resultado de la multiplicacion es :"<< operacion;
		

	}

//metodo encargado de realizar la resta de dos numero	
else	if(operador == 'r')
	{
		cout << "ingrese el primer valor al que desea restarle el 2do numero\n\n";
		cin >> valor1;
		cout << "ahora ingrese el segundo valor";
		cin >> valor2;
		operacion = valor1-valor2;
		cout << "\n\n El resultado de la resta es :"<< operacion;
		

	}

//metdo encargado de realizar la division	
else	if(operador == 'd')
	{
		cout << "ingrese el primer valor de la division\n\n";
		cin  >> valor1;
		cout << "ahora ingrese el segundo valor";
		cin  >> valor2;
		operacion = valor1/valor2;
		cout << "\n\n El resultado de la division es :"<< operacion;
		

	}

//metodo encargado de sacar el porcentaje	
else	if(operador == 'p')
	{
		cout << "ingrese el  valor \n\n";
		cin  >> valor1;
		cout << "ahora ingrese el porcentaje";
		cin  >> valor2;
		cout << "\n\n El resultado es :"<< ((valor1*valor2)/100);
		

	}
	
//metodo encargado de sumar los n numeros naturales	
else	if(operador == 'z')
	{
		cout << "ingrese el  valor \n\n";
		cin  >> valor1;
		
		cout << "\n\n El resultado es :"<< valor1*(valor1+1)/2;
		

	}
//metodo encargado de realizar la secuencia de fibonacci	
	
	
	//metodo que se ejecuta cuando no se selecciona una opcion correcta	
else
	{
		cout << "\nel valor dadio no pertenece a ninguna operacion\n";
	}
	system("pause");
														  
}
	

//metodo que se ejecuta cuando no se selecciona una opcion correcta	
else
	{
		cout << "\nel valor dadio no pertenece a ninguna operacion\n";
	}
	system("pause");
}


int* romanoReal(char* romano, int* real) 
{
	int *numeroReal1 = new int [300];
	
	for(int i=0;i<=300;i++)
{
	char comparar =romano[i];
	if(comparar =='x')
	{
	numeroReal1[i]=10;

	}
	if(comparar =='i')
	{
	numeroReal1[i]=1;

	}
	else if(comparar =='v')
	{
	numeroReal1[i]=5;

	}
	else if(comparar =='l')
	{
	numeroReal1[i]=50;

	}
	else if(comparar =='c')
	{
	numeroReal1[i]=100;

	}
	else if(comparar =='d')
	{
	numeroReal1[i]=10;

	}
}
	cout << numeroReal1[0];
	real = numeroReal1;
	return numeroReal1;
	

}



int realReal(int arregloReal[300])
{
	int numeroFinal = 0;

	for(int i=0;i<300;i++)
	{
		int numeroTemporal =0;
		for(int j = i+1;i<=300;j++)
		{
			int numero1 = arregloReal[i];
			int numero2 = arregloReal[j];
			if(numero1 < numero2) 
			{
				numeroTemporal = numero2-numero1;
				numeroFinal += numeroTemporal;
			}
			else if(numero1 > numero2)
			{
				numeroTemporal = numero1 + numero2;
				numeroFinal +=numeroTemporal;
			}


		}
	}
	return numeroFinal;
}



srry but i dont know what safeguarded values are :S i'm really new to c++
Was This Post Helpful? 0
  • +
  • -

#6 crazyjugglerdrummer   User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 124
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Access Violation using Arrays

Posted 24 February 2009 - 06:47 PM

When you access an array, you have to be sure that you're within the limits of the array. If we make sure that the array is the right size (like you did, declaring it with [300]) and make sure that none of the spaces are empty (see what I added to the top of the program), all should be well.
Look at my comments for the changes :D
#include <iostream>
using namespace std;
int per(int num,int per);
int realReal(int[]); //prototype
int numero1;
int numero2;
int main()
{
	// variable para	
	char operador;
	char sistemaNumeracion;
	char primerRomano[300];
	char segundoRomano[300];
	int numeroReal[300];
	
	
	
	int valor1;
	
	int valor2;
	int operacion;
	int* romanoReal(char*,int*);
	int realRealArray[300];	 //change declaration a bit, name array something other than function name
	
	for (short x=0; x<300; x++)
	{
		realRealArray[x]=0;   //stuff array full of 0's
	}
	
	//mensaje de bienvenida al usuario que le pide el tipo de operacion que desea realizar	
	
	cout << "Ingrese R para sistema de numeracion Romano y \n\nP para sistema de numeracion Posicional\n\n";
	cin >> sistemaNumeracion;
	
	if( sistemaNumeracion == 'r')
	{
		cout << "ingrese el valor para la operacion deseada." << "\n\n PARA SUMA : s" << "\n\nPARA MULTIPLICACION: m" << "\n\nPARA RESTA: r" << "\n\n PARA DIVISION:d" << "\n\n PARA PORCENTAJE : p" << "\n\nPARA SUMATORIA DE LOS PRIMERMOS N NUMEROS NATURALES : z" << " \n\nPARA EL NCIMO NUMERO DE LA SERIE FIBONACCI :f\n\n" << "PARA OBTENER EL FACTORIAL DE UN NUEMOR N: h\n\n" << " PARA LA NCIMA POTENCIA DE UN NUMERO: p\n\n ";
		
		cin >> operador;
		
		if(operador == 's')
		{
			cout << "ingrese el primer valor de la suma\n\n";
			cin >> primerRomano[0];	  //can't input for whole array, but for specific place
			romanoReal(primerRomano,numeroReal);
			cout << realReal(numeroReal);
			
			
		}
		
		
		
	}
	
	
	else if(sistemaNumeracion == 'p')
	{
		cout << "ingrese el valor para la operacion deseada." << "\n\n PARA SUMA : s" << "\n\nPARA MULTIPLICACION: m" << "\n\nPARA RESTA: r" << "\n\n PARA DIVISION:d" << "\n\n PARA PORCENTAJE : p" << "\n\nPARA SUMATORIA DE LOS PRIMERMOS N NUMEROS NATURALES : z" << " \n\nPARA EL NCIMO NUMERO DE LA SERIE FIBONACCI :f\n\n" << "PARA OBTENER EL FACTORIAL DE UN NUEMOR N: h\n\n" << " PARA LA NCIMA POTENCIA DE UN NUMERO: p\n\n ";
		
		cin >> operador;
		
		
		//metodo encargado de sumar 2 numero	
		if(operador == 's')
		{
			cout << "ingrese el primer valor de la suma\n\n";
			cin >> valor1;
			cout << "ahora ingrese el segundo valor";
			cin >> valor2;
			operacion = valor1+ valor2;
			cout << "\n\n El resultado de la suma es :"<< operacion;
			
			
		}
		
		//metodo encargado de hacer la multiplicacion	
		else if(operador == 'm')
		{
			cout << "ingrese el primer valor de la multiplicacion\n\n";
			cin >> valor1;
			cout << "ahora ingrese el segundo valor";
			cin >> valor2;
			operacion = valor1*valor2;
			cout << "\n\n El resultado de la multiplicacion es :"<< operacion;
			
			
		}
		
		//metodo encargado de realizar la resta de dos numero	
		else	if(operador == 'r')
		{
			cout << "ingrese el primer valor al que desea restarle el 2do numero\n\n";
			cin >> valor1;
			cout << "ahora ingrese el segundo valor";
			cin >> valor2;
			operacion = valor1-valor2;
			cout << "\n\n El resultado de la resta es :"<< operacion;
			
			
		}
		
		//metdo encargado de realizar la division	
		else	if(operador == 'd')
		{
			cout << "ingrese el primer valor de la division\n\n";
			cin  >> valor1;
			cout << "ahora ingrese el segundo valor";
			cin  >> valor2;
			operacion = valor1/valor2;
			cout << "\n\n El resultado de la division es :"<< operacion;
			
			
		}
		
		//metodo encargado de sacar el porcentaje	
		else	if(operador == 'p')
		{
			cout << "ingrese el  valor \n\n";
			cin  >> valor1;
			cout << "ahora ingrese el porcentaje";
			cin  >> valor2;
			cout << "\n\n El resultado es :"<< ((valor1*valor2)/100);
			
			
		}
		
		//metodo encargado de sumar los n numeros naturales	
		else	if(operador == 'z')
		{
			cout << "ingrese el  valor \n\n";
			cin  >> valor1;
			
			cout << "\n\n El resultado es :"<< valor1*(valor1+1)/2;
			
			
		}
		//metodo encargado de realizar la secuencia de fibonacci	
		
		
		//metodo que se ejecuta cuando no se selecciona una opcion correcta	
		else
		{
			cout << "\nel valor dadio no pertenece a ninguna operacion\n";
		}
		//system("pause");
		
	}
	
	
	//metodo que se ejecuta cuando no se selecciona una opcion correcta	
	else
	{
		cout << "\nel valor dadio no pertenece a ninguna operacion\n";
	}
   // system("pause");
}


int* romanoReal(char* romano, int* real)
{
	int *numeroReal1 = new int [300];
	
	for(int i=0;i<=300;i++)
	{
		char comparar =romano[i];
		if(comparar =='x')
		{
			numeroReal1[i]=10;
			
		}
		if(comparar =='i')
		{
			numeroReal1[i]=1;
			
		}
		else if(comparar =='v')
		{
			numeroReal1[i]=5;
			
		}
		else if(comparar =='l')
		{
			numeroReal1[i]=50;
			
		}
		else if(comparar =='c')
		{
			numeroReal1[i]=100;
			
		}
		else if(comparar =='d')
		{
			numeroReal1[i]=10;
			
		}
	}
	cout << numeroReal1[0];
	real = numeroReal1;
	return numeroReal1;
	
	
}



int realReal(int arregloReal[300]) //nice try, I like the idea a lot, but complilers aren't that smart yet :(
{
	int numeroFinal = 0;
	
	for(int i=0;i<300;i++)
	{
		int numeroTemporal =0;
		for(int j = i;j<=300;j++) //changed a little
		{
			int numero1 = arregloReal[i];
			int numero2 = arregloReal[j];
			if(numero1 < numero2)
			{
				numeroTemporal = numero2-numero1;
				numeroFinal += numeroTemporal;
			}
			else if(numero1 > numero2)
			{
				numeroTemporal = numero1 + numero2;
				numeroFinal +=numeroTemporal;
			}
			
			
		}
	}
	return numeroFinal;
}




Hope this version works!
Was This Post Helpful? 0
  • +
  • -

#7 oskar132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 23-February 09

Re: Access Violation using Arrays

Posted 24 February 2009 - 06:59 PM

well now it works but as u can see i made an if that says
if(comparar =='x')
		{
			numeroReal1[i]=10;
			
		}


it's supossed to make it a 10 in the other array
now when i execute this method
 cout << "ingrese el primer valor de la suma\n\n";
			cin >> primerRomano[0];	  //can't input for whole array, but for specific place
			romanoReal(primerRomano,numeroReal);
			cout << realReal(numeroReal);


it drops a 100 dont know why :S it should drop a 10 shouldn't it?

seems like it's adding an extra 0 to the numbes :S lol
thx a lot man!!!! u saved my life! as we say in my country u'll go to heaven with ur shoes on ;) hahaha thx a lot again

This post has been edited by oskar132: 24 February 2009 - 07:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 crazyjugglerdrummer   User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 124
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Access Violation using Arrays

Posted 24 February 2009 - 07:09 PM

My guess is that since we stuffed the array with 0's, it felt like displaying one of them. numeroReal1[0] is 10, but numeroReal[1] is 0, so I believe that the values are fine, but that the index 1 got displayed as well.

happy coding!
Was This Post Helpful? 0
  • +
  • -

#9 oskar132   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 23-February 09

Re: Access Violation using Arrays

Posted 24 February 2009 - 07:25 PM

one final question
im supossed to sume 2 numbers :S

so what im trying to do is to use 2 arrays for each roman number i get
 if( sistemaNumeracion == 'r')
	{
		cout << "ingrese el valor para la operacion deseada." << "\n\n PARA SUMA : s" << "\n\nPARA MULTIPLICACION: m" << "\n\nPARA RESTA: r" << "\n\n PARA DIVISION:d" << "\n\n PARA PORCENTAJE : p" << "\n\nPARA SUMATORIA DE LOS PRIMERMOS N NUMEROS NATURALES : z" << " \n\nPARA EL NCIMO NUMERO DE LA SERIE FIBONACCI :f\n\n" << "PARA OBTENER EL FACTORIAL DE UN NUEMOR N: h\n\n" << " PARA LA NCIMA POTENCIA DE UN NUMERO: p\n\n ";
		
		cin >> operador;
		
		if(operador == 's')
		{
			cout << "ingrese el primer valor de la suma\n\n";
			cin >> primerRomano[0];	  //can't input for whole array, but for specific place
			romanoReal(primerRomano,numeroReal);
			numero1 = realReal(numeroReal);
			
			cout << "\n\nahora ingrese el segundo numero que desea sumar\n\n";
			cin >> segundoRomano[0];
			romanoReal(segundoRomano,numeroReal);
			numero2 = realReal(numeroReal);
			
			cout << "\n\noperacion es" << numero1;
			
			
			
		}


in the line
cout << "\n\noperacion es" << numero1;
on numero1 i get a 0 :S seems like its not savin the values or something :S
any idea?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1