Make a function that Replace elements between two arrays [closed] - c

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 2 years ago.
Improve this question
Need to make a function that can replace first n elements from array A with last n elements of array B.
arguments in function should be like (arrayA, arrayB, n)
Works like:
int A[ ] = {1, 2, 3, 4, 5};
int B[ ] = {6, 7, 8, 9, 10};
function name(A[ ], B[ ], 2)
Output:
int A[ ] = {9, 10, 3, 4, 5};
int B[ ] = {6, 7, 8, 1, 2};

void *mymemcpy(void *a, const void *b, size_t size)
{
unsigned char *ac = a;
const unsigned char *bc = b;
while(size--) *ac++ = *bc++;
return a;
}
void replace(void *a, const void *b, size_t nelem, size_t elemsize, size_t size_b)
{
const char *bc = b;
mymemcpy(a, b + (size_b - nelem) * elemsize, nelem * elemsize);
}
int main(void)
{
int a[] = {1,2,3,4,5,6};
int b[] = {1,2,3,4,5,6,7,8,9,0};
replace(a,b,3,sizeof(a[0]), sizeof(b)/sizeof(b[0]));
for(size_t index = 0; index < sizeof(a)/sizeof(a[0]); index++)
{
printf("%2d ", a[index]);
}
printf("\n");
}
I would not use it directly as your teacher probably will not believe you.

