how to store a masked array - c

i have been asked to code a memory game, with some specifics, where the reveled letter shows the first time then, if the user in the 2nd prompt guesses the corresponding matching spot, the board should stay like that until the user finishes the game (by guessing all of the correct matching spots), heres an example with a 2x2 grid
Your program:
* *
* *
Enter a pair of integers in the range [1, 2]
Player: 1 1
Your program:
A *
* *
(then it clears the screen and displays)
* *
* *
Enter another pair of integers in the range [1, 2]
Player: 2 1
Your program:
* *
C *
(then it clears screen and displays)
* *
* *
Enter a pair of integers in the range [1, 2]
Player: 1 2
Your program:
* C
* *
(then it clears screen and displays)
* *
* *
Enter another pair of integers in the range [1, 2]
Player: 2 1
Your program:
* *
C *
(then it clears the screen and displays)
* C
C *
Enter a pair of integers in the range [1, 2]
Player: 1 1
Your program:
A C
C *
(then it clears the screen and displays)
* C
C *
Enter another pair of integers in the range [1, 2]
Player: 1 1
Your program:
A C
C *
(then it clears the screen and displays)
* C
C *
Enter a pair of integers in the range [1, 2]
Player: 1 1
Your program:
A C
C *3
(then it clears the screen and displays)
* C
C *
Enter another pair of integers in the range [1, 2]
Player: 2 2
Your program:
A C
C A
CONGRATULATIONS. YOU SUCCEEDED
i need a 4x4, i understand how to show the correct matches but i cannot seem to store the new board, so the user sees the most current board, i cant wrap my head around it....
char board[4][4] = { {'A','B','A','D'},{'C','E','H','G'},{'B','D','G','C'},{'F','H','F','E'} };
int i, j, row, column, row2, column2;
char boardmatch[4][4];
int tempX,tempY;
for(tempX = 0; tempX < 4; tempX++){
for(tempY = 0; tempY < 4; tempY++){
boardmatch[tempX][tempY] = 0;
}
}
for (i=0; i < 4 ; i++){
for (j=0; j<4; j++){
printf("* ");
}
printf("\n");
}
do {
printf("\nEnter a pair of integers in the range [1, 4]: ");
scanf("%d %d", &row, &column);
row--;
column--;
printf("\n");
for (i=0; i < 4 ; i++){
for (j=0; j < 4 ; j++){
if ( i == row && j == column){
printf("%c ", board[row][column]);
}else{
printf("* ");
}
}
printf("\n");
}
printf("\n");
system("pause");
system("cls");
for (i=0; i < 4 ; i++){
for (j=0; j<4; j++){
printf("* ");
}
printf("\n");
}
printf("\nEnter another pair of integers in the range [1, 4]: ");
scanf("%d %d", &row2, &column2);
row2--;
column2--;
printf("\n");
for (i=0; i < 4 ; i++){
for (j=0; j < 4 ; j++){
if (i == row2 && j == column2){
printf("%c ", board[row2][column2]);
}else{
printf("* ");
}
}
printf("\n");
}
system("pause");
system("cls");
if(board[row][column]==board[row2][column2]){
boardmatch[row][column] = 1;
boardmatch[row2][column2] = 1;
}
for (i=0; i < 4 ; i++){
for (j=0; j<4; j++){
if (boardmatch[i][j] == 1){
printf("%c ", board[row2][column2]);
}else{
printf("* ");
}
}
printf("\n");
}
printf("\n");
system("pause");
system("cls");
}while(1);
system("PAUSE");
return 0;
}

You need another array for the board.
It just holds a bit for each cell, meaning 'found' or 'turned'.
Use that (and the original board) to display the board.
Only show the cells that have been found/turned.
For the players turns, when some cells might be turned back, just remember which cell was turned so you can flip it back to unfound/unturned. This array starts out as all unturned and the game finishes when they are all turned.
(You can use a struct to put this all in one array as well.)

