Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I got this problem statement from a programming puzzle book. I am able to find out the horizontal palindromes, but only if the entire row is a palindrome.
How can I fulfill this? Also how can be diagonal palindromes acquired?
A pseudo code is okay too, I just need the basic logic behind this. I will perform the rest.
Thank You.
The trick behind finding the horizontal palindromes is the take an entire row and then split it into various strings. Once that is done, you need to check if that string is a palindrome. For vertical strings, you need to do the same thing for the columns.
Now for the diagonal ones, you need to start from a point at the edge and then move forward diagonally (+[1][1]) for going to the bottom right until you reach the end. Now keep doing for every tactical point of every edge which will help you get all the diagonal strings, the next thing you need to do is to split these strings and check if each of these short strings are a palindrome or not.
This would come under dynamic programming most likely. Although I am confused it might come under greedy approach as well. I'll confirm it once with my professor.
Here is the code I did back when I was trying to solve the same thing -
#define PALLEN 2
#include <stdio.h>
#include <string.h>
int a[10][10];
/*int a[5][5] = {
{ 1, 2, 1, 3, 5 } ,
{ 4, 5, 6, 7, 4 } ,
{ 4, 5, 5, 4, 1 } ,
{ 1, 9, 2, 1, 4 } ,
{ 1, 9, 4, 1, 5 }
};*/
int n=0;
void checkPalindrome(char*);
void diagonalPal();
void stringSpliter(char*);
int main() {
int i, j, k, l, x;
int c = 0;
int jmp;
int ptr = 0;
int diag;
char recycler[20];
char diaglist[25];
char revdiaglist[25];
system("cls");
printf("\nEnter the dimension (n) of this square matrix i.e. (n*n) - ");
scanf("%d", &n);
printf("\nNow enter the elements for this %d*%d matrix - ", n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("-%d-", a[i][j]);
}
printf("\n");
}
printf("\nHorizontal Palindromes");
for (i = 0; i < n; i++) {
for (j = n-1, k = PALLEN; j > 0; j--, k++) {
while (c < j) {
jmp = c;
memset(recycler, 0, 20);
ptr = 0;
for (l = 0; l < k; l++) {
recycler[ptr] = a[i][jmp]; //0,0 -- 0,1
ptr++;
jmp++;
}
checkPalindrome(recycler);
c++;
}
c = 0;
}
}
printf("\n\nVertical Palindromes");
for (i = 0; i < n; i++) {
for (j = n-1, k = PALLEN; j > 0; j--, k++) {
while (c < j) {
jmp = c;
memset(recycler, 0, 20);
ptr = 0;
for (l = 0; l < k; l++) {
recycler[ptr] = a[jmp][i]; //0,0-- 1,0
ptr++;
jmp++;
}
checkPalindrome(recycler);
c++;
}
c = 0;
}
}
printf("\n\nDiagonal Palindromes");
diagonalPal();
}
void stringSpliter(char *a){
int i,j,k,ptr,jmp,c=0,l;
int len;
len = strlen(a);
char recycler[20];
for (j = len-1, k = PALLEN; j > 0; j--, k++) {
while (c < j) {
jmp = c;
memset(recycler, 0, 20);
ptr = 0;
for (l = 0; l < k; l++) {
recycler[ptr] = a[jmp]; //0,0 -- 0,1
ptr++;
jmp++;
}
checkPalindrome(recycler);
c++;
}
c = 0;
}
}
void diagonalPal(){
int i, x=0, j, k, ptr=0;
char diagrecycler[20];
for(i = 0; i < n; i++){
memset(diagrecycler, 0, 25);
ptr = 0;
for(j = i, k = 0; j < n, k < n; j++, k++){
diagrecycler[ptr++] = a[j][k];
}
stringSpliter(diagrecycler);
}
for(i = 1; i < n; i++){
memset(diagrecycler, 0, 25);
ptr = 0;
for(j = 0, k = i; j < n, k < n ;j++, k++){
diagrecycler[ptr++] = a[j][k];
}
stringSpliter(diagrecycler);
}
}
void checkPalindrome(char *string){
int isPalindrome = 1, i=0;
char rev[20];
strcpy(rev, string);
strrev(rev);
isPalindrome = strcmp(rev, string);
if(isPalindrome == 0){
printf("\n");
while(string[i]!='\0') printf("%d", string[i++]);
}
}
// Output
/*Enter the dimension (n) of this square matrix i.e. (n*n) - 4
Now enter the elements for this 4*4 matrix - 1 2 3 4
5 2 1 6
8 1 1 8
9 5 3 2
-1--2--3--4-
-5--2--1--6-
-8--1--1--8-
-9--5--3--2-
Horizontal Palindromes
11
8118
Vertical Palindromes
22
11
3113
Diagonal Palindromes
121
212
G:\Code snippets\C programmes>*/
Related
I have coding problem to write concentric square matrix (biggest number is in the middle) For example user needs to write an matrix For example:
5 5 5 5 5
5 6 6 6 5
5 6 7 6 5
5 6 6 6 5
5 5 5 5 5
My program has to output "Yes" because this is, by my program's rules, a concentric square matrix.
5 5 5 5 5
5 6 6 6 5
5 6 7 8 5
5 6 6 6 5
5 5 5 5 5
This is not a concentric square matrix because 8 is in 4th column and 3rd row.
This is my code:
#include <stdio.h>
int main() {
int mat[100][100];
int i,j;
int n;
scanf("%d",&n);
printf("Unesite matricu; ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat[i][j]);
}
}
}
I don't know how to do the rest of it so if someone can help me, I would be happy :))
Comment::
I forgot to say that only odd numbers can be the dimension of the matrix (1,3,11,27). The only final output of the program has to be "YES (if the matrix is a concentric square matrix) or "NO" (if it's not). I know how to make a concentric square matrix when the user inputs a number (for example, 4) and the matrix has 2*n-1 dimensions. And through the loops, the program automatically makes the matrix (if you know what I mean). But for my matrix, the user has to input all the elements of the matrix and the program has to check if the matrix is concentric or not.
Would you please try the following:
#include <stdio.h>
int main() {
int mat[100][100];
int ii[] = {0, 1, 0, -1}; // incremental numbers of i
int jj[] = {1, 0, -1, 0}; // incremental numbers of j
int i, j;
int n;
int u, v, w; // variables to walk on edges
int val; // value of the element
int prev; // previous value in one outer edge
int length; // length of the edge
// read matrix size and values
printf("Enter the number:\n");
scanf("%d", &n);
printf("Enter the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
// loop on the edges
for (u = 0; u < n / 2; u++) { // from outmost edge to inner
i = u; j = u; // index of the north west corner
val = mat[u][u]; // initial value to compare
for (v = 0; v < 4; v++) { // four sides
length = n - u * 2 - 1; // length of the edge
for (w = 0; w < length; w++) {
i += ii[v]; // one step ahead on the edge
j += jj[v]; // same as above
if (mat[i][j] != val || (u > 0 && mat[i][j] <= prev)) {
// if u == 0, skip the comparison with prev
printf("No at [%d][%d] (val=%d)\n", i, j, mat[i][j]);
return 1;
}
}
}
prev = mat[i][j];
}
// finally examine the center value (if n is odd number)
if (n % 2) {
if (mat[u][u] <= prev) {
printf("No at [%d][%d] (val=%d)\n", u, u, mat[u][u]);
return 1;
}
}
printf("Yes\n");
return 0;
}
The basic concept is to generate a series of indexes of the edge
such as:
[0, 1], [0, 2], [0, 3], [0, 4],
[1, 4], [2, 4], [3, 4], [4, 4],
[4, 3], [4, 2], [4, 1], [4, 0],
[3, 0], [2, 0], [1, 0], [0, 0]
by using the variables i, j and the arrays ii[], jj[].
The example above is the indexes for the outermost edge and go into
the inner edge in the next iteration. Then the values of the index
is compared with the other value in the same edge and the previous
value in the outer edge.
[Edit]
Here is an alternative which does not use an array other than mat[100][100]:
#include <stdio.h>
int main() {
int mat[100][100];
int i, j;
int ii, jj; // incremental values for i and j
int n;
int u, v, w; // variables to walk on edges
int val; // value of the element
int prev; // previous value in one outer edge
int length; // length of the edge
// read matrix size and values
printf("Enter the number:\n");
scanf("%d", &n);
printf("Enter the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
// loop on the edges
for (u = 0; u < n / 2; u++) { // from outmost edge to inner
i = u; j = u; // index of the north west corner
val = mat[u][u]; // initial value to compare
for (v = 0; v < 4; v++) { // four sides
ii = (v & 1) * ((v & 1) - (v & 2));
// assigned to {0, 1, 0, -1} in order
jj = ((v + 1) & 1) * (((v + 1) & 1) - ((v + 1) & 2));
// assigned to {1, 0, -1, 0} in order
length = n - u * 2 - 1; // length of the edge
for (w = 0; w < length; w++) {
i += ii; // one step ahead on the edge
j += jj; // same as above
if (mat[i][j] != val || (u > 0 && mat[i][j] <= prev)) {
// if u == 0, skip the comparison with prev
printf("No at [%d][%d] (val=%d)\n", i, j, mat[i][j]);
return 1;
}
}
}
prev = mat[i][j];
}
// finally examine the center value (if n is odd number)
if (n % 2) {
if (mat[u][u] <= prev) {
printf("No at [%d][%d] (val=%d)\n", u, u, mat[u][u]);
return 1;
}
}
printf("Yes\n");
return 0;
}
I created an answer using more functions than just main(). It is more verbose than what is required for your homework — it prints out the matrix it reads and diagnoses the first problem it comes across. It works with both positive and negative numbers, and with matrices with odd or even numbers of elements.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
enum { MAT_SIZE = 100 };
static int err_error(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(EXIT_FAILURE);
}
static int err_shell(int r, int c, int a_val, int e_val)
{
printf("Element M[%d][%d] = %d vs expected value %d\n", r, c, a_val, e_val);
return 0;
}
static int check_shell(int shell, int n, int matrix[MAT_SIZE][MAT_SIZE])
{
int lb = shell;
int ub = n - shell - 1;
int val = matrix[lb][lb];
/* Check the horizontals */
for (int c = lb; c <= ub; c++)
{
if (matrix[lb][c] != val)
return err_shell(lb, c, matrix[lb][c], val);
if (matrix[ub][c] != val)
return err_shell(ub, c, matrix[ub][c], val);
}
/* Check the verticals */
for (int r = lb; r <= ub; r++)
{
if (matrix[r][lb] != val)
return err_shell(r, lb, matrix[r][lb], val);
if (matrix[r][ub] != val)
return err_shell(r, ub, matrix[r][ub], val);
}
return 1;
}
static int check_matrix(int n, int matrix[MAT_SIZE][MAT_SIZE])
{
for (int i = 0; i <= n / 2; i++)
{
if (check_shell(i, n, matrix) == 0)
return 0;
}
for (int i = 0; i < (n - 1) / 2; i++)
{
if (matrix[i][i] >= matrix[i+1][i+1])
{
printf("Shell %d has value %d but inner shell %d has value %d\n",
i, matrix[i][i], i+1, matrix[i+1][i+1]);
return 0;
}
}
return 1;
}
static int read_size(void)
{
int n;
if (scanf("%d", &n) != 1)
err_error("failed to read an integer\n");
if (n <= 0 || n > MAT_SIZE)
err_error("matrix size %d is not in the range 1..%d\n", n, MAT_SIZE);
return n;
}
static void read_matrix(int n, int matrix[n][n])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (scanf("%d", &matrix[i][j]) != 1)
err_error("failed to read M[%d][%d]\n", i, j);
}
}
}
static int max_field_width(int n, int matrix[MAT_SIZE][MAT_SIZE])
{
int min_val = matrix[0][0];
int max_val = matrix[0][0];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (matrix[i][j] < min_val)
min_val = matrix[i][j];
if (matrix[i][j] > max_val)
max_val = matrix[i][j];
}
}
int fld_width = snprintf(0, 0, "%d", max_val);
if (min_val < 0)
{
int min_width = snprintf(0, 0, "%d", min_val);
if (min_width > fld_width)
fld_width = min_width;
}
return fld_width;
}
static void print_matrix(const char *tag, int n, int matrix[MAT_SIZE][MAT_SIZE])
{
printf("%s (%d):\n", tag, n);
int w = max_field_width(n, matrix) + 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%*d", w, matrix[i][j]);
}
putchar('\n');
}
}
int main(void)
{
int matrix[MAT_SIZE][MAT_SIZE];
int n = read_size();
read_matrix(n, matrix);
print_matrix("Input", n, matrix);
if (check_matrix(n, matrix))
printf("YES: Matrix is a valid concentric matrix\n");
else
printf("NO: Matrix is not a valid concentric matrix\n");
return 0;
}
One detail is that this code can be made to use a VLA (variable-length array) by simply replacing MAT_SIZE by n in each function definition and modifying main() to read:
static int check_shell(int shell, int n, int matrix[n][n]) { … }
static int check_matrix(int n, int matrix[n][n]) { … }
static void read_matrix(int n, int matrix[n][n]) { … }
static int max_field_width(int n, int matrix[n][n]) { … }
static void print_matrix(const char *tag, int n, int matrix[n][n]) { … }
int main(void)
{
int n = read_size();
int matrix[n][n];
read_matrix(n, matrix);
print_matrix("Input", n, matrix);
if (check_matrix(n, matrix))
printf("YES: Matrix is a valid concentric matrix\n");
else
printf("NO: Matrix is not a valid concentric matrix\n");
return 0;
}
This reads the matrix size before allocating the matrix, instead of allocating a fixed size matrix first.
The read_size() function enables this change — that input must be done separately from the main matrix scanning code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
#include <stdio.h>
int main () {
int []={1,2,3,7,8}; // add element after 3 --> 4,5,6 (condition that i don't know position of 3 in array)
for(int i=0,i<10;i++)
{
printf("%d\n",n[i]);
}
return 0;
}
i want output 1,2,3,4,5,6,7,8
but remember in case i don't know the position of 3 or 7 in array
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
// n = len of tab
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
int main()
{
int len = 10;
int top_number_count = 3;
int data[10] = {1, 11, 3, 4, 5, 20, 7, 8, 9, 10};
int temp[10];
// copy the tab for dont change any value
for (int i = 0; i < len; i++) {
temp[i] = data[i];
}
// sort the new tab
bubbleSort(temp, len);
// print the top number
// top_number_count is the count of max number you want
for (int i = len - top_number_count; i < len; i++)
printf(">%d\n", temp[i]);
return 0;
}
As the number of large values to be displayed may vary, sorting the array is the best choice. There are different techniques of sorting. For this program I shall use the "bubble sort"(if you wish to learn other sorting techniques, check this).
Once the array is sorted in descending order(largest to smallest), we can specify the number of large values we need. The program below displays the first 3 largest numbers.
#include <stdio.h>
int main()
{
int data[10] = {0, 12, 90, 8, 1, 2, 7, 9, 11, 10} ;
int MaxElements = 10 ;
int endBoundary = MaxElements - 1 ;
for(int index = 0 ; index <= MaxElements-1 ; index++)
{
for(int counter = 0 ; counter < endBoundary ; counter++)
{
if(data[counter] < data[counter+1]) // then swap the values
{
int temp = data[counter] ;
data[counter] = data[counter+1] ;
data[counter+1] = temp ;
}
}
endBoundary-- ;
}
// displaying the first 3 largest numbers
int numOfValuesSought = 3 ;
for(int count = 0 ; count < numOfValuesSought ; count++)
{
printf("%d\n", data[count]) ;
}
return 0 ;
}
so I've been struggling with this example for a good hour now and I can't even begin to process how should I do this.
Write a program that, for given n and m, forms a matrix as described.
The matrix should be m x m, and it's filled "spirally" with it's
beginning in the upper left corner. The first value in the matrix is
the number n. It's repeated until the "edge" of the matrix, at which
point the number increments. After the number 9 goes 0. 0 ≤ n ≤ 9, 0 ≤
m ≤ 9
Some time ago I had made a function to display the numbers 1 to n on an odd-sized grid.
The principle was to start from the center and to shift by ;
x = 1
x box on the right
x box on the bottom
x++
x box on the left
x box at the top
x++
With this simple algorithm, you can easily imagine to maybe start from the center of your problem and decrement your value, it seems easier to start from the center.
Here is the code that illustrates the above solution, to be adapted of course for your problem, it's only a lead.
#define WE 5
void clock(int grid[WE][WE])
{
int count;
int i;
int reach;
int flag;
int tab[2] = {WE / 2, WE / 2}; //x , y
count = 0;
flag = 0;
i = 0;
reach = 1;
grid[tab[1]][tab[0]] = count;
for (int j = 0; j < WE - 1 && grid[0][WE - 1] != pow(WE, 2) - 1; j++)
for (i = 0; i < reach && grid[0][WE - 1] != pow(WE, 2) - 1; i++, reach++)
{
if(flag % 2 == 0)
{
for(int right = 0 ; right < reach ; right++, tab[0]++, count++, flag = 1)
grid[tab[1]][tab[0]] = count;
if(reach < WE - 1)
for(int bottom = 0; bottom < reach; bottom++, count++, tab[1]++)
grid[tab[1]][tab[0]] = count;
}
else
{
for(int left = 0; left < reach; left++, count++, tab[0]--, flag = 0)
grid[tab[1]][tab[0]] = count;
for(int top = 0; top < reach; top++, tab[1]--, count++)
grid[tab[1]][tab[0]] = count;
}
}
}
I finally solved it. If anybody's interested, here's how I did it:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Fills the row number "row" with the number n
int fillRow(int m, int n, int arr[m][m], int row)
{
int j;
for(j=0;j<m;j++)
{
if(arr[row][j] == -1 || arr[row][j] == n-1) arr[row][j] = n;
}
}
//Fills the column number "col" with the number n
int fillCol(int m, int n, int arr[m][m], int col)
{
int i;
for(i=0;i<m;i++)
{
if(arr[i][col] == -1 || arr[i][col] == n-1) arr[i][col] = n;
}
}
int main()
{
int n, m, i, j, r=1, c=1, row=-1, col=-1;
scanf("%d %d",&n, &m);
int arr[m][m];
//Fill array with -1 everywhere
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
arr[i][j] = -1;
}
}
//Calculate which row/column to fill (variables row/col)
//Fill row then column then row than column...
for(i=0;i<2*m;i++)
{
if(i%2==0)
{
row = (r%2==0) ? m-r/2 : r/2;
fillRow(m, n, arr, row);
n++;
r++;
}
else if(i%2==1)
{
col = (c%2==0) ? c/2-1 : m-c/2-1;
fillCol(m, n, arr, col);
n++;
c++;
}
}
//If an element is larger than 9, decrease it by 10
//Prints the elements
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(arr[i][j]>9) arr[i][j] -=10;
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
I should make new array out of existing one (ex. 1 0 4 5 4 3 1) so that the new one contains digits already in existing array and the number of their appearances.
So, the new one would look like this: 1 2 0 1 4 2 5 1 3 1 (1 appears 2 times, 0 appears 1 time.... 3 appears 1 time; the order in which they appear in first array should be kept in new one also); I know how to count no. of times a value appears in an array, but how do I insert the no.of appearances? (C language)
#include <stdio.h>
#define max 100
int main() {
int b, n, s, i, a[max], j, k;
printf("Enter the number of array elements:\n");
scanf("%d", &n);
if ((n > max) || (n <= 0)) exit();
printf("Enter the array:\n");
for (i = 0; i < n; i++)
scanf("%d", a[i]);
for (i = 0; i < n; i++) {
for (j = i + 1; j < n;) {
if (a[j] == a[i]) {
for (k = j; k < n; k++) {
a[k] = a[k + 1];
}}}}
//in the last 5 rows i've tried to compare elements, and if they are same, to increment the counter, and I've stopped here since I realised I don't know how to do that for every digit/integer that appears in array//
If you know that the existing array consists of digits between 0 and 9, then you can use the index of the array to indicate the value that you are incrementing.
int in[12] = {1,5,2,5,6,5,3,2,1,5,6,3};
int out[10] = {0,0,0,0,0,0,0,0,0,0};
for (int i = 0; i < 12; ++i)
{
++out[ in[i] ];
}
If you provide any code snippet, its easy for the community to help you.
Try this, even you optimize the no.of loops :)
#include <stdio.h>
void func(int in[], int in_length, int *out[], int *out_length) {
int temp[10] = {0}, i = 0, j = 0, value;
//scan the input
for(i=0; i< in_length; ++i) {
value = in[i];
if(value >= 0 && value <= 9) { //hope all the values are single digits
temp[value]++;
}
}
// Find no.of unique digits
int unique_digits = 0;
for(i = 0; i < 10; ++i) {
if(temp[i] > 0)
unique_digits++;
}
// Allocate memory for output
*out_length = 2 * unique_digits ;
printf("digits: %d out_length: %d \n",unique_digits, *out_length );
*out = malloc(2 * unique_digits * sizeof(int));
//Fill the output
for(i = 0, j = 0; i<in_length && j < *out_length; ++i) {
//printf("\n i:%d, j:%d val:%d cout:%d ", i, j, in[i], temp[in[i]] );
if(temp[in[i]] > 0 ) {
(*out)[j] = in[i];
(*out)[j+1] = temp[in[i]];
temp[in[i]] = 0; //Reset the occurrences of this digit, as we already pushed this digit into output
j += 2;
}
}
}
int main(void) {
int input[100] = {1, 0, 4, 5, 4, 3, 1};
int *output = NULL, output_length = 0, i = 0;
func(input, 7, &output, &output_length );
for(i=0; i < output_length; i+=2) {
printf("\n %d : %d ", output[i], output[i+1]);
}
return 0;
}
Here's a loop to sort an array from min to max, I need the result of this loop to be put into another array so I can filter and remove the numbers that occur only once and find the last member of what's left.
Here's the code I have so far:
#include<stdio.h>
#include<conio.h>
#define buffas 1024
void main() {
int arr[buffas],i,j,element,no,temp;
printf("\nEnter the no of Elements: ");
scanf("%d", &no);
for(i=0; i<no; i++) {
printf("\n Enter Element %d: ", i+1);
scanf("%d",&arr[i]);
}
for(i=0; i<no; i++) {
for(j=i; j<no; j++) {
if(arr[i] > arr[j]) {
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nSorted array:");
for(i=0; i<no; i++) {
printf("\t%d",arr[i]);
}
getch();
}
How do I change the
printf("\t%d",arr[i]);
To fill another array and then sort that to remove single entries and leave ony those that repeat at least once.
eg. the first aray is
2 2 1 6 9 9
and after the second sorting the result should be
2 2 9 9
#include <stdio.h>
#define buffas 16
int main(void)
{
/* Instead of original input and sorting code */
int arr[] = { 1, 2, 2, 6, 9, 9, 10, 10, 10, 11, 12, 13, 14 };
int no = sizeof(arr) / sizeof(arr[0]);
/* Code to copy only duplicated elements in arr */
int copy[buffas];
int n = 0;
for (int i = 0; i < no; i++)
{
int j;
for (j = i + 1; j < no; j++)
{
if (arr[i] != arr[j])
break;
}
if (j - i > 1)
{
for (int k = i; k < j; k++)
copy[n++] = arr[k];
i = j - 1;
}
}
/* Print results for verification */
for (int i = 0; i < n; i++)
printf("c[%d] = %d\n", i, copy[i]);
return 0;
}
The code has been run with various lengths of sorted array and different data in the array; it seems to be correct. The code above produces the output:
c[0] = 2
c[1] = 2
c[2] = 9
c[3] = 9
c[4] = 10
c[5] = 10
c[6] = 10
Note that the code uses the C99 feature of declaring variables in a for loop control statement; if you're on Windows and without C99 support, you'll need to declare i and k outside the loops. If you're using GCC, you need to add -std=c99 or a similar option.