Please help with basic c++ coding!! Write a program: 1. Generate a two dimension
ID: 3669539 • Letter: P
Question
Please help with basic c++ coding!!
Write a program:
1. Generate a two dimensional test vector of integers by using random number generator, integers should be in [0,70]; the vector size should be 4 rows and 5 columns.(4*5)
2. Let the user input number to be searched, use linear search function to search the number in the two dimensional vector
3. The function should accept one two dimensional vector of integers and a number to search for. If the number is found (if there are duplicate numbers, get the first one), print out the number and “Number found!”, and output the rows and columns. If not print out “Number isn’t found!”.
4. You may need iterator to search, keep doing steps above until get negative integers
5. Name your function “LinearSearch”
Write another program:
1. Generate a one dimensional array of integers by using random number generator, integers should be in [0,70]; the array size should be 20
2. Let the user input number to be searched, if you can use binary search in the array? If so, use binary search function to search the number in the array; If not, explain why, fix it and then use binary search function to search the number in the array.
3. The function should accept one array and a number to search for. If the number is found (if there are duplicate numbers, get the first one), print out the number and “Number found!”, and output the original position. If not print out “Number isn’t found!”.
4. You may use data structure for the array, and you may use other pointer or index for array
Explanation / Answer
Solution for the Linear Search..
HOW TO COMIPLE
g++ NameOfProgram.cpp -std=c++11
./a.out
PROGRAM->
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<string>
using namespace std;
string linearSearch(vector<vector<int>> movement,int element) //the space after the int is important
{
int tempRow=0;
int tempCol=0;
int flag=0;
cout<<" You are inside the function...."<<" ";
for (const auto& inner : movement) {
flag=0;
for (const auto& item : inner) {
if(item==element)
{
tempRow=
flag=1;
break;
}
}
}
if(flag==1)
{
return "Number found" ;
}
else
{
return "Number not found";
}
cout << endl;
}
int main()
{
const int rows = 4;
const int columns = 5;
vector< vector<int> > vec;
for (int i = 0; i < rows; i++) {
vector<int> row; // Create an empty row
for (int j = 0; j < columns; j++) {
row.push_back((rand()%70)); // Add an element (column) to the row
}
vec.push_back(row); // Add the row to the main vector
}
for (const auto& inner : vec) {
cout<<" ";
for (const auto& item : inner) {
cout << item << " ";
}
}
int number=0;
cout<<" Enter the number you want to search";
cin>>number;
cout<<linearSearch(vec,number);
cout << endl;
return 0;
}
//Solution for the Binary search
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.