The problem is in these lines (fix already applied).
if (boardmatch[i][j] == 1)
{
printf("%c ", board[i][j]);
}
It should show the correct value if it has been matched before. Not the ones under row2, column2
Also, here's an alternative on what you're doing, storing the previous values of row and column as row_old and column_old.
#include <stdio.h>
// Prints the matrix
void board_print(char board[4][4], char boardmatch[4][4], int revealRow, int revealColumn, int revealRow2, int revealColumn2)
{
int row, col;
printf(" 0 1 2 3\n");
// Printing code
for (row=0; row < 4 ; row++)
{
printf ("%d ", row);
for (col=0; col<4; col++)
{
// Print the value if there's a board match
// or there's a revealRow/revealColumn
if (boardmatch[col][row] == 1
|| (row == revealRow && col == revealColumn)
|| (row == revealRow2 && col == revealColumn2))
{
printf("%c ", board[col][row]);
}
else
{
printf("* ");
}
}
printf("\n");
}
}
// Clears board
void board_clear(char board[4][4])
{
int i,j;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
board[i][j] = 0;
}
}
}
// Main game loop
void game_loop()
{
char board[4][4] = { {'A','B','A','D'},{'C','E','H','G'},{'B','D','G','C'},{'F','H','F','E'} };
int row, column, row_old=-1, column_old=-1;
char boardmatch[4][4];
board_clear(boardmatch);
// Reveal the matrix
board_print(board, boardmatch, -1, -1, -1, -1);
do
{
// Ask for input
printf("\n[column row]: ");
fflush(stdin);
scanf("%d %d", &column, &row);
// Adjust the values
// Check the previous value
if (row_old == -1 || column_old == -1)
{
// There was nothing stored
// Store the previous values
row_old = row;
column_old = column;
// Print only the selected value
board_print(board, boardmatch, row, column, -1, -1);
}
else
{
// There was something stored
// Check if he did it
if (board[column][row] == board[column_old][row_old])
{
// You did it! Store as matched
boardmatch[column][row] = 1;
boardmatch[column_old][row_old] = 1;
// Present the result to the user
board_print(board, boardmatch, row, column, -1, -1);
printf("Match!\n");
}
else
{
// Nope, you didn't
// Present the two items marked (old and selected)
board_print(board, boardmatch, row, column, row_old, column_old);
printf("YOU SUCK!\n");
}
// Now print the matrix with the selected values
// Finally, kill the previous values
row_old = -1;
column_old = -1;
}
// (Check the boardmatch if every value is 1, and break if it does)
} while (1);
}
int main(int argc, const char * argv[])
{
game_loop();
return 0;
}
The key here, is that the row_old and column_old acts as the check for the items, as much as it acts for a flag for the game.
Whenever it's -1, it means that the previous match has not been made, and when it stores a value, it represents the previous match.
I also suggest as an improvement and/or lesson, modify the code using the structs:
#import <stdbool.h>
typedef struct
{
char value;
bool matched;
}tile;
typedef struct
{
int row, col;
}coordinate;

Related

How to write a program to delete an element in an array using C language? [duplicate]

