Your second lab assignment will focus on array, sorting, and searching. Please f
ID: 3669063 • Letter: Y
Question
Your second lab assignment will focus on array, sorting, and searching. Please follow class examples to complete the following tasks: Using C/C++, define an integer array with 20 elements; assign the arrays with random numbers between [0, 99] by calling rand(); then define a function and implement selection sort to sort the array (pass the array and its size as parameters from main to the function); print the array before and after sorting; Similar to a, define an integer array with 20 elements; assign the arrays with random numbers between [0, 99]; then define a function and implement bubble sort to sort the array (pass the array and its size as parameters from main to the function); print the array before and after sorting; following a, using C/C++, define a function and implement binary search to search in the sort the array; in main(), define an integer N, assign its value from keyboard (cin), and call the binary search function to search N in the array; print yes if found, and no if not. Using C/C++, define a two dimensional integer array with 5 rows and 5 columns (e.g. n[5][5]). Assign the array with random numbers between 10 and 100 using rand(); then define two array S[5] and A[5] and use loops to calculate the sum and average of the numbers in each column of the two dimensional array, assign the results to S and A, respectively (i.e. calculate the sum and average of the numbers from n[0][0] to n[0][4] and assign it to S[0] and A[0], calculate the sum of the numbers from n[1][0] to n[1][4] and assign it to S[1] and A[1], etc.) Output the results in S array and A array; finally, count how many even numbers of the two dimensional array has and output the result, (need to change the question next time, solution code posted)Explanation / Answer
1.============================================
#include <iostream>
#include<stdlib.h>
using namespace std;
void printArray(int arr[],int size){
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
cout<<" ";
}
int* fillTheArray(int size){
int max = 100;
int min = 10;
int randNum;
int arr[20];
int i=0;
for(i=0;i<size;i++){
randNum = rand()%(max-min + 1) + min;
arr[i] = randNum;
}
return arr;
}
int* copyAnArray(int src[],int srcStart,int dest[],int destStart,int length){
int i=0;
for(i=0;i<length;i++){
dest[destStart] = src[srcStart];
srcStart++;
destStart++;
}
return dest;
}
void swap(int *i, int *j) {
int t = *i;
*i = *j;
*j = t;
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++) //Last i elements are already in place
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
int main(){
int size = 20;
int* arr;
int x;
int result;
int* selectionSortArray = (int *)malloc(sizeof(int)*size);
int* bubbleSortArray = (int *)malloc(sizeof(int)*size);
arr = fillTheArray(size);
copyAnArray(arr,0,selectionSortArray,0,size);
copyAnArray(arr,0,bubbleSortArray,0,size);
cout<<"Array Before Sorting (Selection Sort ): "<<" ";
printArray(selectionSortArray,size);
cout<<" Array After Sorting (Selection Sort ): "<<" ";
selectionSort(selectionSortArray,size);
printArray(selectionSortArray,size);
cout<<" Array Before Sorting (Bubble Sort ): "<<" ";
printArray(bubbleSortArray,size);
cout<<" Array After Sorting (Bubble Sort ): "<<" ";
bubbleSort(bubbleSortArray,size);
printArray(bubbleSortArray,size);
cout<<"Enetr the number to search : "<<" ";
cin>>x;
result = binarySearch(bubbleSortArray,0,size-1,x);
if(result==-1){
cout<<" No"<<" ";
}else{
cout<<" Yes"<<" ";
}
return 0;
}
=================================================================================
2.==============================================================
#include <iostream>
#include <stdlib.h>
using namespace std;
int** generateRandomArray(int row,int col){
int max = 100;
int min = 10;
int randNum;
int i=0;
int j=0;
int **n = (int**)malloc(row*sizeof(int*));
for(i=0;i<row;i++){
n[i] = (int*)malloc(col*sizeof(int));
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
randNum = rand()%(max-min + 1) + min;
n[i][j] = randNum;
}
}
return n;
}
int main()
{
int **n;
int row = 5;
int col = 5;
int i =0;
int j=0;
int S[col];
int A[col];
n= generateRandomArray(row,col);
for(i=0;i<row;i++){
for(j=0;j<col;j++){
S[i]+=n[i][j];
}
A[i]=S[i]/col;
}
for(i=0;i<col;i++){
cout<<"Sum of Column "<<i+1<<S[i]<<" ";
cout<<"Average of Column "<<i+1<<A[i]<<" ";
}
}
==============================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.