Home > Net >  Error while printing 2 arrays with sort selection method
Error while printing 2 arrays with sort selection method

Time:03-08

Ive been working with this project for school to leanr about the sorting and searching methos like quicksort, bubblesort, etc. I started working one cpp file per method. Everythind almost works fine, but when the program is printing the info, the first column (words) prints 0x70fc90 instead the words.

#include<iostream>  
using namespace std;  
int buscarpeq (int[],int);  
int main ()  
{  
    string busquedasFrecuentes[20] ={"2d arrays","matrices","algoritmos", "arrays c  ", "C   Multidimensional", "initialize 2d array", "center elements","2d array as element", "matriz bidimensional","too many initializers","0x6ffdf0 error c  ","c   beginners","error initializers char","combining 2d array","random number list generator","multidimensional arrays","c   matriz 2 arrays","llenar matriz letras","multidimensional char array","c   matrix bidimensional"};
    int frecuencia [] = {152,5,5,842,476,438,65,152,148,4,16,5,634,634,120,16,729,148,83,645,83,570,148,842,706,788,842,716,395,707,707,152,500,560,614,463,847,152,83,707};                
    int pos,temp,pass=0; 
     
     
    cout<<"\n\n\n Elementos a ordenar\n";  
    for(int i=0;i<20;i  )  
    {  
        cout<<busquedasFrecuentes[i]<<"\t";  
    }  
    cout<<"\n\n\n Elementos a ordenar\n";  
    for(int i=0;i<20;i  )  
    {  
        cout<<frecuencia[i]<<"\t";  
    } 
    for(int i=0;i<20;i  )  
    {  
        pos = buscarpeq (frecuencia,i);  
        temp = frecuencia[i];  
        frecuencia[i]=frecuencia[pos];  
        frecuencia[pos] = temp; 
        pass  ;
    }  
    cout<<"\n\n\n Lista ordenada...\n";  
    for(int i=0;i<20;i  )  
    {  
        cout << "    "  << busquedasFrecuentes << "     " <<frecuencia[i]<< sizeof(20) << endl;
    } 
    cout<<"\n\nNumero de ciclos usados para ordenar la lista: "<<pass<<endl;
    return 0;  
}  
int buscarpeq(int frecuencia[],int i)  
{  
    int ele_small,position,j; //declaramos variables elementos menores y posiciones 
    ele_small = frecuencia[i];  
    position = i;  
    for(j=i 1;j<20;j  )  
    {  
        if(frecuencia[j]<ele_small)  
        {  
            ele_small = frecuencia[j];  
            position=j;  
        }  
    }  
    return position;  
}

Any suggestion?

CodePudding user response:

You forgot to append [i] on busquedasFrecuentes

Instead of this:

cout << "    "  << busquedasFrecuentes << "     " <<frecuencia[i]<< sizeof(20) << endl;

This:

cout << "    "  << busquedasFrecuentes[i] << "     " <<frecuencia[i]<< sizeof(20) << endl;

Also, I'm not sure why you want to print sizeof(20) at the end of each string. That will always resolve to the number 4(or more precisely, the size of an integer on your platform) getting printed on the end of each line.

  •  Tags:  
  • c
  • Related