Hello everyone, I am a new member of our forum, and I am a beginning programmer.
I have a question which I hope that you guys can help me. ^=^
Here is a program I am programming:
#include <iostream>
#include <stdlib.h>
using namespace std;
// Function prototype
int searchList (int [], int, int) ;
const int number = 5 ;
int main(int argc, const char * argv[])
{
int tests[number] = {87, 75, 98, 100, 82} ;
int results; // Holds the search results.
//Search the array for the value 100.
results = searchList(tests, number, 100) ;
// If searchList returned -1, 100 was not found.
if (results == -1)
cout << "You did not earn 100 points on any test. \n" ;
else
{ // Otherwise results contains the subscript of
// the first 100 found in the array
cout << "You earned 100 points on the test " ;
cout << (results+1) << " .\n" ;
}
system ("PAUSE") ;
return 0 ;
}
int searchList (int list[], int size, int value)
{
int index = 0 ; // Used as a subscript to search array.
int position = -1 ; // Used to record position of search value.
bool found = false ; // Flag to indicate if the value was found.
while (index < size && !found)
{
if (list[index] == value) // If the value is found.
{
found = true ; // Set the flag.
position = index ; // Record the value's subscript.
}
index ++ ; // Go to the next element.
}
return position ;
}
I dont understand why 100 is the parameter of the function searchList(tests, number, 100).
Moreover, what does this statement "index < size && !found" mean?
I am looking forward to receive your reply.
Happy thanksgiving.
"I dont understand why 100 is the parameter of the function searchList(tests, number, 100)." (sic)
By looking at the definition of the routine, the third parameter is used to store the value to be searched for. Here's what I think the routine does: While the value within index is less than the value within size, and the Boolean value within found is false, this occurs: listindex is compared to value. If the two match, found is assigned to true and position is assigned to the value of index. index is then incremented. The condition of the loop is tested again. If the loop doesn't qualify to end, another iteration of the loop is made. Once the loop ends, the routine returns to the calling routine with the value of position.