Question

In: Computer Science

1 : An array a is defined to be self-referential if for i=0 to a.length-1, a[i]...

1 : An array a is defined to be self-referential if for i=0 to a.length-1, a[i] is the count of the number of times that the value i appears in the array. As the following table indicates, {1, 2, 1, 0} is a self-referential array.

Here are some examples of arrays that are not self-referential:

{2, 0, 0} is not a self-referential array. There are two 0s and no 1s. But unfortunately there is a 2 which contradicts a[2] = 0.

{0} is not a self-referential array because there is one 0, but since a[0] = 0, there has to be no 0s.

{1} is not a self-referential array because there is not a 0 in the array as required by a[0] = 1.

Self-referential arrays are rare. Here are the self-referential arrays for arrays of lengths up to 10 elements:

{1, 2, 1, 0} (see above)
{2, 0, 2, 0} (there are two 0s, no 1s, two 2s and no 3s
{2, 1, 2, 0, 0}  (there are two 0s, one 1, two 2s, no 3s and no 4s)
{3, 2, 1, 1, 0, 0, 0} (there are three 0s, two 1s, one 2, one 3, no 4s, 5s or 6s)
{4, 2, 1, 0, 1, 0, 0, 0} (there are four 0s, two 1s, one 2, no 3s, one 4, and no 5s, 6s, or 7s)
{5, 2, 1, 0, 0, 1, 0, 0, 0} (there are five 0s, two 1s, one 2, no 3s or 4s, one 5, and no 6s, 7s, or 8s)
{6, 2, 1, 0, 0, 0, 1, 0, 0, 0} (there are six 0s, two 1s, one 2, no 3s, 4s, or 5s, one 6, and no 7s, 8s, or 9s)

Write a function named isSelfReferential that returns 1 if its array argument is self-referential, otherwise it returns 0.

If you are programming in Java or C#, the function signature is
   int isSelfReferential(int[ ] a)

If you are programming in C or C++, the function signature is
   int isSelfReferential(int a[ ], int len) where len is the number of elements in the array

Solutions

Expert Solution

We have to just verify the condition whether a[i] is the count that element i appears in the array for that we can keep a count array for 0 to n-1 and then verify whether it matches the count or not in the original array.

The code is

#include <iostream>

using namespace std;
int isSelfReferential(int a[],int len){
int counts[len]={};//counts array to store the counts of integers
for(int i=0;i<len;i++){
if(a[i]<len){
//if a[i] is in range of 0 to n-1 then increment the count
counts[a[i]]++;
}
}
//verifying the result now
for(int i=0;i<len;i++)
{
if(a[i]!=counts[i])
return 0;//if count of i is not a[i]
}
return 1;
}
int main() {
//testing
int a[]={1, 2, 1, 0};
int b[]={5, 2, 1, 0, 0, 1, 0, 0, 0};
int c[]={5, 2, 1, 0, 0, 1, 3, 2, 0};
int d[]={1,2, 2, 1, 0, 0, 1, 0, 0, 0};
cout<<isSelfReferential(a,sizeof(a)/sizeof(int))<<"\n";
cout<<isSelfReferential(b,sizeof(b)/sizeof(int))<<"\n";
cout<<isSelfReferential(c,sizeof(c)/sizeof(int))<<"\n";
cout<<isSelfReferential(d,sizeof(d)/sizeof(int))<<"\n";
}


Related Solutions

Prove that the function defined to be 1 on the Cantor set and 0 on the...
Prove that the function defined to be 1 on the Cantor set and 0 on the complement of the Cantor set is discontinuous at each point of the Cantor set and continuous at every point of the complement of the Cantor set.
Question 2. The following code defines an array size that sums elements of the defined array...
Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++) numbers[i] = i+1; int i =0; while (i<=A){    Total += numbers[i];    i++; }...
f(t) = 1- t 0<t<1 a function f(t) defined on an interval 0 < t <...
f(t) = 1- t 0<t<1 a function f(t) defined on an interval 0 < t < L is given. Find the Fourier cosine and sine series of f and sketch the graphs of the two extensions of f to which these two series converge
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn =...
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn = Fn−1 + Fn−2 for n ≥ 2. Use induciton to prove that F0F1 + F1F2 + · · · + F2n−1 F2n = F^2 2n for all positive integer n.
An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i <...
An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i < j. For example, the array A = [-49, 75, 103, -147, 164, -197, -238, 314, 348, -422], though not sorted in the standard sense, is abs-sorted. Design an algorithm that takes an abs-sorted array A and a number k, and returns a pair of indices of elements in A that sum up to k. For example if k = 167 your algorithm should output...
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 +...
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 + Fn for all n ∈ N ∪ {0}. (1) Make and prove an (if and only if) conjecture about which Fibonacci numbers are multiples of 3. (2) Make a conjecture about which Fibonacci numbers are multiples of 2020. (You do not need to prove your conjecture.) How many base cases would a proof by induction of your conjecture require?
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
Hello, I need to convert this java array into an array list as I am having...
Hello, I need to convert this java array into an array list as I am having trouble please. import java.util.Random; import java.util.Scanner; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int[] data = new int[1000]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println(randomInt); data[count++] = randomInt; System.out.print("Want another random number (Yes / No)? "); choice =...
Polynomial Multiplication by Divide-&-Conquer A degree n-1 polynomial ? (?) =Σ(n-1)(i=0) ???i = ?0 + ?1?...
Polynomial Multiplication by Divide-&-Conquer A degree n-1 polynomial ? (?) =Σ(n-1)(i=0) ???i = ?0 + ?1? + ?2?2 ... + ??−1?n-1 can be represented by an array ?[0. . ? − 1] of its n coefficients. Suppose P(x) and Q(x) are two polynomials of degree n-1, each given by its coefficient array representation. Their product P(x)Q(x) is a polynomial of degree 2(n-1), and hence can be represented by its coefficient array of length 2n-1. The polynomial multiplication problem is to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT