finding index of max value in an array in C - c

I want to find index of max value in array in C.
I write this code example:
maks=0;
for(i=0;i< N * N;i++) {
if(array[i]>maks) {
maks=(int) array[i];
k=i;
}
}
But this isn't work properly.Could you advise me another example please?
Best Regards...

k = 0;
max = array[k];
for (i = 0; i < N * N; ++i)
{
if (array[i] > max)
{
max = (int)array[i];
k = i;
}
}
Should work !

Below function accepts pointer to array with size of the array as arguments and returns the max index.
int max_index(float *a, int n)
{
if(n <= 0) return -1;
int i, max_i = 0;
float max = a[0];
for(i = 1; i < n; ++i){
if(a[i] > max){
max = a[i];
max_i = i;
}
}
return max_i;
}
Usage example,
float a[3] ={1.2,3.2,4.0};
cout<<max_index(a,3)<<endl; //will output 2, because a[2] element is the max

Related

I want to store elements of maximum and minimum frequency in the arr2 array ? But not able to

I want to store elements of maximum and minimum frequency in the arr2 array if there are more than one element of same frequency then both the elements should be stored ? But it is showing wrong results and i am not able to find what is the err. Can anyone help me with this. Any kind of help would be greatly appreciated.
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int arr2[n];
int prevcount = 0;
int k = 0;
// for finding max element
for (int i = 0; i < n; i++)
{
int count = 0;
//counting the number of times it has occured
for (int j = 0; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
}
}
// checking if the same element was not there in the new array
for (int i = 0; i < k; i++)
{
if (arr[i] == arr[k])
{
goto nextit;
}
}
//it will update the kth element if the count is greater than the prev count
if (prevcount < count)
{
arr2[k] = arr[i];
}
//if these both are same but the number is different then will iterate k by 1 and store that element as well
else if (prevcount == count)
{
k++;
arr2[k] = arr[i];
}
prevcount = count;
nextit:
}
// for finding min element
prevcount = 1000;
for (int i = 0; i < n; i++)
{
int count = 0;
for (int j = 0; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
}
}
// checking if the same element was not there in the new array if there is then go to the next iteration
for (int i = 0; i < k; i++)
{
if (arr[i] == arr[k])
{
goto nextit2;
}
}
if (prevcount > count)
{
arr2[k] = arr[i];
}
else if (prevcount == count)
{
k++;
arr2[k] = arr[i];
}
prevcount = count;
nextit2:
}
for (int i = 0; i < k; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
As #SparKot suggests, sorting the array makes the problem simple. Would you please try:
#include <stdio.h>
#include <stdlib.h>
// compare values numerically
int numeric(const void *a, const void *b)
{
return (*(int *)a < *(int *)b) ? -1 : (*(int *)a > *(int *)b);
}
int main()
{
int n, i, j;
int *arr; // input array
int *count; // count frequency: initialized to 0's by calloc
int min = 0; // minimum occurrences
int max = 0; // maximum occurrences
scanf("%d", &n);
if (NULL == (arr = malloc(n * sizeof(int)))) {
perror("malloc");
exit(1);
}
if (NULL == (count = calloc(n, sizeof(int)))) {
perror("calloc");
exit(1);
}
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
qsort(arr, n, sizeof(int), numeric);
// count the length of sequence of the same numbers
for (i = 0; i < n; i++) {
for (j = 0; i + j < n && arr[i] == arr[i + j]; j++) {
;
}
count[i] = j; // i'th element has length j
i += j - 1; // jump to next number
}
// find minimum and maximum frequencies
for (i = 0; i < n; i++) {
if (count[i]) {
if (min == 0 || count[i] < min) min = count[i];
if (max == 0 || count[i] > max) max = count[i];
}
}
// report the result
for (i = 0; i < n; i++) {
if (count[i] == min) {
printf("min frequency %d value %d\n", count[i], arr[i]);
}
if (count[i] == max) {
printf("max frequency %d value %d\n", count[i], arr[i]);
}
}
return 0;
}
Sample input (n=10):
6
1
2
5
1
2
3
1
3
6
Output:
max frequency 3 value 1
min frequency 1 value 5

Cannot call a function in a C program

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
void max_min(int arr[MAX_SIZE]) {
int i, j;
//maximum loop below
int max;
max = arr[0];
for (i = 0; i < sizeof(arr[i]); ++i) {
if ((arr[i]) > max) {
arr[i] = max;
}
if ((arr[i]) < max) {
break;
}
}
printf("Largest = %d", max);
//minimum loop below
int min;
min = arr[0];
for (j = 0; j < sizeof(arr[j]); ++i) {
if ((arr[j]) < min) {
arr[j] = min;
}
if ((arr[j]) > min) {
break;
}
}
int main(void) {
int arr[MAX_SIZE];
printf("Enter 10 elements for array > ");
int i;
for (i = 0; i < 10; ++i) {
scanf("%d", &arr[i]);
}
printf("Your array is: \n");
for (i = 0; i < 10; ++i) {
printf("%d", arr[i]);
printf(" ");
}
max_min(arr[MAX_SIZE]);
return 0;
}
I am trying to write a min, max loop as I typed above. The problem is when I don't call function and enter 10 number inputs, the array print loop works fine and it takes an array. When I call array by max_min(arr[MAX_SIZE]); the array print loop stops working and the program doesn't go further. Appreciate any help.
You just need to call your function with max_min(arr), not max_min(arr[MAX_SIZE]).
arr[MAX_SIZE] is trying to access the element at index 100 in your array, but valid indexes only range from 0 to 99.
Passing arr will pass the array pointer instead, which is what the function is expecting.
If you indent your code you'll find that the max_min function is lacking the end }. Also, max_min(arr[MAX_SIZE]); is only sending in one int to the function (and it's out of bounds) - also sizeof(arr[i]); is not going to work and neither will sizeof(arr); since arrays decay into pointers to the first element when passed as arguments to functions. You need to send in the number of elements in the array as an argument to the function.
You assign arr[i] = max; and arr[i] = min; in your loops when it should be max = arr[i]; and min = arr[i];.
You are also using the wrong variable in for (j = 0; j < sizeof(arr[j]); ++i) (note the ++i which should be ++j).
Example fix:
#include <stddef.h>
void max_min(int arr[], size_t count) {
int min = arr[0];
int max = arr[0];
for (size_t i = 1; i < count; ++i) {
if (arr[i] > max) max = arr[i];
else if (arr[i] < min) min = arr[i];
}
printf("Smallest = %d\nLargest = %d\n", min, max);
}
And call it with:
max_min(arr, 10); // where 10 is the number of values the user has entered

modified version of selection sort in C

I'm trying to modify selection sort in such a way that it puts the biggest element at the end of the array and then repeats selection sort for n - 1 items until n is 0. My code compiles but the output is still an unsorted array, please help me out!
#include <stdio.h>
void selection_sort(int arr[], int n);
int main ()
{
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
selection_sort(arr, n);
for(int i = 0; i < n; i++)
printf("%d\n", arr[i]);
return 0;
}
void selection_sort(int arr[], int n)
{
if(n <= 0)
return;
while(n > 0)
{
int max = 0;
int temp, x;
for(int i = 0; i < n; i++)
{
if(arr[i] >= max)
max = arr[i];
x = i;
}
temp = arr[n - 1];
arr[n - 1] = max;
arr[x] = temp;
selection_sort(arr, --n);
}
}
The part
if(arr[i] >= max)
max = arr[i];
x = i;
is wrong. You forgot to write {}, so x = i; is executed unconditionally and the swapping after the loop will always be done with the last element. This means that the swapping is done between the same element and it is effectively doing nothing.
Also note that doing both of loop while(n > 0) and recursion selection_sort(arr, --n); is wasteful. You will need only one of that.
Another note is initializing max with 0 will make it behave wrongly when all elements of the input array are negative.
Finally, you should format your code properly with consistent indentation.
Try this:
void selection_sort(int arr[], int n)
{
if(n <= 0)
return;
while(n > 0)
{
int max = arr[0];
int temp, x;
for(int i = 0; i < n; i++)
{
if(arr[i] >= max)
{
max = arr[i];
x = i;
}
}
temp = arr[n - 1];
arr[n - 1] = max;
arr[x] = temp;
--n;
}
}

Why is my code producing a segmentation fault?

So im working on this program that is supposed to take the pointer to an array and the array’s size (number of elements in the array)
as arguments, finds the place the index of the outlier, fixes the array in place (that is puts the outlier to a
place it is supposed to be), and returns the old index where the outlier was found. i finished my code but for some reason, somewhere in my main function its telling me there is a segmentation fault, i know its in my main function because it compiled and ran fine when it was just the original code. heres the code;
#include <stdio.h>
long long int fix_sorted_array(double* arr, unsigned long n)
{
double temp;
int i, j;
for ( i = 0; i < n - 1; i ++)
{
if (arr[i] > arr[i + 1])
{
for ( j = i + 1; j > 0; j --)
{
if (arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
return i + 1;
}
}
return -1;
}
int main()
{
int n;
int j;//declared variables
double arr[n];
printf("Enter elements of array : \n");
for ( int i = 0; i < n; i ++)
{
scanf("%lf", &arr[i]);
}
printf("Return index : %lld\n",fix_sorted_array (&arr[n], n));
printf("Array after : \n");
for ( j = 0; j < n; j ++)
{
printf("%.2lf", arr[j]);
}
}
You're passing an address outside the array to the function in this line:
printf("Return index : %lld\n",fix_sorted_array (&arr[n], n));
You want to pass the address of the start of the array, not the end, so it should be:
printf("Return index : %lld\n",fix_sorted_array (arr, n));
You also need to initialize n before you declare the array.
printf("How many numbers? ");
scanf("%d", &n);
double arr[n];
You have never accepted the value of n. Below code might help.
#include <stdio.h>
long long int fix_sorted_array(double* arr, unsigned long n)
{
double temp;
int i, j;
for ( i = 0; i < n - 1; i ++)
{
if (arr[i] > arr[i + 1])
{
for ( j = i + 1; j > 0; j --)
{
if (arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
return i + 1;
}
}
return -1;
}
int main()
{
int n;
int j;//declared variables
printf("Enter the number of elements in arrays");
scanf("%d",&n); // initialize the values of n
double arr[n];
printf("Enter elements of array : \n");
for ( int i = 0; i < n; i ++)
{
scanf("%lf", &arr[i]);
}
printf("Return index : %lld\n",fix_sorted_array (&arr[0], n)); // Also pass the value of starting index in array i.e. `arr[0]`
printf("Array after : \n");
for ( j = 0; j < n; j ++)
{
printf("%.2lf", arr[j]);
}
}

Position of array element

I'm working on program that should find the position of min and max element of array. The code I wrote finds the largest and smallest element but I can't figure out how to find the position it takes in array. Any tips would be greatly appreciated.
That is what I have for now:
int main ()
{
int i;
float a[8] = {
1.90, 0.75, 3.30, 1.10, 2.00, 4.50, 0.80, 2.50
}; /* array of ints */
printf("\nValues are: \n");
for(i = 0; i < 8; i++)
printf("%.2lf\n", a[i]);
float max = a[0];
float min = a[0];
for (i = 0; i < 8; i++) {
if (a[i] > max) {
max = a[i];
} else if (a[i] < min) {
min = a[i];
}
}
printf ("Maximum element in an array : %.2f\n", max);
printf ("Minimum element in an array : %.2f\n", min);
return 0;
}
Your program looks pretty good. All you need is to remember indexes of min and max element, like this:
int i_min = 0;
int i_max = 0;
if (a[i] > max) {
max = a[i];
i_max = i;
} else if (a[i] < min) {
min = a[i];
i_min = i;
}
and then print it.
You can slightly optimize it by storing only indexes of min and max element, but it probably does not matter that much.
Store position ie i instead of content of the a[i]
min=max=0;
for (i = 0; i < 8; i++)
{
if (a[i] > a[max])
{
max = i;
}
else if (a[i] < a[min])
{
min = i;
}
If you want to print the minimum and maximum number you can print a[min] and a[max]

Resources