Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function serach coded correctly, but outputs wrong data!

Status
Not open for further replies.

Goku

Programmer
Feb 2, 2001
3
US
alright here is my programming code.

// program 12.4.6
// author: mike morales
// objective: to take a two dimensional array,
// and then sort's it's elements in ascending order,
// and store them in a one dimensional array
#include <iostream>
#include<string>
using namespace std;
/*void SelectionSort(int array[],int g, int value1, int value2);*/

int Search( const int F[], int p, int Key){
for(int i=0;i<p;++i){
if(F==Key){

return i;

}
}
return p;
}
int main(){
int A[4][5] ={{16,22,99,4,18},{-258,4,101,5,98},{105,6,15,2,45},{33,88,72,16,3}}, B[100];
// no we are going to convert
int n;
cout <<&quot; number of elements?&quot; << endl;
cin >> n;


for (int r=0;r<4;++r){
for(int c=0;c<5;++c){
B[20]= A[r][c];
B[n]=B[20];
cout << B[n] << endl;
}


}
int Key;
cout << &quot; enter search value &quot; << endl;
cin >> Key;

int q = Search(B,n,22);
cout << q << endl;

return 0;
}

supposedly, the function search should return the matching subscript of that elements value. per se... i pick 22, it should return the address for that element which is 1...but i keep on getting a twenty!

is something wrong with my if statements or do i need to declare a new variable, instead of &quot;n&quot; in my search function?

plz help!
 
Well in your code the variable 'F' is a pointer, so this comparison:

if(F==Key){


will likely never be true and your code will return 'p'

Good luck
-pete
 
That is from your code:

for (int r=0;r<4;++r){
for(int c=0;c<5;++c){
B[20]= A[r][c];
B[n]=B[20];
cout << B[n] << endl;
}


You are not storing numbers in B array.you can use this code in the body of the loop.
{
static int q=0;
B[q]=A[r][c];
cout<<B[q]<<endl;
q++;
}

P.S. To grab size of the array use: sizeof(array_name)/sizeof(classofVariableused); Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top