Part A 1. Create the file lab3.cpp. In your main function declare a two-dimensio
ID: 3722570 • Letter: P
Question
Part A 1. Create the file lab3.cpp. In your main function declare a two-dimensional array of size [5x5]. Use conditional statements to generate the values of the array as follows: The main diagonal contains character in all its elements . The upper triangle contains character -in all its elements . The lower triangle contains character' in all its elements 2. Define function printArray that takes a 2D array and displays its contents. From main, call printArray and pass to it the 2D array you created above Note: Refer to defining/passing 2d array syntax. You need to declare the number of columns as global const. Part B 1. Using the list provided in the worksheet, trace the following sequential search algorithm. Record your answers in the tables in the provided worksheet. Please see the example below as a guide. Pseudocode for sequential search of an unordered list: Line Pseudocode Statement 1 function sequentialSearch(a, arraySize, targetValueExplanation / Answer
Part A
#include <iostream>
#include<stdio.h>
using namespace std;
printArray(char arr[][]);
int main() {
int a=0,b=0;;
char arr[5][5];
for(a=0;a<5;a++)
{
for(b=0;b<5;b++)
{
if(a==b)
{
arr[a][b]='*';
}
else if(b>a)
{
arr[a][b]='-';
}
else
{
arr[a][b]='^';
}
}
}
printArray(arr);
return 0;
}
printArray(char arr[][])
{
int i=0,j=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
cout<< arr[i][j] << endl;
}
}
}
........................................................................................................................................................................................
Part B
#include <iostream>
sequentialSearch(int a[],int arraysize,int targetValue);
using namespace std;
int arraysize=0,i=0;
int main() {
int arr[100];
int key;
cout<< "enter the size of array" << endl;
cin>> arraysize;
cout<< "enter" << arraysize << "elemets" <<endl;
for(i=0;i<arraysize;i++)
{
cin>> arr[i];
}
cout<< "enter the value to be search"<< endl;
cin>> key;
sequentialSearch(arr,arraysize,key)
}
sequentialSearch(int a[], int arraySize, int targetValue )
{
int position=-1;
int index=0;
while(index<arraySize && position==-1)
{
if(a[index]=targetValue)
{
position=index;
}
}
if(position==-1)
{
cout<< "target not found" << endl;
}
else
{
cout<< "target found at "<< index << "position" << endl;
}
}
.......................................................................................................................................................................................
Part C
#include <iostream>
binarySearch(int a[],int arraysize,int targetValue);
using namespace std;
int arraysize=0,i=0;
int main() {
int arr[100];
int key;
cout<< "enter the size of array" << endl;
cin>> arraysize;
cout<< "enter" << arraysize << "elemets" <<endl;
for(i=0;i<arraysize;i++)
{
cin>> arr[i];
}
cout<< "enter the value to be search"<< endl;
cin>> key;
binarySearch(arr,arraysize,key)
}
binarySearch(int a[], int arraySize, int targetValue )
{
int position=-1;
int first=0;
int last=arraySize-1;
int mid=0;
while(last>first && position==-1)
{
mid=(first+last)/2;
if(a[mid]==targetValue)
{
position=mid;
}
else if(a[mid]>targetValue)
{
last=mid-1;
}
else if(a[mid]<targetValue)
{
first=mid+1;
}
}
if(position==-1)
{
cout << "target not found";
}
else
{
cout<<"target found at "<< position;
}
}
//please comment if code is not running on your IDE or you have any problem in understanding any logic and please thumbs up my answer :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.