Will need 3 different C programs, can be commented out in the c file unless it n
ID: 3711556 • Letter: W
Question
Will need 3 different C programs, can be commented out in the c file unless it needs to be activated.
Searching and Sorting The auxiliary pointer aux will be declared and initialized to head. Node aux = head; The end condition for the while loop (a while loop is recommended) should be aux->next != NULL ; The update of aux should simply be: auxauz->next; The comparison can be made in whatever manner is required. This includes the use of = and stremp OK, here is a simple problem for you to do now during the lab. Given the array of strings beatles[whose contents include "john", "paul" "george" and "ringo", build a search function called search) that will search the list for names to see whether they were one of the Beatles or not. Start with char beatles[4]-"john", "paul", "george", "ringo") Now write a search function to search this array and see whether "george" and "ringo" are in that list. Then check to see whether "mick" and "rce are on that list.Explanation / Answer
Bubble sort
#include<bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf(" ");
}
int main()
{
int arr[] = {2,6,8,4,7,9,3,1,5,0};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Selection Sort
#include<bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
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]);
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf(" ");
}
int main()
{
int arr[] = {4,1,9,5,7,3,8,0,6,2};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
bool search(char* beatles[], const string &str)
{
if(str=="john" || str== "paul" || str=="ringo" || str=="george")
return true;
else
return false;
}
int main()
{
char * beatles[]={"john","paul","ringo","george"};
cout << "Enter the name you want to search ";
string str;
cin >> str;
bool a= search(beatles, str);
if(a)
cout << "He is one of the Beatles";
else
cout << "Error 404!! Beatles not found";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.