I wrote the following program to delete an array element entered by the user.
#include <stdio.h>
#include <conio.h>
void main() {
int j, i, a[100], n, key, l;
clrscr();
printf("Enter the number of elements:");
scanf("%d", &n);
printf("\nEnter the elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\nEnter the element to delete:");
scanf("%d", &key);
l = n; //Length of the array
for (i = 0; i < l; i++) {
if (a[i] == key) {
for (j = i; j < l; j++)
a[j] = a[j + 1];
l--; //Decreasing the length of the array
}
}
printf("\nThe new array is \n");
for (i = 0; i < l; i++)
printf("%d ", a[i]);
getch();
}
It works fine for most inputs but when the input is something like: 1 2 2 3 5 (here 2 repeats consecutively) and the element to be deleted is 2, the output is 1 2 3 5.
How can I modify the program such that all instances of the entered element is removed?
After l-- put i-- too as shown below
if(a[i]==key)
{
for(j=i;j<l;j++)
a[j]=a[j+1];
l--; //Decreasing the length of the array
i--; //check again from same index i
}
Other posters have given you 2 solutions ... I think understanding why it happens is good too :)
Let's take your example 1, 2, 2, 3, 5 and follow the code line by line
i = 0; /* first time through the loop; i "points" to 1 */
if (a[i] == 2) ... /* nope; next loop */
i = 1;
if (a[1] == 2) ... /* yes! let's go inside the if */
/* move all elements back
** and "decrease" array length */
/* array is now 1, 2, 3, 5 */
/* next loop */
i = 2;
if (a[i] == 2) ... /* nope; OH! Wait ...
** a[1] is the new 2 and it wasn't checked */
If you don't care about the order of the elements in the array, you can move the last element of the array into the newly formed gap (cunningly reducing the length of the array by one). This can be vastly more efficient than shunting the elements down: in computer science term this makes deleting an element O(1) rather than O(N).
a[i] = a[--l];
If your i index is looping over the array, you'll want to loop over this element again:
a[i--] = a[--l];
For example, to remove all elements '3' from an array of length 'l':
for (i = 0; i < l; ++i) {
if (a[i] == 3) {
a[i--] = a[--l];
}
}
If you do care about the order of the elements in the array, it's most efficient to use memmove rather than move elements by hand. It's designed to be used where the source and destination memory overlap.
memmove(a + i, a + i + 1, sizeof(a[0]) * (l - i - 1));
Change "if" to "while":
for(i=0;i<l;i++)
{
while (i<l && a[i]==key)
{
for(j=i;j<l;j++)
a[j]=a[j+1];
l--; //Decreasing the length of the array
}
}
use a new array.
int array[l];
int k=0;
for(i=0;i<l;i++)
{
if(a[i]!=key)
{
array[k]=a[i];
k++;
}
}
#include<stdio.h>
int main(){
int size;
int array[20];
int delete_pos;
int i;
printf("Enter the Size of the Array :");
scanf("%d",&size);
for(i=0;i<=size-1;i++){ //no of elements taken are 1 less than size of the array asked.
printf("\nEnter the element[%d] :",i+1);
scanf("%d",&array[i]);
}
printf("\nEnter the Position of the array to be deleted :");
scanf("%d",&delete_pos);
for(i=delete_pos-1;i<=size;i++){ //every element of the array is replaced by array on next position.
array[i]=array[i+1];}
size=size-1; // Reducing the size of the array as one element is deleted.
printf("Your new array is \n");
for(i=0;i<=size-1;i++){ //printing the entire new array.
printf("%d ",array[i]);
}
printf("\n\n");
return 0;
}
Your method with 2 nested for loops is too complicated. You can simple scan the array with an index i and copy all elements different from key with a different index len. The resulting array length is the final value of len.
Here is a modified version:
#include <stdio.h>
#include <conio.h>
int main(void) {
int a[100];
int i, n, key, len;
clrscr();
printf("Enter the number of elements: ");
if (scanf("%d", &n) != 1) {
printf("invalid input\n");
return 1;
}
if (n < 0 || n > 100) {
printf("invalid number of elements\n");
return 1;
}
printf("\nEnter the elements:\n");
for (i = 0; i < n; i++) {
if (scanf("%d", &a[i]) != 1) {
printf("invalid input\n");
return 1;
}
}
printf("\nEnter the element to delete: ");
if (scanf("%d", &key) != 1) {
printf("invalid input\n");
return 1;
}
for (i = len = 0; i < n; i++) {
if (a[i] != key)
a[len++] = a[i];
}
printf("\nThe new array is:\n");
for (i = 0; i < len; i++)
printf("%d ", a[i]);
printf("\n");
getch();
return 0;
}
Notes:
The prototype for main without arguments is int main(void) and it is considered good style to return 0 for success.
always test the return value of scanf(). This prevents many bugs and undefined behavior for invalid input. It also saves a lot of time looking in the wrong places when input was just invalid.
avoid naming a variable l as it looks too close to 1 in many fixed pitch fonts.
always terminate the program output with a newline.

why the program cannot count the number? c language

