Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks
#include
#include
using namespace std;
void dynAlloc(int size, int *&arr);
void displayArray(int *arr, int n);
void insertionSort(int *arr, int n, int *temp);
void linear_search(int *arr, int n, int key);
void binary_search(int *arr, int n, int key);
int main() {
const int n = 50;
int *arr, *temp;
dynAlloc(n, arr);
dynAlloc(n, temp);
// randomly generating 50 numbers
for (int i = 0; i < n; i++)
arr[i] = (rand() % 100) + 1;
cout << "\nHere is the unsorted array" << endl;
displayArray(arr, n);
insertionSort(arr, n, temp);
cout << "\nHere is the sorted array" << endl;
displayArray(temp, n);
int key;
cout << "\nEnter an integer to search for" << endl;
cin >> key;
while (key != -1) {
linear_search(arr, n, key);
binary_search(temp, n, key);
cout << "\nEnter an integer to search for" << endl;
cin >> key;
}
cout << "Thanks and have a nice day!" << endl;
delete arr;
delete temp;
return 0;
}
void dynAlloc(int size, int *&arr) { arr = new int[size]; }
void displayArray(int *arr, int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
if ((i + 1) % 5 == 0)
cout << endl;
}
cout << endl;
}
void insertionSort(int *arr, int n, int *temp) {
for (int i = 0; i < n; i++)
temp[i] = arr[i];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (temp[i] > temp[j]) {
int t = temp[i];
temp[i] = temp[j];
temp[j] = t;
}
}
}
}
void linear_search(int *arr, int n, int key) {
cout << "invoking linear search" << endl;
int i;
for (i = 0; i < n; i++) {
if (arr[i] == key) {
cout << "target found - element # " << i << endl;
break;
}
}
cout << "number of iterations = " << i << endl;
}
void binary_search(int *arr, int n, int key) {
cout << "invoking binary search" << endl;
int l = 0;
int r = n - 1;
int i = 0;
bool isFound = false;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == key) {
cout << "target found - element # " << m << endl;
isFound = true;
break;
} else if (arr[m] < key)
l = m + 1;
else
r = m - 1;
i++;
}
if (!isFound)
cout << "target not found" << endl;
cout << "number of iterations = " << i << endl;
}
Display of numbers 1:
Here is the unsorted array
8 50 74 59 31
73 45 79 24 10
41 66 93 43 88
4 28 30 41 13
4 70 10 58 61
34 100 79 17 36
98 27 13 68 11
34 80 50 80 22
68 73 94 37 86
46 29 92 95 58
Here is the sorted array
4 4 8 10 10
11 13 13 17 22
24 27 28 29 30
31 34 34 36 37
41 41 43 45 46
50 50 58 58 59
61 66 68 68 70
73 73 74 79 79
80 80 86 88 92
93 94 95 98 100
Display of numbers 2:
Here is the unsorted array
8 50 74 59 31
73 45 79 24 10
41 66 93 43 88
4 28 30 41 13
4 70 10 58 61
34 100 79 17 36
98 27 13 68 11
34 80 50 80 22
68 73 94 37 86
46 29 92 95 58
Here is the sorted array
4 4 8 10 10
11 13 13 17 22
24 27 28 29 30
31 34 34 36 37
41 41 43 45 46
50 50 58 58 59
61 66 68 68 70
73 73 74 79 79
80 80 86 88 92
93 94 95 98 100
Enter an integer to search for
Code - You have to add srand(time(0)); before storing
random number in array because what srand(time(0));
does is it Uses current time for generating random number , so
every time you run you will generate new random number , I have
this in code you can try running the code.
#include <iostream>
using namespace std;
void dynAlloc(int size, int *&arr);
void displayArray(int *arr, int n);
void insertionSort(int *arr, int n, int *temp);
void linear_search(int *arr, int n, int key);
void binary_search(int *arr, int n, int key);
int main() {
const int n = 50;
int *arr, *temp;
dynAlloc(n, arr);
dynAlloc(n, temp);
// randomly generating 50 numbers
//Usin current time for generating random time , so every time you
run you will generate new random number
srand(time(0));
for (int i = 0; i < n; i++)
arr[i] = (rand() % 100) + 1;
cout << "\nHere is the unsorted array" << endl;
displayArray(arr, n);
insertionSort(arr, n, temp);
cout << "\nHere is the sorted array" << endl;
displayArray(temp, n);
int key;
cout << "\nEnter an integer to search for" << endl;
cin >> key;
while (key != -1) {
linear_search(arr, n, key);
binary_search(temp, n, key);
cout << "\nEnter an integer to search for" << endl;
cin >> key;
}
cout << "Thanks and have a nice day!" << endl;
delete arr;
delete temp;
return 0;
}
void dynAlloc(int size, int *&arr) { arr = new int[size]; }
void displayArray(int *arr, int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
if ((i + 1) % 5 == 0)
cout << endl;
}
cout << endl;
}
void insertionSort(int *arr, int n, int *temp) {
for (int i = 0; i < n; i++)
temp[i] = arr[i];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (temp[i] > temp[j]) {
int t = temp[i];
temp[i] = temp[j];
temp[j] = t;
}
}
}
}
void linear_search(int *arr, int n, int key) {
cout << "invoking linear search" << endl;
int i;
for (i = 0; i < n; i++) {
if (arr[i] == key) {
cout << "target found - element # " << i << endl;
break;
}
}
cout << "number of iterations = " << i << endl;
}
void binary_search(int *arr, int n, int key) {
cout << "invoking binary search" << endl;
int l = 0;
int r = n - 1;
int i = 0;
bool isFound = false;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == key) {
cout << "target found - element # " << m << endl;
isFound = true;
break;
} else if (arr[m] < key)
l = m + 1;
else
r = m - 1;
i++;
}
if (!isFound)
cout << "target not found" << endl;
cout << "number of iterations = " << i << endl;
}
Screenshots -
First run -
Second Run -
Pls do give a like , thank you . If you have any question pls ask in comment section
Hello, I need help with my code. The code needs to display random number from 1...
I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...
Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner; public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1) bubbleSort(array); if (ch==2) insertion(array); if (ch==3) Selection(array); if (ch==4) break; print(array); System.out.println(); } }...
Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...
Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....
I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...
My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...
C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) { // Add your code here. string line; getline(inFile, line); int sp = 0; for (int i = 0; i < line.size(); i++) { if (line[i] == ' ') sp++; } sp++; return sp; } int measureLines(ifstream& inFile) { // Add your code here. string line; int n = 0; while (!inFile.eof()) { getline(inFile,...
I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...
I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....