Here you are.
#include <stdio.h>
void swap_n( int *a, int *b, size_t n )
{
while ( n-- )
{
int tmp = *a;
*a++ = *b;
*b++ = tmp;
}
}
int main(void)
{
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 6, 7, 8, 9, 10 };
const size_t N1 = sizeof( a ) / sizeof( *a );
const size_t N2 = sizeof( b ) / sizeof( *b );
for ( size_t i = 0; i < N1; i++ )
{
printf( "%2d ", a[i] );
}
putchar( '\n' );
for ( size_t i = 0; i < N2; i++ )
{
printf( "%2d ", b[i] );
}
putchar( '\n' );
size_t n = 2;
swap_n( a, b + N2 - n, n );
for ( size_t i = 0; i < N1; i++ )
{
printf( "%2d ", a[i] );
}
putchar( '\n' );
for ( size_t i = 0; i < N2; i++ )
{
printf( "%2d ", b[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3 4 5
6 7 8 9 10
9 10 3 4 5
6 7 8 1 2
Pay attention to that the argument n of the function swap_n shall be not greater than the number of elements in the array a and in the array b.
Using the function swap_n you can exchange any parts of the size n of two arrays. For example to exchange two elements third and fourth of the array a with second and third elements of the array b you could write
size_t n = 2;
swap_n( a + 2, b + 1, n );
In this case the program output will be
1 2 3 4 5
6 7 8 9 10
1 2 7 8 5
6 3 4 9 10

Related

Im trying to find the max and min value and its respective index. However I cant get the indexmin

Find the minimum element of the array and its corresponding index.
I can't get the the minimum index to work. Do I add else statement under each if statement?
#include<stdio.h>
int main()
{
int array[10]={1,2,3,4,5,6,7,8,9,10} , i;
**//finding max and min, and its respective index**
int max = array[0] , min = array[0];
int indmin , indmax;
for( i = 0 ; i < 10 ; i++ )
{
if(array[i] > max)
{
max = array[i];
indmax = i;
}
if(array[i] < min)
{
min = array[i];
indmin = i;
}
}
//print the max and min value and its indexs
printf("\nMaximum element is %d\t index is %d", max , indmax);
printf("\nMinimum element is %d\t index is %d", min , indmin);
}
Initialize indmin and indmax. When defining the array leave out the size so it's derived from the data. When iterating over the array use sizeof(array) / sizeof(*array) to let compiler determine the size of the array instead of hard-coding it. Minimize scope of variable i. Use a function to print output for less duplication:
#include <stdio.h>
void print(const char *prompt, int value, int index) {
printf("%s element is %d\t index is %d\n", prompt, value, index);
}
int main() {
int array[]={1,2,3,4,5,6,7,8,9,10};
int min = array[0];
int indmin = 0;
int max = array[0];
int indmax = 0;
for(int i = 0; i < sizeof(array) / sizeof(*array); i++) {
if(array[i] > max) {
max = array[i];
indmax = i;
}
if(array[i] < min) {
min = array[i];
indmin = i;
}
}
print("Maximum", max, indmax);
print("Minimum", min, indmin);
}
You could refactor this by creating a struct to keep the value and index together:
#include <stdio.h>
struct value_index {
int value;
int index;
};
void print(const char *prompt, struct value_index *vi) {
printf("%s element is %d\t index is %d\n", prompt, vi->value, vi->index);
}
int main() {
int array[]={1,2,3,4,5,6,7,8,9,10};
struct value_index min = { array[0], 0 };
struct value_index max = { array[0], 0 };
for(int i = 0; i < sizeof(array) / sizeof(*array); i++) {
if(array[i] > max.value) {
max.value = array[i];
max.index = i;
}
if(array[i] < min.value) {
min.value = array[i];
min.index = i;
}
}
print("Maximum", &max);
print("Minimum", &min);
}
Or you could realize that you only need the original array along with the two indices. To make my version even better than #Fe2O3's answer, I used a macro to make mine smaller (and if bait works then I will claim mine is easier to read) :-)
#include <stdio.h>
void print(const char *prompt, int *arr, int index) {
printf("%s element is %d\t index is %d\n", prompt, arr[index], index);
}
int main() {
int array[]={1,2,3,4,5,6,7,8,9,10};
int indmin = 0;
int indmax = 0;
for(int i = 0; i < sizeof(array) / sizeof(*array); i++) {
#define CMP_AND_SET(OP, V) if(array[i] OP array[V]) V = i
CMP_AND_SET(<, indmin);
CMP_AND_SET(>, indmax);
#unset CMP_AND_SET
}
print("Maximum", array, indmax);
print("Minimum", array, indmin);
}
Building on #Fe2O3's branchless idea combined with an initialized array which I find to compact and quite readable:
indmin = (int[]) { indmin, i }[array[i] < array[indmin]];
indmax = (int[]) { indmax, i }[array[i] > array[indmax]];
By using (a < b) <=> -1 * (-a > -b) you can write the last one as (note: UB if array contains INT_MIN):
indmax = (int[]) { indmax, i }[-array[i] < -array[indmax]];
I would use a local macro to reduce code duplication by using macro to generate either the first version by passing in the operator (see above) or the 2nd version by passing in a factor F:
#define MINMAX(V, F) V = (int[]) { V, i }[F * array[i] < F * array[V]]
indmin = MINMAX(indmin, 1);
indmax = MINMAX(indmax, -1);
I am totally cheating but you can shuffle the min and max elements to fixed positions within the source array. No storage overhead. This would be the opposite of branchless.
#include <stdio.h>
void print(const char *prompt, int value) {
printf("%8s = %3d\n", prompt, value);
}
int swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
return 0;
}
int main(void) {
int arr[] = { 1, 2, 3, 4, 42, 5, -42, 6, 7, 8, 9, 10 };
const int min = 0;
const int max = sizeof arr/sizeof *arr - 1;
for(int i = 1; i < max + 1; i++ )
arr[i] < arr[min] && swap(arr + i, arr + min) ||
arr[i] > arr[max] && swap(arr + i, arr + max);
print("min", arr[min]);
print("max", arr[max]);
}
Leaving variables uninitialised is asking Demon of Hard-To-Find Bugs to co-author your code. Define variables close to where they are used to increase clarity. And, don't define more variables than you need. (Common beginner mistake to make another copy "just in case"...)
// use the spacebar to increase readability
#include <stdio.h>
int main() {
// let the compiler assign the size of an initialised array
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// use fewer variables
int indmin = 0, indmax = 0;
// don't compare an element (arr[0]) to itself
for( int i = 1 ; i < sizeof array/sizeof array[0]; i++ )
if( array[ i ] > array[ indmax ] )
indmax = i; // updated
else
if( array[ i ] < array[ indmin ] )
indmin = i; // updated
// don't add unnecessary braces (imho)
// this isn't the 17th century in need of needless filligree.
// use '\n' at the END of output. sometimes needed to 'flush' output buffer
printf("Maximum element is %d\t index is %d\n", array[ indmax ] , indmax);
printf("Minimum element is %d\t index is %d\n", array[ indmin ] , indmin);
return 0;
}
Maximum element is 10 index is 9
Minimum element is 1 index is 0
EDIT:
So, there's a friendly competition going on in this question... :-)
How's this:
#include <stdio.h>
int main() {
// let the compiler assign the size of an initialised array
// use shorter names to expose operations (not lengthy variable names)
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int iMin = 0, iMax = 0;
// don't compare an element to itself
for( int i = 1; i < sizeof arr/sizeof arr[0]; i++ ) {
// use "branchless" coding for speed.
int n = arr[i] > arr[iMax];
iMax = n*i + !n*iMax;
n = arr[i] < arr[iMin];
iMin = n*i + !n*iMin;
}
// reduce duplication of static data
char *fmt = "%s element is %d\t index is %d\n";
printf( fmt, "Maximum", arr[ iMax ], iMax );
printf( fmt, "Minimum", arr[ iMin ], iMin );
return 0;
}
Same output.
Ball's in your court #Allan :-)
EDIT:
There has been an advance on the last offering that needs to be addressed...
Here we go whole-hog, splurging-out with a third 'container' (mm[0]) to catch all those indexes that satisfy neither conditional ('<' & '>'). AND, a 4th 'container' (mm[3]) that doesn't change from being initialised to 0, the index of the 1st element. Besides being cryptic (not advised), this may-or-may-not be more expensive with its multiple array offset calculations... But, it's fun to look at...
#include <stdio.h>
int main() {
// added two elements to show 0 and nElem are not 'flukes'
// this really does find and report the min/max values
int arr[] = { 1, 2, 3, 4, 42, 5, -42, 6, 7, 8, 9, 10 };
int i, mm[1 + 2 + 1] = { 0 };
// assign 'i' to mm[ 0 or 1 or 2 ]. 0=latest, 1=max, 2=min, (3 unaffected)
for( i = 1; i < sizeof arr/sizeof arr[0]; i++ )
mm[ (arr[i] > arr[mm[1]]) + 2*(arr[i] < arr[mm[2]]) ] = i;
mm[ 0 ] = i-1; // always pick up the last index. Thanks #A Wind!
// now... this is getting silly!!
char *fmt = "%5s = %3d # arr[%d]\n";
char *type[] = { "last", "max", "min", "first" };
i = 3; do printf( fmt, type[i], arr[ mm[i] ], mm[i] ); while( --i >= 0 );
return 0;
}
first = 1 # arr[0]
min = -42 # arr[6]
max = 42 # arr[4]
last = 10 # arr[11]
Y'know... This might be interesting to try to apply to 3-way branching as is needed for binary searching; determining '<', '=' or '>'... Hmmm...
EDIT: (another variation on a theme at the suggestion of a worthy competitor :-)
#include <stdio.h>
int main() {
struct {
char *label;
int ind;
} mm[] = {
{ "last" },
{ "maximum" },
{ "minimum" },
{ "first" },
};
int i, arr[] = { 1, 2, 3, 4, 42, 5, -42, 6, 7, 8, 9, 10 };
for( i = 1; i < sizeof arr/sizeof arr[0]; i++ )
mm[ (arr[i] > arr[mm[1].ind]) + 2*(arr[i] < arr[mm[2].ind]) ].ind = i;
mm[ 0 ].ind = --i; // always pick up the last index. Thanks #A Wind!
for( i = sizeof mm/sizeof mm[0]; --i >= 0; /* space for rent */ )
printf( "%8s = %3d # arr[%d]\n", mm[i].label, arr[ mm[i].ind ], mm[i].ind );
return 0;
}
EDIT:
Trying to cover ALL the bases, here are three more ways to skin a cat
/* Minimalist */
#include <stdio.h>
int main() {
int mm[3] = { 0 },
arr[] = { 1, 2, 3, 4, 42, 5, 6, 7, 8, 9, 10 },
i = sizeof arr/sizeof arr[0];
while( --i )
mm[ 2*(arr[i] > arr[mm[2]]) + (arr[i] < arr[mm[1]]) ] = i;
char *fmt = "arr[%d] = %3d M%simum\n";
printf( fmt, mm[1], arr[mm[1]], "in" );
printf( fmt, mm[2], arr[mm[2]], "ax" );
return 0;
}
/* Recursive - for brevity, excluding the index; just reporting two values */
#include <stdio.h>
int amin( int a[], int i ) { // NB: "amin", not "main"
int j = --i ? amin( a, i ) : i;
return (a[j]<a[i])*j + (a[j] > a[i])*i;
}
int amax( int a[], int i ) {
int j = --i ? amax( a, i ) : i;
return (a[j]>a[i])*j + (a[j]<a[i])*i;
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, },
sz = sizeof arr/sizeof arr[0];
char *fmt = "M%simum: %3d\n";
printf( fmt, "in", arr[ amin(arr, sz) ] );
printf( fmt, "ax", arr[ amax(arr, sz) ] );
return 0;
}
/* And, simply brute force using a library function */
#include <stdio.h>
#include <stdlib.h>
int cmp( const void *a, const void *b ) { return *(int*)a - *(int*)b; }
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
sz = sizeof arr/sizeof arr[0];
qsort( arr, sz, sizeof arr[0], cmp );
char *fmt = "M%simum: %3d\n";
printf( fmt, "in", arr[ 0 ] );
printf( fmt, "ax", arr[ --sz ] ); // again, thanks to #A Wind
return 0;
}
Many ways to skin a cat.

