#include <iostream> #include <fstream> using namespace std; //------------------
ID: 3767891 • Letter: #
Question
#include <iostream>
#include <fstream>
using namespace std;
//-------------------------------------------------------------------
// Function Prototypes
//-------------------------------------------------------------------
//<write prototype for search function>
int linear_search ( int array [300] [2] , int key )
{
int i;
for ( i = 0; i < 300;i++ )
{
if ( array [ i ] [ 0 ] == key )
return i;
}
return -1;
}
/********************************************************************
Function: main
Description: Program entry/exit point, reads data file, prompts for inputs,
displays results
Parameters: none
Returns: 0
*********************************************************************/
int main ( )
{
int student; // input ID
int index;
int run_data [ 300 ] [ 2 ] = { 0 };
int num = 0; //number of students read
bool done = false; //flag to end main loop
ifstream fin;
fin.open ( "runtimes.txt" );
if ( !fin )
{
cout << "Error opening data file. Exiting." << endl;
return -1;
}
while ( num < 300 && fin >> run_data [ num ] [ 0 ] )
{
fin >> run_data [ num ] [ 1 ];
num++;
}
fin.close ( );
cout << "Enter student ID: ";
while ( !done && cin >> student )
{
// Test for end of the program
if ( student == 0 )
{
cout << "End of Program!" << endl;
done = true;
}
else
{
// Call search function to find student index
int index;
index = searchIntegers ( run_data, num);
// Display error if not found, otherwise ID and runtime
cout << "Error";
if ( index == -1 )
cout << "Student not found!" << endl;
else
cout << "Student: " << student << " had runtime of: "
<< run_data [ index ] [ 1 ] << " seconds" << endl;
// prompt for another student
cout << "Enter student ID: ";
}
}
return 0;
}
Explanation / Answer
Small bug:
#include <iostream>
#include <fstream>
using namespace std;
//-------------------------------------------------------------------
// Function Prototypes
//-------------------------------------------------------------------
//<write prototype for search function>
int linear_search ( int array [300] [2] , int key )
{
int i;
for ( i = 0; i < 300;i++ )
{
if ( array [ i ] [ 0 ] == key )
return i;
}
return -1;
}
/********************************************************************
Function: main
Description: Program entry/exit point, reads data file, prompts for inputs,
displays results
Parameters: none
Returns: 0
*********************************************************************/
int main ( )
{
int student; // input ID
int index;
int run_data [ 300 ] [ 2 ] = { 0 };
int num = 0; //number of students read
bool done = false; //flag to end main loop
ifstream fin;
fin.open ( "runtimes.txt" );
if ( !fin )
{
cout << "Error opening data file. Exiting." << endl;
return -1;
}
while ( num < 300 && fin >> run_data [ num ] [ 0 ] )
{
fin >> run_data [ num ] [ 1 ];
num++;
}
fin.close ( );
cout << "Enter student ID: ";
while ( !done && cin >> student )
{
// Test for end of the program
if ( student == 0 )
{
cout << "End of Program!" << endl;
done = true;
}
else
{
// Call search function to find student index
int index;
index = linear_search ( run_data, student);
// Display error if not found, otherwise ID and runtime
cout << "Error";
if ( index == -1 )
cout << "Student not found!" << endl;
else
cout << "Student: " << student << " had runtime of: "
<< run_data [ index ] [ 1 ] << " seconds" << endl;
// prompt for another student
cout << "Enter student ID: ";
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.