Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Both projects must have main function for testing. Project 1 iefner seuch n non-

ID: 3725944 • Letter: B

Question

Both projects must have main function for testing. Project 1 iefner seuch n non-recursive and recursive way. Test your functions in a main function Project 2 (This is not an array project) 5.26 Perfecr Numbers) An integer number is said to be a perfecr number if its factors, including (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 . 1+2+3. Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than 1000.

Explanation / Answer

#include <stdio.h>

int iterative_linear_search(int *arr, int n, int search)

{

int i = 0;

while (i < n)

{

if (arr[i] == search)

{

return i;

}

i++;

}

return -1;

}

int recursive_linear_search(int *arr, int l, int n, int search)

{

if (n < l)

return -1;

else if (arr[l] == search)

return l;

return recursive_linear_search(arr, l + 1, n, search);

}

int isPerfect(int n)

{

int i = 1, sum = 0;

while (i < n)

{

if (n % i == 0)

sum += i;

i++;

}

if (sum == n)

return 1;

else

return 0;

}

void swap(int *a, int *b)

{

int temp = *a;

*a = *b;

*b = temp;

}

void bubble_sort(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]);

}

}

}

}

int binary_search(int *arr, int l, int r, int search)

{

bubble_sort(arr, r);

while (l <= r)

{

int mid = l + (r - l) / 2;

if (arr[mid] == search)

return mid;

else if (arr[mid] < search)

l = mid + 1;

else

r = mid - 1;

}

return -1;

}

int checkBarcode(int *arr)

{

int i, oddSum = 0, evenSum = 0;

for (i = 0; i < 11; i++)

{

if (i % 2 == 0)

oddSum += arr[i];

else

evenSum += arr[i];

}

int sum = oddSum * 3 + evenSum;

int checkDigit = sum % 10;

if (checkDigit != 0)

checkDigit = 10 - checkDigit;

if (arr[11] == checkDigit)

return 1;

else

return 0;

}

int main()

{

int arr[] = {0, 5, 1, 0, 0, 0, 1, 3, 8, 1, 0, 1}, n = 12;

int i = 0;

printf("Array elements: ");

for (i = 0; i < n; i++)

{

printf("%d ", arr[i]);

}

printf(" ");

printf("After performing iterative linear search element 3 found at: %d ", iterative_linear_search(arr, n, 3));

printf("After performing recursive linear search element 3 found at: %d ", recursive_linear_search(arr, 0, n, 3));

if(checkBarcode(arr)!=-1){

printf(" Taken barcode is valid ");

}

else{

printf("Invalid barcode");

}

int ind = binary_search(arr, 0, 12, 3);

printf("After Performing bubble sort: ");

for (i = 0; i < n; i++)

{

printf("%d ", arr[i]);

}

if(ind!=-1)

printf(" After performing binary search element 3 found at: %d", ind);

else

printf(" Search element not found. ");

printf(" Printing Perfect numbers from 1 to 1000: ");

for(i=1;i<=1000; i++){

if(isPerfect(i))

printf("%d ", i);

}

printf(" ");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote