Hello everybody :icon_neutral:
I'm having an error in finding the biggest/smallest numbers in an array; the debug result gives me wrong numbers. Here's the code that I have been working on:
#include<iostream.h>
int linearsearch(char b[],char searchkey, int size);
void main(void)
{
const int arraysize=7;
char a[arraysize],key;
int result,i;
int s;
int b;
int c;
for (i=0;i<arraysize;i++)
{ cout<<"a["<<i<<"]=";
cin>>a[i];
}
cout<<"\n*Enter the item you want to search for: ";
cin>>key;
result=linearsearch(a,key,arraysize);
if (result != -1)
{ cout<<"\n1-The item has been found: \n"<<"* The position is: "<<result;
cout<<endl<<"* The number is: "<<a[result]<<endl;
s=a[0];
a[0]=a[result];
a[result]=s;
cout<<"\n2-Switching between 1st item and selected item is done!\n";
cout<<"* The array now is: ";
for(i=0;i<arraysize;i++)
{
cout<<a[i];
}
cout<<" "<<endl;
b=a[0];
cout<<"\n3-The biggest item is: ";
for (i=0;i<arraysize;i++)
if (a[i]>b)
{
b=a[i];
}
cout<<b;
c=a[0];
cout<<"\n4-The smallest item is: ";
for (i=0;i<arraysize;i++)
if (a[i]<c)
{
c=a[i];
}
cout<<c;
}
else
cout<<"The item is not found \n";
}
int linearsearch (char b[], char searchkey, int size)
{ int n;
for (n=0;n<size;n++)
if (b[n]==searchkey)
return n;
return -1;
}
The problem is in the IF statements in lines 37 and 46, I guess.
Here's the windows of the debug as an example to my problem: http://img248.imageshack.us/img248/3162/333zx.jpg
I also wanna know how can I type how many even/odd numbers I have in the same array, and what do I have to use to count them.