Unique 2D array columns permutations - arrays

I have the following function
function permutation(a){
let res = [];
for(let i=0; i<a.length; i++){
let restA = a.slice(0,i).concat(a.slice(i+1));
let rest = permutation(restA);
if(rest.length === 0){
res.push([a[i]]);
}else{
for(let j=0; j<rest.length; j++){
res.push([a[i]].concat(rest[j]));
}
}
}
return res;
}
which generates all permutations of 1D array with unique values like [1,2,3,4].
I also have a 2D array filled with zeroes and ones like this
[
[1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1]
]
I need to generate all unique permutations of this array. How do I do this?

You can ignore the fact it's a 2D array and consider this a 64 bit value with 34 1's and 30 0's. With every valid combination of these bits you can build a unique 2D array (8 bits per row).
However, if you do the math you'd see it's not really feasible to create all these combinations:
64! / ( 34! * 30!) = 1.620288e+18
This is almost as large as unsigned 64 bit range (1.8e+19) - it's practically a 10th of that.
Having said that, if you really want to do this, just iterate a 64 bit unsigned integer from 0 up to MAXVALUE, and test "on" bit count in each iteration - if it's 34, you've found a valid combination.

Related

Write an algorithm accepts a positive integer n as input, when executed, prints out a list of all self-aware arrays of length n

Self-Aware Arrays.
An integer array int[] A is self-aware if for each i < A.length, A[i] is the exact number of occurrences of i in A.
For example, [2, 0, 2, 0] is self-aware.
Write an algorithm accepts a positive integer n as input, when executed, prints out a list of all self-aware arrays of that length.
Here are some Self-aware arrays have been found. But I haven't found an algorithm.
N = 4
{2, 0, 2, 0}
{1, 2, 1, 0}
N = 5
{2, 1, 2, 0, 0}
N = 6
None
N = 7
{3, 2, 1, 1, 0, 0, 0}
N = 8
{4, 2, 1, 0, 1, 0, 0, 0}
N = 9
{5, 2, 1, 0, 0, 1, 0, 0, 0}
N = 10
{6, 2, 1, 0, 0, 0, 1, 0, 0, 0}
N = 11
{7, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0}
N = a
{ a-4, 2, 1, <a-7 0s>, 1, 0, 0, 0 }
Seems we have the properties
sum(A[i]) = n = sum(i * A[i])
A[0] = sum( (i-1) * A[i] ) i>=2
Outer loop: Generate all candidates of length n. This can be done with any convenient set product software; you need n elements in the range 0:n-1. For each candidate, filter and check (below).
Filter:
The sum of the candidates elements must equal n. Note that if you're writing your own candidate generator, you can use this to greatly reduce the quantity of candidates.
The first element can never be 0.
Check:
Count the quantity of each number 0:n-1 in order. The resulting list is your check count. If this list is equal to the original candidate, you have a solution: print it.
Shortcuts:
Are you allowed to use known results? It's long proved that for n >= 7, the formula you give is the only solution: you can directly generate the one solution and return. For n < 7, brute force will produce all valid solutions quite quickly.
function valueCountMap(arr) {
let obj = {};
for (let i = 0; i < arr.length; i++) {
let temp = arr[i];
let val = obj[temp];
if (val) {
obj[temp] += 1;
} else {
obj[temp] = 1;
}
}
return obj;
}
function isSelfAware(arr) {
let map = valueCountMap(arr);
for (const key in map) {
if (map.hasOwnProperty(key)) {
const found = map[key];
if (arr[key] !== found) {
return false;
}
}
}
return true;
}
let arr1 = [7, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0];
console.log(isSelfAware(arr1));

Loop variables on arduino

