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.

55 is the ASCII code for the character '7' and 49 is the ASCII code for the character '1'. This means that your program is functioning correctly, but you have an intent error.

Your error appears to be that your array and key are defined as char instead of int like everything else. Either change them to int or change everything else (except arraysize) to char and see what happens.

PS:
Your main() is defined as void main(void). This is not correct. The C++ standard requires that main() return an int. If you are not using command-line arguments, main() should be defined as " int main() " the use of void parameters is redundant in C++, just leave the parentheses empty if there are no arguments/parameters.

Edited 6 Years Ago by Fbody: n/a

55 is the ASCII code for the character '7' and 49 is the ASCII code for the character '1'. This means that your program is functioning correctly, but you have an intent error.

Your error appears to be that your array and key are defined as char instead of int like everything else. Either change them to int or change everything else (except arraysize) to char and see what happens.

Oh my god; I'm really an idiot :icon_eek:
declared eveything back to int and everything is now working great. Thanks.

What about the second thing that I need some help in?

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.

Oh yeah....

The process would be similar to your highest/lowest processes except you would take the modulus relative to 2 then increment the appropriate counter depending on the result. If you do it correctly, you should be able to do both at the same time rather than separately.

Edited 6 Years Ago by Fbody: n/a

Oh yeah....

The process would be similar to your highest/lowest processes except you would take the modulus relative to 2 then increment the appropriate counter depending on the result. If you do it correctly, you should be able to do both at the same time rather than separately.

Do you mean doing the IF statement that declares the number being even or odd, I mean this one:

if (x%2==0)

But then what to do? I understand that we need to make a count but I don't know how.


But first about the main and changing it to int main(), I tried that but had a warning that I need it to return a value, so I changed it back, and now I have another error while trying to switch between bigger / smaller numbers. Here's the modified code:

#include<iostream.h>
int linearsearch(int b[],int searchkey, int size);
void main(void)
{
 const int arraysize=7;
 int a[arraysize],key;
 int result,i;
 int s;
 int b;
 int c;
 int q;
 int q2;
 int f;

 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 numberis: ";
  for (i=0;i<arraysize;i++)
  if (a[i]>b)
  {
  b=a[i];
  q=i;
  }
  cout<<b;


  c=a[0];
  cout<<"\n4-The smallest number is: ";
  for (int j=0;j<arraysize;j++)
  if (a[j]<c)
  {
  c=a[j];
  q2=i;
  }
  cout<<c;

  f=a[q];
  a[q]=a[q2];
  a[q2]=f;
  cout<<endl;
  cout<<"\n5-Switching between biggest and smallest number is done!\n";
  cout<<"* The array now is: ";
  for(i=0;i<arraysize;i++)
  {
  cout<<a[i];
  }
  cout<<" "<<endl;

}
else
cout<<"The item is not found \n";
}

int linearsearch (int b[], int searchkey, int size)
{ int n;
for (n=0;n<size;n++)
if (b[n]==searchkey)
return n;

return -1;
}

And here's the error:http://img85.imageshack.us/img85/9914/222jq.jpg

But first about the main and changing it to int main(), I tried that but had a warning that I need it to return a value, so I changed it back, and now I have another error while trying to switch between bigger / smaller numbers.

It does need to return a value. A value of 0 is considered a successful execution.

int main() {

  /*...the main program code...*/

  return 0;
}

Do you mean doing the IF statement that declares the number being even or odd, I mean this one:

But then what to do? I understand that we need to make a count but I don't know how.

That is correct. I think you know what to do. You'll need 2 accumulators. The one you increment depends on the result of the conditional.

I can't tell you what's going on with your last output, I don't see anything in your code right off hand. Also, the way you have formatted the output makes it impossible to tell what characters are part of which iteration.

Edited 6 Years Ago by Fbody: n/a

Do you mean doing the IF statement that declares the number being even or odd, I mean this one:

if (x%2==0)

But then what to do? I understand that we need to make a count but I don't know how.

You don't know how to do d = d + 1; or d++ ?

Also, to make your code readable, please learn to be consistent when you format your code. Inconsistency makes the code difficult to follow and understand.

This article has been dead for over six months. Start a new discussion instead.