Can anyone help me about this program

Hello, everyone, I am new in learning C++. My professor assigns me to this. But I really have no ideas, please help. Thank you the one who help me.




Modify main() to generate ten values of d randomly in a for loop. d should be between 0 and 49.


/*
* TestSrch.cpp
*
* Created on: Nov 7, 2012
* Author: Sam
*/

#include <iostream>
using namespace std;

int linearSearch(int , int );
void testSrch(int, int );
int a[14]={3,7,4,2,11,43,79,15,20,7,11,13,4,40};// global array a
int main(){
int d = 13,aLength = 14;
testSrch(d,aLength);
d=33;
testSrch(d,aLength);
system("PAUSE");
return 0;
}
void testSrch(int d, int aLength){
int srchResult;
srchResult=linearSearch(d,aLength);
if (srchResult<aLength)
{ cout<<"Value "<<d;
cout<<"\nIs at array element "<<srchResult<<endl<<endl;
}
else
cout<<"there is no match for search argument:"<<d<<endl;
}
int linearSearch(int v, int len){
//precondition: array a is a global array of length len, that has already been filled.
//v is the search argument.
//postcondition: returns the position of the first occurrence of search arg.v
//in array a. if no match, returns n.
int aSub=0;
for(;aSub<len ;aSub++){

if(a[aSub] == v){
break;
}
}
return aSub;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
 
int linearSearch(int , int );
void testSrch(int, int );

int a[14]={3,7,4,2,11,43,79,15,20,7,11,13,4,40};// global array a

int main(){
   const int aLength = 14;
   const int testCount = 10;
   int d;

   srand( time( 0 ) );

   for ( int i = 0; i < testCount; i++ )
   {
      d = rand() % 50;
      testSrch(d,aLength);
   }

   system("PAUSE");

   return 0;
}
Topic archived. No new replies allowed.