I want do to a loop with some variables on my arduino , I have 24 variables ( ConfigSonde[0][3] to ConfigSonde[24][3] ) to change, i need do to a loop:
EX :
ConfigSonde_0[] = {'xxx', 3, 'A', 1, 0, 0, 0, 0, 0, 0};
ConfigSonde_1[] = {'xxx', 1, 'A', 1, 0, 0, 0, 0, 0, 0};
for (i = 0; i < 25; i = i + 1) { ConfigSonde_[i][3]=0;}
Is it possible ?
Thank
You are mixing ints and 'xxx'? (if it's supposed to be a string it must be "xxx"), and this is not legal in C.
ConfigSonde_[i][3] where i = 0 doesn't give you a reference to the variable ConfigSonde_0[3]
Maybe you are looking for:
int ConfigSonde_0[] = {'x', 3, 'A', 1, 0, 0, 0, 0, 0, 0};
int ConfigSonde_1[] = {'x', 1, 'A', 1, 0, 0, 0, 0, 0, 0};
int *ConfigSonde [] = {ConfigSonde_0, ConfigSonde_1};
for (i = 0; i < 2; i = i + 1) {ConfigSonde[i][3] = 0;}
or
int ConfigSonde[][10] = {
{'x', 3, 'A', 1, 0, 0, 0, 0, 0, 0},
{'x', 1, 'A', 1, 0, 0, 0, 0, 0, 0}
};
for (i = 0; i < 2; i = i + 1) {ConfigSonde[i][3] = 0;}
Rewrite: Do do what you need, you're going to have to either add additional variables that point at the things you want to change in the loop, or change your 25 distinct arrays to be a single 2-dimensional array.
You cannot dynamically "compute" names of variables to access their values in C (or C++), since these languages are compiled the names of variables are no longer relevant when the program runs.

How to correctly count æ ø å (Unicode as UTF-8) characters in C?

I am writing a simple program that counts characters from a textfile (UTF-8) that I put in a linked list. Everything seem to work well except that it counts æ ø å (three last characters in the norwegian alphabet) twice for each instance. So if the string is æøå, I get 6 instead of 3. How to fix this?
int length()
{
pointer = root; // Reset pointer
int i; // Looping through data in node
int len = 0; // Counting characters
int sizedata = sizeof(pointer->data); // Sets size limit for data in node
while(pointer != NULL)
{
for(i = 0; i < sizedata; i++) // Looping through data in node
{
if(pointer->data[i] == '\0') break; // Stops count on end of string
len++; // Counting characters
}
pointer = pointer->next; // Linking to next node
}
printf("Length of text is: %d characters\n", len);
}
I changed the code according to this site. Everything is the same expect for the if statement before len++;
int length()
{
pointer = root; // Reset pointer
int i; // Looping through data in node
int len = 0; // Counting characters
int sizedata = sizeof(pointer->data); // Sets size limit for data in node
while(pointer != NULL)
{
for(i = 0; i < sizedata; i++) // Looping through data in node
{
if(pointer->data[i] == '\0') break; // Stops count on end of string
if ((pointer->data[i] & 0xC0) != 0x80) //count characters
len++;
}
pointer = pointer->next; // Linking to next node
}
printf("Length of text is: %d characters\n", len);
}
Note (thanks #Eljay):
This is counting Unicode code points (that are encoded in UTF-8), but not characters (glyphs). Some characters are made up of multiple code points. For example, x̝̌ is 78 cc 9d cc 8c, for the x and the two combining code points. This routine would count that 1 character as a length of 3 (code points).
Your text file seems to be encoded in UTF-8. Then you should respect the encoding length of a character, which can be derived from the first byte of a byte sequence, see this Wikipedia atice.
The code below just counts the length of a single string, i.e. it does not make use of your linked list structure.
The length of a byte sequence is stored in an array for easy look-up; -1 marks an illegal value for a first byte in a sequence. The bytes marked cont are continuation bytes and should only occur after the first byte of a sequence in well-formed UTF-8 strings. Igor's solution, which is admirably concise in comparison to this one, just skips them.
I've cast the char pointer to a byte (or uint8_t in <stdint.h>), so that the array indices are guaranteed to be non-negative.
This solution is probably needlessly long, but may serve as a starting point when trying to decode the characters (as opposed to just counting them).
Anyway, here goes:
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
static char utf8_len[256] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* cont */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* cont */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* cont */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* cont */
-1, -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
/*
* Return character count of an UTF-8 encodes string; -1 indicates
* a decoding error.
*/
int str_length(const char *s)
{
const uint8_t *p = (const uint8_t *) s;
int len = 0;
while (*p) {
int cl = utf8_len[*p];
if (cl <= 0) return -1;
len++;
p += cl;
}
return len;
}

Memory leak in C program, can't see where to free memory

I am writing a C program to generate Keys and test them in an encryption function. However since I have NEVER written a C program before, and I'm completely unused to having manage memory manually, I have run into a problem. I have a memory leak and to be honest, I don't know how to solve it. I understand I need to release the memory at some point but can't until I've run through all keys and I run out of memory before I get through all of them. Writing the program in a different language is NOT an option so please do not suggest that. The code that is leaking is shown below, any help would be appreciated.
EDIT: I know i haven't called a free function to free the memory. I don't see where I can put it because I need the memory until i run through all keys. Putting it outside the loops doesn't solve the problem because the leak occurs inside the loops
2nd EDIT: Posted the full program. I do not have the option of using data structures (i.e. the bool arrays) other than those shown because of how the DES encrypt function(which I did not write) works
#include <stdio.h>
#include <stdlib.h>
#include "des.h"
void dec2bin(bool *testaRR, bool *to_return, int convert);
int main(int argc, const char * argv[])
{
// insert code here...
bool testKey[56] = {
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1
};
bool testKey2[56] = {//intuitive key reversed for testing
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0
};
bool output[64];
bool input[64] = {//the reverse of below... DES bits are numbered left to right, in order of least to most significant so we must enter the bit values in reverse.
//forexample the binary vale of N is 01001110 but below is displayed as 01110010
1, 0, 0, 0, 1, 1, 0, 0,//1
0, 0, 0, 0, 1, 1, 0, 0,//0
1, 1, 0, 0, 0, 0, 1, 0,//C
1, 0, 1, 0, 0, 0, 1, 0,//E
1, 1, 0, 0, 1, 0, 1, 0,//S
0, 0, 1, 0, 1, 0, 1, 0,//T
1, 0, 1, 0, 0, 0, 1, 0,//E
0, 1, 1, 1, 0, 0, 1, 0 //N
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int i = y-4; i >= 0; i-=4)
printf("%X", input[i]+2*input[i+1]+4*input[i+2]+8*input[i+3]);//this is the conversion to hex code
printf(")\n");
/*
use char[] to store the key as set of
*/
/*bool input[64] = {//this is the given plaintext message in the intuitive order (opposite of what it is)
0, 1, 0, 0, 1, 1, 1, 0,//N
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 1, 0, 1, 0, 0,//T
0, 1, 0, 1, 0, 0, 1, 1,//S
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 0, 0, 0, 1, 1,//C
0, 0, 1, 1, 0, 0, 0, 0,//0
0, 0, 1, 1, 0, 0, 0, 1 //1
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int j = 0; j < y; j+=4)
printf("%X", input[j+3]+2*input[j+2]+4*input[j+1]+8*input[j]);//this is the conversion to hex code
printf(")\n");*/
bool test [8];
bool returned[8];
char keyphrase [8];
keyphrase[7] = 0;
for(int start = 65; start<=90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for(int two = 65; two<=90; two++){
keyphrase[1]=two;
for(int three = 65; three<=90; three++){
keyphrase[2]=three;
for(int four = 65; four<=90; four++){
keyphrase[3]=four;
for(int five = 65;five<=90;five++){
keyphrase[4]=five;
for( int six = 65; six <=90; six++){
keyphrase[5]=six;
for(int seven = 65; seven <=90; seven++){
keyphrase[6]=seven;
printf("%s \n", keyphrase);
}
}}
}
}
}
//once i fix the memory leak I will be calling the EncryptDes Function here and checking the outputblk agains the given cipher text
}
free(keyphrase);
int k = sizeof(testKey);
printf("(Test Key: ");
for (int z = 0; z < k; z+=7)
printf("%d", testKey[z+7]+2*testKey[z+6]+4*testKey[z+5]+8*testKey[z+4]+16*testKey[z+3]+32*testKey[z+2]+64*testKey[z+1]+ 128*testKey[z]);//this is the conversion to hex code
printf(")\n");
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
printf("(Output MSG: ");
for (int i = 0; i < x; i+=4)
printf("%X", output[i+3]+2*output[i+2]+4*output[i+1]+8*output[i]);//this is the conversion to hex code
printf(")\n");
return 0;
}
void dec2bin (bool *testaRR, bool *to_return, int convert)
{
printf("%d : ", convert);
printf("%c", convert);
printf("\n ");
//bool testaRR [8];
for(int st = 0; st<8; st++){
testaRR[st] = convert%2;
to_return[7-st] = testaRR[st];
//printf("%d :", 7-st);
//printf(" %d spot ", st);
convert = convert/2;
//testaRR stores the arrays in one direction
//to_return stores them in the other
//Example:
//65 = 01000001 testaRR least significant on the far right (m0st sig is in index 7)better for storage and keeping track of where the bits actually are in binary
//65 = 10000010 to_return least significant on the far left (same as DES) (most significant bit is index 0) good for printing to screen
}
You do not need dynamic memory management here.
Start with
char keyphrase[8];
keyphrase[7]=0;
instead of your malloc and you will be good to go. Your highest array index is 7 (the terminating NUL), and hence you need an array of 8 items (0..7).
If you really want to use malloc, simply a free() at the end will be fine, but you need to malloc 8 characters and set keyphrase[7] to 0 to do the terminating NUL still.
Here's a tested version that works:
#include <stdio.h>
/* compile with gcc -Wall -std=c99 keyphrase.c -o keyphrase */
int
main (int argc, char **argv)
{
char keyphrase[8];
keyphrase[7] = 0;
for (int start = 65; start <= 90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for (int two = 65; two <= 90; two++)
{
keyphrase[1] = two;
for (int three = 65; three <= 90; three++)
{
keyphrase[2] = three;
for (int four = 65; four <= 90; four++)
{
keyphrase[3] = four;
for (int five = 65; five <= 90; five++)
{
keyphrase[4] = five;
for (int six = 65; six <= 90; six++)
{
keyphrase[5] = six;
for (int seven = 65; seven <= 90; seven++)
{
keyphrase[6] = seven;
printf ("%s \n", keyphrase);
}
}
}
}
}
}
}
}
The real problem is the use of printf. You did not NULL terminate keyphrase, so every time you printf you overflow.
Also, to avoid the memory leak, simply replace char *keyphrase = (char *)malloc(7); with char keyphrase[8];.
You're calling malloc on the first line but I don't see a single free to release what you allocated. After all the loops are completed (i.e. usage of the allocated data is complete) you must call free(keyphrase);
New answer as the program has been modified.
You say your program is using all memory 'because I'm watching the available memory go down from over 4 free GB to about 5 MB'.
I'm guessing the answer is not the loops but these lines:
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
We can't see the source to or declaration of EncryptDES, but you aren't passing a length to it. If 0 is meant to be the length, this would explain it.
The next line however suggests that output is meant to be an array of 64 bytes (rather than 2 strings). But EncryptDES would have no way of knowing this.
I suggest you run the whole thing under valgrind to find out what is happening.

Removing row and column of matrix after looking for a value (C)

I made a question some hours ago but I get myself in a mess on what I had to do after finishing what I was asking on that question. All the solutions that the people gave me were ok, but useless for what I was really looking for as I didn't wrote the question as it has to be. I've to save an important position of a value, and it wasn't necessary to be saved on the other question to solve the problem. So here's the proper one.
(everything's is explained with an example above, understanding it is easy) I've a 8x8 matrix, and after choosing the row I desire, I want to get the three minimum elements of it, and choose one of this three randomly. Then, remove the row and column that contains this number. The thing is that I'dont know how to handle those three elements and remove the columns/rows. I just know how to get the minimum element, that is the following code.
int pieza[ROWS][COLS] = {
0, 2, 2, 5, 3, 2, 1, 1,
0, 4, 5, 2, 4, 3, 0, 0,
0, 4, 2, 2, 1, 2, 3, 2,
0, 3, 1, 5, 1, 2, 3, 4,
2, 5, 6, 5, 3, 1, 2, 7,
8, 2, 0, 0, 0, 2, 1, 1,
1, 2, 2, 1, 1, 6, 3, 4,
0, 1, 3, 2, 0, 0, 0, 0,
};
int myrow = 3; // the row I want to analyze
int index;
int min=0;
for (index=0;index<8;index++) {
printf("%d", piezas[myrow][index] );
if(piezas[myrow][index]<min)
min=piezas[myrow][index];
printf("\t\t");
}
printf("min: %d", min);
This is what I want to do. If the initial matrix is (which is always a nxn matrix):
{
0, 2, 2, 5, 3, 2, 1, 1,
0, 4, 5, 2, 4, 3, 0, 0,
0, 4, 2, 2, 1, 2, 3, 2,
0, 3, 1, 5, 1, 2, 3, 4,
2, 5, 6, 5, 3, 1, 2, 7,
8, 2, 0, 0, 0, 2, 1, 1,
1, 2, 2, 1, 1, 6, 3, 4,
0, 1, 3, 2, 0, 0, 0, 0,
};
And I choose row number 3:
0, 3, 1, 5, 1, 2, 3, 4,
The algorithm must choose the three minimum elements of that row.
0, 1, 1
And choose randomly one of these three. If, for example, it choose the first 'one'...
0, **1**, 1
... the algorithm must go to the 3th column of that line (becaue that was the position that was that '1') and remove the row and column, so the output matrix will be as follows, one dimension less than the original matrix (beucase you have removed a row and a column):
{
0, 2, 5, 3, 2, 1, 1,
0, 4, 2, 4, 3, 0, 0,
0, 4, 2, 1, 2, 3, 2,
2, 5, 5, 3, 1, 2, 7,
8, 2, 0, 0, 2, 1, 1,
1, 2, 1, 1, 6, 3, 4,
0, 1, 2, 0, 0, 0, 0,
};
I only know how to arrive to the line, but I'm having problems to handle three minimums because I'm having tons of problems pointers and I'm not a lot into C. Thanks in advance
Example to be sorted with the number of column.
#include <stdio.h>
#include <stdlib.h>
typedef struct pair {
int value, column;
} Pair;
int cmp(const void *a, const void *b){
Pair *pa = (Pair *)a;
Pair *pb = (Pair *)b;
return pa->value - pb->value;
}
int main(void){
int data[8] = {0, 3, 1, 5, 1, 2, 3, 4};
Pair data_pair[8];
int i;
for(i=0;i<8;++i){
data_pair[i].value = data[i];
data_pair[i].column = i;
}
qsort(data_pair, 8, sizeof(Pair), cmp);
for(i=0;i<3;++i)
printf("value = %d, column = %d\n", data_pair[i].value, data_pair[i].column);
return 0;
}
/* result
value = 0, column = 0
value = 1, column = 2
value = 1, column = 4
*/
#include <stdio.h>
#include <string.h>
#define SIZE 8
void delrow(int a[SIZE][SIZE], int row){
if(row < SIZE - 1)
memmove(&a[row], &a[row+1], (SIZE*SIZE - SIZE*(row+1))*sizeof(int));
};
void delcol(int a[SIZE][SIZE], int col){
int r;
if(col < SIZE - 1){
for(r=0;r<SIZE;++r){
memmove(&a[r][col], &a[r][col+1], (SIZE - (col+1))*sizeof(int));
}
}
}
int main(void){
int piezas[8][8] = {
0, 2, 2, 5, 3, 2, 1, 1,
0, 4, 5, 2, 4, 3, 0, 0,
0, 4, 2, 2, 1, 2, 3, 2,
0, 3, 1, 5, 1, 2, 3, 4,
2, 5, 6, 5, 3, 1, 2, 7,
8, 2, 0, 0, 0, 2, 1, 1,
1, 2, 2, 1, 1, 6, 3, 4,
0, 1, 3, 2, 0, 0, 0, 0,
};
//test
int row = 8, col = 8;
int r,c;
delrow(piezas, 3);
row -= 1;
for(r=0;r<row;++r){
for(c=0;c<col;++c)
printf("%2d", piezas[r][c]);
printf("\n");
}
printf("\n");
delcol(piezas, 1);
col -= 1;
for(r=0;r<row;++r){
for(c=0;c<col;++c)
printf("%2d", piezas[r][c]);
printf("\n");
}
return 0;
}
/* result
0 2 2 5 3 2 1 1
0 4 5 2 4 3 0 0
0 4 2 2 1 2 3 2
2 5 6 5 3 1 2 7
8 2 0 0 0 2 1 1
1 2 2 1 1 6 3 4
0 1 3 2 0 0 0 0
0 2 5 3 2 1 1
0 5 2 4 3 0 0
0 2 2 1 2 3 2
2 6 5 3 1 2 7
8 0 0 0 2 1 1
1 2 1 1 6 3 4
0 3 2 0 0 0 0
*/
Below is extract to get a nth smallest element from a selected row -Your first requirement before removing a row and column.
Copy the row
Sort the copied row (save index too while sorting)
Sorted index represents position of value in sorted order
pick 0 th or 1st or nth min indexing the sorted index.
------------ Very Draft code ---- try optimize -------
#include <stdio.h>
#include<memory.h>
void sortIndex(int *array, int *arrayIdx)
{
int i=0,j=0;
int temp=0;
int tempArr[4];
memcpy(tempArr, array, 4*sizeof(int));
for(i=0;i<4;i++)
{
printf("%d ",tempArr[i]);
}
printf("\n");
for(i=0;i<4;i++)
{
for(j=i+1;j<4;j++)
{
if(tempArr[i]>tempArr[j])
{
temp = arrayIdx[i];
arrayIdx[i]=arrayIdx[j];
arrayIdx[j]=temp;
temp = tempArr[i];
tempArr[i]=tempArr[j];
tempArr[j]=temp;
}
}
}
printf("Sorted array Index\n");
for(i=0;i<4;i++)
{
printf("%d ",arrayIdx[i]);
}
printf("\n");
printf("Sorted array Value\n");
for(i=0;i<4;i++)
{
printf("%d ",array[arrayIdx[i]]);
}
printf("\n");
}
int main ()
{
int array[4][4] = {{4,3,2,1},{7,5,4,3},{6,5,4,4},{5,5,2,1}};
int sortedIdx[4] = {0,1,2,3};
int i,ii;
for(i=0;i<4;i++)
{
for(ii=0;ii<4;ii++)
printf("%d ",array[i][ii]);
printf("\n");
}
printf("(Note:Count from 0). Which Row : ");
scanf("%d",&i);
sortIndex(array[i],sortedIdx);
printf("\n");
printf("(Nth smallest value)Give a N value (0 to 3): ");
scanf("%d",&ii);
printf(" (%d) smallest value in row (%d) is (%d)\n",ii,i,array[i][sortedIdx[ii]]);
printf("Now call function to remove Row (%d) and column (%d)\n",i,sortedIdx[ii]);
return 0;
}

Resources