QUESTION 1
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int n=0, key=0, searches=0;
int main()
{
cout << "Enter the number of random integers you want: ";
cin >> n;
cout << "Enter the number you want to search for (max 200): ";
cin >> key;
srand(time(0));
const int arr_len = n;
int num_list[arr_len] = {0};
for (int i=0; i<n; i++){
int sentinel = 1, random_num;
while (sentinel == 1){
random_num = 1 + rand()%200;
int checklist = 0;
for (int b=0; b<i; b++){
if (num_list[b] == random_num){
checklist++;
}
}
if (checklist == 0){
sentinel--;
}
}
num_list[i] = random_num;
for (int i=0; i<n; i++){
searches++;
if (num_list[i] == key){
cout << "Your key was found after " << searches << " iterations. ";
return 0;
}
}
cout << "Your key was not found in the number list.";
return 0;
}
QUESTION 2
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int n=0, key=0, comps=0;
int main()
{
cout << "Enter the number of random integers you want: ";
cin >> n;
srand(time(0));
const int arr_len = n;
int num_list[arr_len] = {0};
for (int i=0; i<n; i++){
int sentinel = 1, random_num;
while (sentinel == 1){
random_num = 1 + rand()%200;
int checklist = 0;
for (int b=0; b<i; b++){
if (num_list[b] == random_num){
checklist++;
}
}
if (checklist == 0){
sentinel--;
}
}
num_list[i] = random_num;
for (int i=1; i<n; i++){
int sentinel = i;
while (sentinel != 0 && num_list[sentinel] > num_list[sentinel-1]){
int initial_forward = num_list[sentinel];
int initial_backward = num_list[sentinel-1];
num_list[sentinel-1] = initial_forward;
num_list[sentinel] = initial_backward;
comps++;
sentinel--;
}
cout << "Array: ";
for (int i=0; i<n; i++){
cout << num_list[i] << " ";
}
cout << endl << "The total number of comparisons made is " << comps << ".";
return 0;
}
QUESTION 3
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int n=0, key=0, swaps=0;
int main()
{
cout << "Enter the number of random integers you want: ";
cin >> n;
srand(time(0));
const int arr_len = n;
int num_list[arr_len] = {0};
for (int i=0; i<n; i++){
int sentinel = 1, random_num;
while (sentinel == 1){
random_num = 1 + rand()%200;
int checklist = 0;
for (int b=0; b<i; b++){
if (num_list[b] == random_num){
checklist++;
}
}
if (checklist == 0){
sentinel--;
}
}
num_list[i] = random_num;
for (int i=0; i<n; i++){
int temp_smallindex = i, temp_smallestnum = num_list[i];
for (int b=i+1; b<n; b++){
if (num_list[b] < temp_smallestnum){
temp_smallindex = b;
temp_smallestnum = num_list[b];
}
}
if (temp_smallindex != i){
int smallest_num = num_list[i];
num_list[i] = temp_smallestnum;
num_list[temp_smallindex] = smallest_num;
swaps++;
}
cout << "Iteration " << i+1 << ": ";
for (int a=0; a<n; a++){
if (a == i || a == temp_smallindex){
cout << "[" << num_list[a] << "]" << " ";
}else{
cout << num_list[a] << " ";
}
}
cout << endl << endl;
cout << "Array: ";
for (int i=0; i<n; i++){
cout << num_list[i] << " ";
}
cout << endl << "The total number of comparisons made is " << swaps << ".";
return 0;
}