i am making a minesweeper game in c language. in this stage, i want to the program to count the number of mines in specific row or column. what the program supposed to do is that after the user input the locations of the mines, he can count the no. of mines in specific row by typing 1 (row no.) or count the no. of mines in specific col by typing 2(col no.). however, my program cannot count the no. and always print out 0. i can't figure out why. plz kindly help me t o find out the problems of my program
here is the pictures that show what my program do
#include <stdio.h>
#include <stdlib.h>
// Possible square states.
#define VISIBLE_SAFE 0
#define HIDDEN_SAFE 1
#define HIDDEN_MINE 2
// The size of the starting grid.
#define SIZE 8
// The possible command codes.
#define DETECT_ROW 1
#define DETECT_COL 2
#define DETECT_SQUARE 3
#define REVEAL_SQUARE 4
#define GAMEPLAY_MODE 5
#define DEBUG_MODE 6
#define REVEAL_RADIAL 7
// Add any extra #defines here.
void initialise_field(int minefield[SIZE][SIZE]);
void print_debug_minefield(int minefield[SIZE][SIZE]);
// Place your function prototyes here.
int main(void) {
int minefield[SIZE][SIZE];
int mines;
int pair1, pair2;
int detect;
int count = 0;
initialise_field(minefield);
printf("Welcome to minesweeper!\n");
printf("How many mines? ");
scanf("%d",&mines);
// TODO: Scan in the number of pairs of mines.
printf("Enter pairs:\n");
for( int i = 0; i < mines; i++){
scanf("%d %d",&pair1, &pair2);
minefield[pair1][pair2] = HIDDEN_MINE;
}
// TODO: Scan in the pairs of mines and place them on the grid.
printf("Game Started\n");
print_debug_minefield(minefield);
while(scanf("%d %d", &detect, &pair2) != EOF){
if ( detect == DETECT_ROW){
for( int i = 0; i < pair2; i++){
if(minefield[pair2][i] == HIDDEN_MINE){
count++;
}
}
printf("There are %d mine(s) in row %d\n",count,pair2);
print_debug_minefield(minefield);
}
else if( detect == DETECT_COL){
for(int j = 0; j < pair2; j++){
if( minefield[j][pair2] == HIDDEN_MINE){
count++;
}
}
printf("There are %d mine(s) in column %d\n",count,pair2);
print_debug_minefield(minefield);
}
}
// TODO: Scan in commands to play the game until the game ends.
// A game ends when the player wins, loses, or enters EOF (Ctrl+D).
// You should display the minefield after each command has been processed.
return 0;
}
// Set the entire minefield to HIDDEN_SAFE.
void initialise_field(int minefield[SIZE][SIZE]) {
int i = 0;
while (i < SIZE) {
int j = 0;
while (j < SIZE) {
minefield[i][j] = HIDDEN_SAFE;
j++;
}
i++;
}
}
// Print out the actual values of the minefield.
void print_debug_minefield(int minefield[SIZE][SIZE]) {
int i = 0;
while (i < SIZE) {
int j = 0;
while (j < SIZE) {
printf("%d ", minefield[i][j]);
j++;
}
printf("\n");
i++;
}
}
In line minefield[pair1][pair2] = HIDDEN_MINE set mines in below co-ordinate.
(0 0), (1 1), (2 2)
But in below code.
for( int i = 0; i < pair2; i++){
if(minefield[pair2][i] == HIDDEN_MINE){
count++;
}
It only verifies co-ordinate (pair2, 0 to (pair2-1) ) whereas HIDDEN_MINE values are in co-ordinate (0-pair1, pair2).
So loop to be executed till pair2 not (pair2 -1).
for( int i = 0; i <= pair2; i++) // Will modify count properly

How to write the algorithm of this matrix in this ascending order in C.Where is error in my code?

I am writing this code to print the following matrix in this spiral order(spiral by column).But my code is printing totally different thing.
a a+7 a+8 a+15
a+1 a+6 a+9 a+14
a+2 a+5 a+10 a+13
a+3 a+4 a+11 a+12
Here is what i did:
int main() {
int a;
int Sum = 0;
int i = 0, j = 0,n;
printf("Insert the value of n: ");
scanf("%d",&n);
printf("Insert the value of a number: ");
scanf("%d",&a);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",a);
a = a + 7;
printf("\t");
}
printf("%d",a);
a = a + 1 ;
printf("\n");
}
return 0;
}
The way I approached this is to build the matrix of values you actually want, but doing so in column order, where we can relatively easily control the logic of value progression by row. Then, with that matrix in hand, print out the values in row order, as you want the output:
int main()
{
int a = 7;
int n = 4;
int array[4][4];
for (int c=0; c < n; ++c)
{
for (int r=0; r < n; ++r)
{
// values ascending for even columns
if (c % 2 == 0)
{
array[r][c] = a + c*n + r;
}
// values descending for odd columns
else
{
array[r][c] = a + c*n + n-r-1;
}
}
}
for (int i=0; i < n; ++i)
{
for (int j=0; j < n; ++j)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
}
Output:
Demo here:
Rextester
Instead of using this complex mechanism to keep track of all elements you can just calculate the value to add at any time by simple arithmetic.
See this
int row;
int column;
printf("\n");
for (row = 0; row < n; row++) {
for (column = 0; column < n; column++) {
int base;
int flag;
if (column % 2 != 0) {
base = (column+1)/2 * 2*n - 1;
flag = -1;
}else {
base = column/2 * 2*n;
flag = 1;
}
printf( "%d ", a + base + flag * row);
}
printf("\n");
}
I hope you are able to follow this logic. If not feel free to ask.
Demo here:
Ideone
There seem to be two issues with your code as it is. As mentioned in the above comment, you are using the variable a in the loop calculation, so it is constantly being updated. This means your loop becomes invalid after a few iterations. If you define a dummy variable, this would avoid the problem. Secondly the implementation of the spiralling is close to being right, but it's not quite there.
Consider in the case n = 4. When you print along each row, the difference between a new element and the last alternates between values of (2n - 1) = 7 and 1. To take this into account, you could for example check every time you want to print whether the column index (j) is odd or even, and use this to determine which difference to add. Once you have the row machinery fixed, it shouldn't be difficult to extend it to the columns.
Simple solution using a matrix to calculate values before print them
#include <stdio.h>
int main(void)
{
int a;
int i = 0, j = 0, n;
printf("Insert the value of n: ");
scanf("%d", &n);
printf("Insert the value of a number: ");
scanf("%d", &a);
int matrix[n][n];
for (i=0; i< n*n; i++)
{
// even columns ascending
if (((i/n) % 2) == 0)
{
matrix[i%n][i/n] = a++;
}
// odd column descending
else
{
matrix[n-(i%n)-1][i/n] = a++;
}
}
for (i=0; i< n; i++)
{
for (j=0; j< n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
Insert the value of n: 4
Insert start value: 1
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13

C Chomp questions?

I'm trying to create the game Chomp. I am halfway through but pretty stuck.
The game will have 5 different functions. Pointers and structs are not allowed.
This is how far I have come and I have been struggeling with a few problems for a while, but I can't figure out how to solve them by myself so I thought I could get some help here.
BUGS
a) If you first input 2 2 and then input 2 1 it will say that the position already has been eaten, even though it's a perfectly valid position to eat. Instead of checking if the position is != 'O' I should check if it is == 'O', but that won't work either because in the check_move() loop the row and col will not always be an O...
b) If you input a position that is not inside the matrix (i.e. 20 20) you will get two lines of errors. I don't understand why. Of course I only want to display one error, not two.
c) If you input a position that has already been eaten you will get the error "Already been eaten!" several times due to the loop that is looping through the print several times.
QUESTION
a) What is the best way to alternate between Player 1 and Player 2? I thought about an int that will increase by +1 every time a player makes a valid move. Then I will check if the value of the int is odd or even. Odd = Player 1 and even = Player 2 or vice verca. But that won't work because I'm not allowed to have any more global variables than I currently has. And I'm only allowed to return one value from one function (check_move()).
#include <stdio.h>
int height = 4;
int width = 10;
char matrix[4][10];
void initialize()
{
for(int row = 0; row < height; row++)
for(int col = 0; col < width; col++)
matrix[row][col] = 'O';
}
void print_board()
{
printf("\n\n");
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
printf("%c", matrix[row][col]);
}
printf("\n");
}
printf("\n\n");
}
void get_move(int player, int input[])
{
printf("Player %d, make your move: ", player);
scanf("%d %d", &input[0], &input[1]);
}
int check_move(int position[])
{
int row = position[0];
int col = position[1];
int status = 1;
if(row <= height && col <= width)
{
for(row; row <= height; row++)
{
for(col; col <= width; col++)
{
// Checks if position already has been eaten
if(matrix[row-1][col-1] != 'O')
{
printf("Already eaten!\n");
status = 0;
}
}
}
}
else if(row >= height || col >= width)
{
printf("Your move must be inside the matrix!\n");
status = 0;
}
return status;
}
void update_board(int x, int y)
{
for(int xi = x; xi <= 10; ++xi)
{
for(int yi = y; yi <= 10; ++yi)
matrix[xi-1][yi-1] = ' ';
}
}
int main(void)
{
int player = 1;
int position[2];
initialize();
print_board();
while(1){
get_move(player, position);
check_move(position);
while(check_move(position) != 1)
{
printf("Try again!\n\n");
get_move(player, position);
}
update_board(position[0], position[1]);
print_board();
}
getchar();
getchar();
getchar();
return 0;
}
Bug a and c:
Your check_move function is wrong, you should only test if the position played is eaten or not, the status of the other positions are not relevant:
int check_move(int pos[])
{
if(pos[0] < 1 || pos[0] > height || pos[1] < 1 || pos[1] > width)
{
printf("Your move must be inside the matrix!\n");
return 0;
}
if(matrix[ pos[0] - 1 ][ pos[1] - 1 ] != 'O' ) {
printf("Already eaten!\n");
return 0;
}
return 1;
}
Bug b:
You get the error message twice because you're calling check_move twice in your main:
check_move(position);
while(check_move(position) != 1)
Just remove the useless first call to check_move().
Question a:
You can switch between players by updating the variable player inside your main :
player = (player + 1) % maxNumberOfPlayer;
This will go from 0 to maxNumberOfPlayer - 1, so you may use printf("Player %d, make your move: ", player + 1); for a more user-friendly output. Also, if maxNumberOfPlayer = 2, player = (player + 1) % 2; is equivalent to player = !player.
In main, inside your while loop just add:
player = !player;
Which will toggle player between 0 and 1.