writing function with pointers in c

I wrote this code :
#include <stdio.h>
void transpose(int *Al[]){
int x=0;
int z=0;
int k=1;
while (*Al[z] != "\0") {
int c=*Al[z];
if (c>x)
x=c;
z++;
}
printf("%d ",x);
for(int o=0;o<6;o++){
for(int i=0 ;i<x;i++ ) {
int *p = Al[i];
int l=*p;
if(k<l)
printf("%d ",*(p+k));
else
printf(" ");
}
k++;
printf("\n");
}
}
int main() {
int A[] = {5, -5, 14, 5, 2};
int B[] = {3, 6, 11};
int C[] = {4, 1, -3, 4};
int D[] = {6, 2, 7, 1, 8, 2};
int E[] = {2, 15};
int F[] = {3, 4, -2};
int *All[] = {A, B, C, D, E, F, NULL};
transpose(All);
}
The function gets an array that points to different array I need to print the arrays using pointers
the output should be :
-5 6 1 2 15 4
14 11 -3 7 -2
5 4 1
2 8
2
But this code doesn't print anything.
Also the arrays that it points at the first value is the size of the array.
I tried this :
void transpose(int *Al[]){
int x=0;
int z=0;
int k=1;
for(int o=0;o<5;o++){
for(int i=0 ;i<6;i++ ) {
int *p = Al[i];
int l=*p;
if(k<l)
printf("%d ",*(p+k));
else
printf(" ");
}
k++;
printf("\n");
}
}
It worked only I need to replace the five and six in the loop
Five is the biggest size of all he arrays -1 so I will know how many lines to print and six to how many arrays in All so I can know how many colums I should print. Is there a solution for this?
The condition in the while loop
while (*Al[z] != "\0") {
does not make a sense. The expression *Al[z] has the type int while the string literal "\0" has the type char *.
Also it is unclear why there is present the magic number 6 in this loop
for(int o=0;o<6;o++){
There is no need to calculate explicitly the number of columns because you have a sentinel value equal to NULL.
I can suggest for example the following solution
#include <stdio.h>
void transpose( int * Al[] )
{
int rows = 0;
for ( int **p = Al; *p != NULL; ++p )
{
if ( rows < **p ) rows = **p;
}
if ( rows ) --rows;
for ( int i = 0; i < rows; i++ )
{
for ( int **p = Al; *p != NULL; ++p )
{
if ( i + 1 < **p ) printf( "%2d ", *( *p + i + 1 ) );
else printf( " " );
}
putchar( '\n' );
}
}
int main(void)
{
int A[] = {5, -5, 14, 5, 2};
int B[] = {3, 6, 11};
int C[] = {4, 1, -3, 4};
int D[] = {6, 2, 7, 1, 8, 2};
int E[] = {2, 15};
int F[] = {3, 4, -2};
int *All[] = { A, B, C, D, E, F, NULL };
transpose( All );
return 0;
}
The program output is
-5 6 1 2 15 4
14 11 -3 7 -2
5 4 1
2 8
2

C kind of sorting

Ok so a function which arranges the elements in an array in such a way that all elements
smaller than a given value are placed into positions to the left of the elements that are larger
than the given value.
For instance if the array contents are {4,6,2,9,1,7,3,10} and x is given as 5, then
{4,3,2,1,9,7,6,10} is a possible solution, since all elements smaller than 5 are to the left
of the elements larger than 5.
Also, using brackets [ ] is ​ forbidden ​ except for defining the array in the main function.
Also, implement a function which prints the contents of an array. Both functions must be
implemented recursively.
you are allowed to access each element of the array only for once.
ok so this "challenge" and I dont know if it is possible with the given restrictions. I have tried to make it with a while loop and then convert it to recursive somehow but you are not allowed to change the parameters as well. Does anyone know a solution.
I have written something but its garbage.
#include <stdio.h>
#define length 8
void selection(int array[],int size, int x){
int i=0;
int temp;
if(( array[i]>x ) && (array[i] > array[i+1])){
temp=array[i+1];
array[i+1]=array[i];
array[i]=temp;
i++;
selection(array+1,size-1,x)
}
else if(( array[i] > x) && ( array[i+1] > array[i])){
i++;
}
//This is not correct
}
void printArray(int arr[], int start, int len)
{
if(start >= len)
return;
printf("%d ", arr[start]);
printArray(arr, start + 1, len);
}
int main(){
int array[length]={6,4,2,9,1,7,3,10};
int x=5;
selection(array,length,x);
printArray(array,0,length);
return 0;
}
I havent implemented the a recursive solution because things I tried kept giving segmentation faults because I was reaching outside the array.
Can anyone do this recursivly without for or while. I guess you need to split the array and look at it half by half
Here you are.
#include <stdio.h>
void partition( int a[], size_t n, int pivot )
{
if ( !( n < 2 ) )
{
if ( *a < pivot )
{
partition( a + 1, n - 1, pivot );
}
else
{
if ( *( a + n - 1 ) < pivot )
{
int tmp = *a;
*a = *( a + n - 1 );
*( a + n - 1 ) = tmp;
partition( a + 1, n - 2, pivot );
}
else
{
partition( a, n - 1, pivot );
}
}
}
}
int main(void)
{
int a[] = { 4, 6, 2, 9, 1, 7, 3, 10 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
int pivot = 5;
partition( a, N, pivot );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
return 0;
}
The program output is
4 6 2 9 1 7 3 10
4 3 2 1 9 7 6 10
Or also with a recursive definition of the function printArray.
#include <stdio.h>
void partition( int a[], size_t n, int pivot )
{
if ( !( n < 2 ) )
{
if ( *a < pivot )
{
partition( a + 1, n - 1, pivot );
}
else
{
if ( *( a + n - 1 ) < pivot )
{
int tmp = *a;
*a = *( a + n - 1 );
*( a + n - 1 ) = tmp;
partition( a + 1, n - 2, pivot );
}
else
{
partition( a, n - 1, pivot );
}
}
}
}
void printArray( const int a[], size_t n )
{
if ( n )
{
printf( "%d ", *a );
printArray( a + 1, n - 1 );
}
else
{
putchar( '\n' );
}
}
int main(void)
{
int a[] = { 4, 6, 2, 9, 1, 7, 3, 10 };
const size_t N = sizeof( a ) / sizeof( *a );
printArray( a, N );
int pivot = 5;
partition( a, N, pivot );
printArray( a, N );
return 0;
}
The recursive function printArray also can be defined the following way
void printArray( const int a[], size_t n )
{
n == 0 ? ( void )putchar( '\n' )
: ( printf( "%d ", *a ), printArray( a + 1, n - 1 ) );
}

Select certain elements in array to create new array

How could I select a certain number of elements from an array by giving a start and ending index number to create a new array?
For example, if my original array was {1,2,3,4,5,6}, and I say x=0 and y=2 for their index values, I would have a new array that is {1,2,3}.
Thank you.
If your compiler supports variable length arrays then you can do this the following way
#include <stdio.h>
#include <string.h>
int main(void)
{
int a[] = { 1, 2, 3, 4, 5, 6 };
size_t n1 = 0, n2 = 2;
int b[n2 - n1 + 1];
memcpy( b, a + n1, ( n2 - n1 + 1 ) * sizeof( int ) );
size_t n = sizeof( b ) / sizeof( *b );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", b[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3
Otherwise the new array should be allocated dynamically as for example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int a[] = { 1, 2, 3, 4, 5, 6 };
size_t n1 = 0, n2 = 2;
size_t n = n2 - n1 + 1;
int *b = malloc( n * sizeof( *b ) );
memcpy( b, a + n1, n * sizeof( int ) );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", b[i] );
}
putchar( '\n' );
free( b );
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_arr(int* arr, int len) {
for (int i = 0; i < len; i ++) {
printf("%d", arr[i]);
if (i != len - 1) {
printf(" ");
} else {
printf("\n");
}
}
}
int main() {
int arr1[] = {1, 2, 3, 4, 5, 6};
int start, end;
printf("input the beginning and the end: ");
scanf("%d %d", &start, &end);
int len = end - start + 1;
// we use malloc to dynamically allocate an array of the correct size
int* arr2 = (int*)malloc(len * sizeof(int));
// we use memcpy to copy the values
memcpy(arr2, arr1 + start, len * sizeof(int));
print_arr(arr1, 6);
print_arr(arr2, len);
// we have to free the memory
free(arr2);
return 0;
}

Sorting 5 arrays in one function

If I have 5 arrays and one array of pointers that contains all of the 5 arrays, and I need to write a function that will sort every single one of the arrays using the pointers array only, how can I do that?
The function needs to sort every single one of the array starting from the index 1 (!) and not 0.
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int * parr[] = { arr1, arr2, arr3, arr4, arr5 };
I know how to sort one array but I got a bit lost when I tried to sort every single one of the arrays using the pointers array in the most efficient way possible. Maybe there's an option to sort every array but in one loop? Because it seems a bit weird to do a whole "for" loop for every single one of the arrays
NOTE the first element of each array indicates the size of each one. For example : in arr1[0] is 3 so the amount of numbers that will come after the index 0 in that array is 3 (9,6,7).
You can call in a loop standard C function qsort for each element of the array parr.
For example
#include <stdio.h>
#include <stdlib.h>
int cmp( const void *a, const void *b )
{
return ( *( const int * )b < *( const int * )a ) -
( *( const int * )a < *( const int * )b );
}
int main(void)
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int * parr[] = { arr1, arr2, arr3, arr4, arr5 };
const size_t N = sizeof( parr ) / sizeof( *parr );
for ( size_t i = 0; i < N; i++ )
{
qsort( parr[i] + 1, parr[i][0], sizeof( int ), cmp );
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < parr[i][0] + 1; j++ )
{
printf( "%d ", parr[i][j] );
}
putchar( '\n' );
}
return 0;
}
The program output is
3 6 7 9
2 5 5
0
1 6
4 1 2 5 6

Resources