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

Write a program that asks the user to enter 1000 integers to be stored in an arr

ID: 3835279 • Letter: W

Question

Write a program that asks the user to enter 1000 integers to be stored in an array called "numbers". Since the same integer might occur (exist) in the array multiple times, your program needs to fill a second array, called "Isolate" that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is; the number of times the element is repeated in the first array). You can use any sorting algorithm you want. (You must use at least one function in your program)

Explanation / Answer

Code:
#include <stdio.h>

/*
Program Name: sort_by_freq.c
Program to sort given elements by their occurences
*/

/* function declaration */
void sort(int[], int);
void count_occur(int[], int, int[], int);
void sort_by_occur(int[], int[], int);

int main()
{
   int n, un = 0;
   int numbers[1000], isolate[1000], freq[1000];

   printf(" Enter how many elements you want ? ");
   scanf("%d", &n);
   for(int i = 0; i < n; i++) {
       scanf("%d", &numbers[i]);
   }
   for(int i = 0; i < n; i++) {
       int j = 0;
       for(; j < un; j++)
           if(numbers[i] == isolate[j])
               break;
       if(j == un) {
           isolate[j] = numbers[i];
           un++;
       }
   }
   count_occur(numbers, n, isolate, un);

return 0;
}

/* count the occurences */
void count_occur(int numbers[], int n, int isolate[], int un)
{
   int count[1000];
   int counter;
   /* Both numbers and isolate arrays sorted */
   sort(numbers, n);
   sort(isolate, un);

   int k = 0;
   for(int i = 0; i < un; i++){
       counter = 0;
       for(int j = 0; j < n; j++){
           if(isolate[i] == numbers[j]){
               counter++;
           }
           if(numbers[j] > isolate[i])
               break;
       }
       if(counter > 0)
           count[k++] = counter;
   }
   sort_by_occur(isolate, count, k);

   printf(" Array sorting based on occurences");
   printf(" ---------------------------------");
printf(" IsolateArray Occurences");
for(int i = 0; i < k; i++)
printf(" %5d %5d", isolate[i], count[i]);
printf(" ");
}

/* function to sort by occurences */
void sort_by_occur(int isolate[], int count[], int size)
{
   int temp1, temp2;
  
   for(int i = 0; i < size; i++)
for(int j = 0; j < size-1; j++)
           if(count[j] > count[j+1]){
               temp1 = count[j];
               count[j]= count[j+1];
               count[j+1] = temp1;
          
               temp2 = isolate[j];
               isolate[j] = isolate[j+1];
               isolate[j+1] = temp2;  
           }
}


/* sort the given array */
void sort(int arr[], int size)
{
   int temp;

   for(int i = 0; i < size; i++)
       for(int j = 0; j < size-1; j++)
           if(arr[j] > arr[j+1]){
               temp = arr[j];
               arr[j] = arr[j+1];
               arr[j+1] = temp;
           }
}

Execution and output:
186590cb0725:C bonkv$ ./a.out

Enter how many elements you want ? 20
1
2
3
4
5
5
4
3
2
1
10
20
30
40
40
100
200
300
400
99

Array sorting based on occurences
---------------------------------
IsolateArray   Occurences
10   1
20   1
30   1
99   1
100   1
200   1
300   1
400   1
1   2
2   2
3   2
4   2
5   2
40   2

186590cb0725:C bonkv$ ./a.out

Enter how many elements you want ? 5
10
10
10
20
30

Array sorting based on occurences
---------------------------------
IsolateArray   Occurences
20   1
30   1
10   3

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