Solve in C programming use a onedimensional array to solve t

Solve in C programming.

use a one_dimensional array to solve the following problem. read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it\'s not a duplicate of a number already read. Provide for the \"worst case\" in which all 20 numbers are different. use the smallest possible array to solve this problem.

Solution

// C code print distinct elements in array


#include <stdio.h>
#include <string.h>

int main()
{
int i;
int array[20];
int count[100] = {0};

for (i = 0; i < 20; ++i)
{
printf(\"\ Enter element at index %d: \", (i+1) );
scanf(\"%d\",&array[i]);
if (count[array[i]] == 0)
{
count[array[i]]++;
printf(\"distinct element in array: %d\ \",array[i]);
}

}
return 0;
}

/*
time complexity = O(n)
considering the worst case scenario
when all the elements of array are distinct then also
then also the time complexity will be O(n) as we need to take input of all
the 20 elements of the array,
A count array is maintained to check of the value in array is duplicate or not
by checking the index of array[i] in count array.
for this extra space is reuired.
Space complexity = O(n)

*/

/*
output:

Enter element at index 1: 11
distinct element in array: 11

Enter element at index 2: 22
distinct element in array: 22

Enter element at index 3: 32
distinct element in array: 32

Enter element at index 4: 89
distinct element in array: 89

Enter element at index 5: 22

Enter element at index 6: 43
distinct element in array: 43

Enter element at index 7: 22

Enter element at index 8: 67
distinct element in array: 67

Enter element at index 9: 67

Enter element at index 10: 11

Enter element at index 11: 32

Enter element at index 12: 33
distinct element in array: 33

Enter element at index 13: 67

Enter element at index 14: 65
distinct element in array: 65

Enter element at index 15: 45
distinct element in array: 45

Enter element at index 16: 44
distinct element in array: 44

Enter element at index 17: 33

Enter element at index 18: 23
distinct element in array: 23

Enter element at index 19: 67

Enter element at index 20: 67
*/

Solve in C programming. use a one_dimensional array to solve the following problem. read in 20 numbers, each of which is between 10 and 100, inclusive. As each
Solve in C programming. use a one_dimensional array to solve the following problem. read in 20 numbers, each of which is between 10 and 100, inclusive. As each

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site