1. Write a method that determines if a string has all unique characters. 2. Cons
ID: 3665078 • Letter: 1
Question
1. Write a method that determines if a string has all unique characters.
2. Consider the following data from Forbes:
Create a one dimensional array holding the cheapest ticket price data. Compute the following:
1. The average cheapest ticket price
2. The maximum
3. The minimum
4. The median – half the values fall below the median; half above
How would you do these same things if you wanted to keep the name of the tour associated with the ticket data?
Most Expensive Tours Tour One Direction The Rolling Stones Beyoncé Eagles Pink Paul McCartney Fleetwood Mac Depeche Mode Bruno Mars Black Sabbath Justin Timberlake Justin Bieber Taylor Swift & Ed Sheeran John Mayer Dave Matthews Band Lil Wayne Rush Bon Jovi Matchbox 20 Avg Price $674.23 $637.50 $358.97 $312.25 $299.90 $273.73 $270.29 $263.30 $250.56 $227.95 $221.88 $214.12 Cheapest Ticket/Venue $54/First Midwest Amphitheatre $159/TD Garden $35/Chesapeake Energy Center $57/Rexall Place $52/Verizon Arena-Little Rock $49/Safeco Field $59/BB&T; Center $33/DTE Energy Music $48/Scottrade Center $33/First Midwest Bank Amphitheatre $40/Ford Field $14/Scotiabank Place $58/Bridgestone Arena $37/Desert Sky Pavilion $47/DTE Energy Center $33/First Niagara Pavilion $29/Time Warner Cable Pavilion $25/MetLife Stadium $25/Farm Bureau Live $214.03 $208.09 $202.53 $193.51 $192.04 $191.68 $182.69Explanation / Answer
#include <stdio.h>
/* This function returns median of ar1[] and ar2[].
Assumptions in this function:
Both ar1[] and ar2[] are sorted arrays
Both have n elements */
int getMedian(int ar1[], int ar2[], int n)
{
int i = 0; /* Current index of i/p array ar1[] */
int j = 0; /* Current index of i/p array ar2[] */
int count;
int m1 = -1, m2 = -1;
/* Since there are 2n elements, median will be average
of elements at index n-1 and n in the array obtained after
merging ar1 and ar2 */
for (count = 0; count <= n; count++)
{
/*Below is to handle case where all elements of ar1[] are
smaller than smallest(or first) element of ar2[]*/
if (i == n)
{
m1 = m2;
m2 = ar2[0];
break;
}
/*Below is to handle case where all elements of ar2[] are
smaller than smallest(or first) element of ar1[]*/
else if (j == n)
{
m1 = m2;
m2 = ar1[0];
break;
}
if (ar1[i] < ar2[j])
{
m1 = m2; /* Store the prev median */
m2 = ar1[i];
i++;
}
else
{
m1 = m2; /* Store the prev median */
m2 = ar2[j];
j++;
}
}
return (m1 + m2)/2;
}
/* Driver program to test above function */
int main()
{
int ar1[] = {1, 12, 15, 26, 38};
int ar2[] = {2, 13, 17, 30, 45};
int n1 = sizeof(ar1)/sizeof(ar1[0]);
int n2 = sizeof(ar2)/sizeof(ar2[0]);
if (n1 == n2)
printf("Median is %d", getMedian(ar1, ar2, n1));
else
printf("Doesn't work for arrays of unequal size");
getchar();
return 0;
}
#include <stdio.h>
/* This function returns median of ar1[] and ar2[].
Assumptions in this function:
Both ar1[] and ar2[] are sorted arrays
Both have n elements */
int getMedian(int ar1[], int ar2[], int n)
{
int i = 0; /* Current index of i/p array ar1[] */
int j = 0; /* Current index of i/p array ar2[] */
int count;
int m1 = -1, m2 = -1;
/* Since there are 2n elements, median will be average
of elements at index n-1 and n in the array obtained after
merging ar1 and ar2 */
for (count = 0; count <= n; count++)
{
/*Below is to handle case where all elements of ar1[] are
smaller than smallest(or first) element of ar2[]*/
if (i == n)
{
m1 = m2;
m2 = ar2[0];
break;
}
/*Below is to handle case where all elements of ar2[] are
smaller than smallest(or first) element of ar1[]*/
else if (j == n)
{
m1 = m2;
m2 = ar1[0];
break;
}
if (ar1[i] < ar2[j])
{
m1 = m2; /* Store the prev median */
m2 = ar1[i];
i++;
}
else
{
m1 = m2; /* Store the prev median */
m2 = ar2[j];
j++;
}
}
return (m1 + m2)/2;
}
/* Driver program to test above function */
int main()
{
int ar1[] = {1, 12, 15, 26, 38};
int ar2[] = {2, 13, 17, 30, 45};
int n1 = sizeof(ar1)/sizeof(ar1[0]);
int n2 = sizeof(ar2)/sizeof(ar2[0]);
if (n1 == n2)
printf("Median is %d", getMedian(ar1, ar2, n1));
else
printf("Doesn't work for arrays of unequal size");
getchar();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.