check common values in 2 arrays and put the different in third array

i have this code ... for a function who get 7 numbers from the user and put it into array number 1.... and second create a random numbers and put them into array number 2....check if there is a common values ...(not only in the same place for example: not only if ar1[1]==ar2[1] ... but to check for all places if there is a common values)....and to put the different values in third array...to create third array and put the not common values in it ... this is my code:
//includes
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <time.h>
//define
#define N 7
//prototype
void randomArray(int maxVal,int ar2[]);
int main()
{
//variables/arrays
int ar1[N],ar2[N],ar3[N],i,z,t,temp,maxVal,counter;
printf("Please Enter %d numbers: ",N);
for(i=0;i<7;i++)
{
scanf("%d",&ar1[i]);
}
while(1)
{
printf("Please enter a max value: ");
scanf("%d",&maxVal);
if(maxVal>0)
break;
}
randomArray(maxVal,ar2);
getch();
return 0;
}
//randomArray
void randomArray(int maxVal,int ar2[])
{
int i;
for(i=0;i<N;i++)
{
/* get random value between minVal and maxVal */
ar2[i] = (rand() % (maxVal+1 - 1)) + 1;
}
}
Here's the simple, brute force approach. It first compares each element in array 1 against every element in array 2 and stores it if it's not in array 2. Then it does the same thing for array 2. Then it prints array 3.
Note: you need to change the size of array 3 to N*2 in case there are no matches. Also, set all elements in array 3 to 0 first to mark as a non-result. Or keep track of array 3's length.
int ar3[N*2] = {0};
int index = 0;
//Check 1st array
printf("Array 1:\t");
for (int i = 0; i < N; i++) {
bool in_array = false;
for (int k = 0; k < N; k++) {
if (ar1[i] == ar2[k]) {
in_array = true;
break;
}
}
if (!in_array) {
ar3[index++] = ar1[i];
}
printf(" %d", ar1[i]);
}
//Check 2nd array
printf("\r\nArray 2:\t");
for (int i = 0; i < N; i++) {
bool in_array = false;
for (int k = 0; k < N; k++) {
if (ar2[i] == ar1[k]) {
in_array = true;
break;
}
}
if (!in_array) {
ar3[index++] = ar2[i];
}
printf(" %d", ar2[i]);
}
// Print result
printf("\r\nUnique:\t");
for (int i = 0, j = 0; i < N*2; i++) {
if (0 == ar3[i]) {
break;
}
printf(" %d", ar3[i]);
}
If this works for you, please accept as correct answer.

Resources