So, for example, I have array: [1, 4, 9, 3, 9]
I need to find all max elements [9, 9] and their index [2, 4]
How can I do this? In C language
int i, pom, max;
max=*gradovi;
for(i=0;i<n;i++) {
if(*(gradovi+i)>max) {
max=*(gradovi+i);
pom=i;
}
if(*(gradovi+i)==max) {
pom=i;
}
}
return pom;
I need postions of all max elemenents, but this print just last
In any case you need a container that will store the indices of the elements with the maximum value.
You can allocate memory dynamically for such a container.
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
size_t max_elements( const int a[], size_t n, size_t **result )
{
*result = NULL;
size_t count = 0;
if ( n != 0 )
{
size_t max_i = 0;
count = 1;
for ( size_t i = 1; i < n; i++ )
{
if ( a[max_i] < a[i] )
{
max_i = i;
count = 1;
}
else if ( !( a[i] < a[max_i] ) )
{
++count;
}
}
*result = malloc( count * sizeof( size_t ) );
if ( *result != NULL )
{
for ( size_t i = max_i, j = 0; i < n && j < count; i++ )
{
if ( !( a[i] < a[max_i ] ) ) ( *result )[j++] = i;
}
}
}
return count;
}
int main(void)
{
int a[] = { 1, 4, 9, 3, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
size_t *result = NULL;
size_t count = max_elements( a, N, &result );
for ( size_t i = 0; i < count; i++ )
{
printf( "%zu: %d, ", result[i], a[result[i]] );
}
putchar( '\n' );
free( result );
return 0;
}
Its output is
2: 9, 4: 9,
If the returned value from the function is not equal to 0 but the pointer result was set to NULL then it means that there was a memory allocation error. You can check such a situation.
I'm stupid, it's simple solution:
void maks(int *gradovi, int n){
int i, pom, max;
max=*gradovi;
for(i=1;i<n;i++){
if(*(gradovi+i)>max){
max=*(gradovi+i);
}
if(*(gradovi+i)==max){
pom=i;
printf("Najvise zarazenih je u gradu sa indeksom: %d\n", pom);
}
}
}
Related
I'm trying to figure out how to "flip" an 2d array like this:
{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
{{7, 8, 9},
{4, 5, 6},
{1, 2, 3}}
Searching how to reverse/flip rows of 2D array just returns how to reverse the content of the rows, or some other variation that doesn't work like I need
Swapping rows work exactly the same way as swapping integers, only instead of assigning you need to copy the memory.
void reverseRows(size_t rows, size_t cols, int (*array)[cols])
{
int temp[cols];
size_t last = rows - 1, first = 0;
while(last > first)
{
memcpy(temp, array[first], sizeof(*array));
memcpy(array[first++], array[last], sizeof(*array));
memcpy(array[last--], temp, sizeof(*array));
}
}
#define N 3
int main(void)
{
int a[N][N] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", a[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
reverseRows(N,N,a);
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", a[i][j] );
}
putchar( '\n' );
}
}
https://godbolt.org/z/qxW3rG3sx
Here you are.
#include <stdio.h>
void swap_arrays( int *a1, int *a2, size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
int tmp = a1[i];
a1[i] = a2[i];
a2[i] = tmp;
}
}
int main(void)
{
enum { N = 3 };
int a[N][N] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", a[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
for ( size_t i = 0; i < N / 2; i++ )
{
swap_arrays( a[i], a[N - i - 1], N );
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", a[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3
4 5 6
7 8 9
7 8 9
4 5 6
1 2 3
I'm trying to write a function that copies all of the values in source1 which are also found in source2 into a destination and then returns the number of elements copied into the destination.
int common_elements(int length, int source1[length], int source2[length], int destination[length])
{
int counter = 0;
int i = 0;
while (i < length) {
int j = 0;
while (j < length) {
if ( source1[i] == source2[j]) {
destination[counter] = source1[i];
counter++;
}
j++;
}
i++;
}
return counter;
}
The problem is e.g. given (common_elements(5, {1,2,3,4,5}, {1,2,3,2,1}, [])), the correct input should be
1,2,3
return value: 3
However, the program is accounting for the duplicates and produces :
1,1,2,2,3
return value: 5
which is incorrect.
How can I remedy this?
In this while loop
int j = 0;
while (j < length) {
if ( source1[i] == source2[j]) {
destination[counter] = source1[i];
counter++;
}
j++;
}
you are counting all elements in the array source2 that are equal to the element source1[i].
I can suggest the following solution provided that the source arrays may not be changed within the function.
#include <stdio.h>
size_t common_elements( int destination[],
const int source1[],
const int source2[],
size_t n )
{
size_t counter = 0;
for ( size_t i = 0; i < n; i++ )
{
size_t number = 1;
for ( size_t j = 0; j < i; j++ )
{
if ( source1[i] == source1[j] ) ++number;
}
for ( size_t j = 0; number && j < n; j++ )
{
if ( source1[i] == source2[j] ) --number;
}
if ( number == 0 ) destination[counter++] = source1[i];
}
return counter;
}
int main(void)
{
enum { N = 5 };
int source1[N] = { 1, 2, 3, 4, 5 };
int source2[N] = { 1, 2, 3, 2, 1 };
int destination[N];
size_t n = common_elements( destination, source1, source2, N );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", destination[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3
this is what i have to make: c. It should be possible to remove an animal with a specified name. If more animals with the same name exist, it should remove all the animal with the same name.
this is my code:
void deleteAnimalByName(char *animalName, int *nrOfAnimals, ANIMAL *animalArray)
{
for(int i = 0; i < *nrOfAnimals; i ++)
{
if(strcmp((animalArray + i)->Name, animalName) == 0)
{
for(int j = i; j < *nrOfAnimals - 1; j++)
{
animalArray[j] = animalArray[j + 1];
}
(*nrOfAnimals)--;
}
}
}
the outcome after tyring to delete the animals with the same name:
Animals in shelter: 1
Name: ted
Species: Parrot
Age: 1
only one gets deleted, the other one stays. what could cause this?
For starters the function should be declared at least like
size_t deleteAnimalByName( ANIMAL *animalArray, size_t nrOfAnimals, const char *animalName );
And the function can be defined like
size_t deleteAnimalByName( ANIMAL *animalArray, size_t nrOfAnimals, const char *animalName )
{
size_t n = 0;
for ( size_t i = 0; i < nrOfAnimals; i++ )
{
if ( strcmp( animalArray[i].Name, animalName ) != 0 )
{
if ( n != i ) animalArray[n] = animalArray[i];
++n;
}
}
return n;
}
As for your approach then it at least is inefficient because you move all elements of the array one position left after finding an element that need to be deleted.
Here is a demonstrative program
#include <stdio.h>
#include <string.h>
typedef struct ANIMAL
{
char *Name;
} ANIMAL;
size_t deleteAnimalByName( ANIMAL *animalArray, size_t nrOfAnimals, const char *animalName )
{
size_t n = 0;
for ( size_t i = 0; i < nrOfAnimals; i++ )
{
if ( strcmp( animalArray[i].Name, animalName ) != 0 )
{
if ( n != i ) animalArray[n] = animalArray[i];
++n;
}
}
return n;
}
int main(void)
{
ANIMAL animalArray[] =
{
{ "hare" }, { "hare" }, { "fox" }, { "hare" }
};
size_t nrOfAnimals = sizeof( animalArray ) / sizeof( *animalArray );
nrOfAnimals = deleteAnimalByName( animalArray, nrOfAnimals, "hare" );
for ( size_t i = 0; i < nrOfAnimals; i++ )
{
printf( "%s ", animalArray[i].Name );
}
putchar( '\n' );
return 0;
}
The program output is
fox
I need to write a function that takes the elements in an array and changes the sign (ex. 3 --> -3 or -3 --> 3). l want use this array (int a[2][3] = { { 55,-44,},{1, -4},{6,11} };) instead of ( int a[] = { 5,6,-4};)
What should I do?
#include <stdio.h>
void change_sign(int* beta)
{
for (int i = 0; i < 3; i++) {
beta[i] = -beta[i];
}
}
int main(void)
{
int a[] = { 5, 6, -4};
for (int i = 0; i < 3; i++) {
printf("%d ", a[i]);
}
printf("\n");
change_sign(a);
for (int i = 0; i < 3; i++) {
printf("%d ", a[i]);
}
return 0;
}
For starters it seems you mean this array
int a[3][2] = { { 55,-44,},{1, -4},{6,11} };
instead of this
int a[2][3] = { { 55,-44,},{1, -4},{6,11} };
In any case if your compiler supports variable length arrays then the function can look like
void change_sign( size_t m, size_t n, int a[][n] )
{
for ( size_t i = 0; i < m; i++ )
{
for ( size_t j = 0; j < n; j++ )
{
a[i][j] = -a[i][j];
}
}
}
and call the function like
change_sign( 3, 2, a );
Otherwise the function can look like
#define N 2
void change_sign( int a[][N], size_t m )
{
for ( size_t i = 0; i < m; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
a[i][j] = -a[i][j];
}
}
}
and called like
change_sign( a, 3 );
I want to create an array of string like:
{{"zero","zero"},
{"zero","one"},
{"one","zero"},
{"one","one"}}
If I am not wrong I need 3D array. How can I create it with using dynamic memory allocation and how to return it from function? I mean how is my function should be (char ***getString etc...) and in my main function, how can I use it? Is char ***string = getstring(); work?
I didn't get I have to use 3d array yet, I tried it with 2 mallocs.
I create string of array in function like
char** editKeys = malloc(128 * sizeof(char*));
for (int i = 0; i < 128; i++ )
{
editKeys[i] = (char*) calloc(2, sizeof(char));
}
and it works in function. After that I calledmy function from main like
char **editFile=getEditFile();
printf("%s",editFile[0][0]);
at this point I failed and now I can't be sure is it 2d or 3d and my brain is failed me too. Can't think how can I turned it in 3d array.
If your compiler supports variable length arrays then you can allocate the array the following way
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void f( size_t n1, size_t n2, size_t n3, char s[n1][n2][n3] )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
enum { N1 = 4, N2 = 2, N3 = 5 };
char ( *p )[N2][N3] = malloc( sizeof( char[N1][N2][N3] ) );
f( N1, N2, N3, p );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
free( p );
return 0;
}
The program output is
"zero" "zero"
"zero" "one"
"one" "zero"
"one" "one"
Or you can indeed allocate an array of arrays of arrays.
For example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void f( char ***s, size_t n1, size_t n2, size_t n3 )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
enum { N1 = 4, N2 = 2, N3 = 5 };
char ***p = malloc( N1 * sizeof( char ** ) );
for ( size_t i = 0; i < N1; i++ )
{
p[i] = malloc( N2 * sizeof( char * ) );
for ( size_t j = 0; j < N2; j++ )
{
p[i][j] = malloc( N3 );
}
}
f( p, N1, N2, N3 );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
for ( size_t i = 0; i < N1; i++ )
{
for ( size_t j = 0; j < N2; j++ )
{
free( p[i][j] );
}
free( p[i] );
}
free( p );
return 0;
}
Atain the program output is
"zero" "zero"
"zero" "one"
"one" "zero"
"one" "one"
There is also a third approach where the number of pointers can be less than in the last case. All you need is to allocate a one dimensional array of pointers to first elements of two dimensional arrays.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { N1 = 4, N2 = 2, N3 = 5 };
void f( char ( **s )[N3], size_t n1 )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
char ( **p )[N3] = malloc( N1 * sizeof( char ( * )[N3] ) );
for ( size_t i = 0; i < N1; i++ )
{
p[i] = malloc( N2 * sizeof( char[N3] ) );
}
f( p, N1 );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
for ( size_t i = 0; i < N1; i++ )
{
free( p[i] );
}
free( p );
return 0;
}
And at last (I hope) there is a forth approach to declare at first an array of pointers to arrays.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { N1 = 4, N2 = 2, N3 = 5 };
void f( char * ( *s )[N2], size_t n1, size_t n3 )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
char * ( *p )[N2] = malloc( N1 * sizeof( char * [N2] ) );
for ( size_t i = 0; i < N1; i++ )
{
for ( size_t j = 0; j < N2; j++ )
{
p[i][j] = malloc( N3 * sizeof( char ) );
}
}
f( p, N1, N3 );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
for ( size_t i = 0; i < N1; i++ )
{
for ( size_t j = 0; j < N2; j++ ) free( p[i][j] );
}
free( p );
return 0;
}
If the compiler indeed supports variable length array then the first approach is the best.
Note: In the approaches sometimes some parameter as for example n2 is not used because I knwo that it is equal to 2. But in general it should be specified.