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

For each problems segment given below, do the following: Create an algorithm to

ID: 655366 • Letter: F

Question

For each problems segment given below, do the following:

Create an algorithm to solve the problem

Identify the factors that would influence the running time, and which can be known before the algorithm or code is executed. Assign names (such as n) to each factor.

Identify the operations that must be counted. You need not count every statement separately. If a group of statements always executes together, treat the group as a single unit. If a method is called, and you do not know the running time of that method, count it as a single operation.

Count the operations performed by the algorithm or code. Express the count as a function of the factors you identified in Step 2. If the count cannot be expressed as a simple function of those factors, define the bounds that can be placed on the count: the best case (lower bound) and worst case (upper bound).

Determine what the Best Case Inputs are, and the Worst Case Inputs are, and the efficiency of your implementation

Transform your count formula into big-O notation by:

Taking the efficiency with worst case input,

Dropping insignificant terms.

Dropping constant coefficients.

a. Determine if 2 arrays contain the same elements
b. Counting total number characters that have a duplicate within a string (i.e. "gigi the gato" would result in 7 (g x 3 + i x 2 + t x 2)
c. Finding an empty row in a 2-D array where empty is defined as an element with a 0 entry.

Explanation / Answer

traverse the list for i= 0 to n-1 elements
{
check for sign of A[abs(A[i])] ;
if positive then
make it negative by A[abs(A[i])]=-A[abs(A[i])];
else // i.e., A[abs(A[i])] is negative
this element (ith element of list) is a repetition
}

implementation-

# include <stdio.h>

# include <stdlib.h>

# define NO_OF_CHARS 256

/* Fills count array with frequency of characters */

void fillCharCounts(char *str, int *count)

{

int i;

for (i = 0; *(str+i); i++)

count[*(str+i)]++;

}

/* Print duplicates present in the passed string */

void printDups(char *str)

{

// Create an array of size 256 and fill count of every character in it

int *count = (int *)calloc(NO_OF_CHARS, sizeof(int));

fillCharCounts(str, count);

// Print characters having count more than 0

int i;

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

if(count[i] > 1)

printf("%c, count = %d ", i, count[i]);

free(count);

}

/* Driver program to test to pront printDups*/

int main()

{

char str[] = "test string";

printDups(str);

getchar();

return 0;

}