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]
Related
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
#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
I wrote to program in C to attempt to print array elements in descending order. I wrote a nested loop which would find the maximum element of the array and the value of the element would be set to later 0. This process would be repeated for all the elements. However, in the output, I am getting the first 2-3 values as desired but the remaining values are garbage. Any suggestions?
int main() {
int i, j, n, k;
scanf("%d\n", &n);
int a[100], b[100];
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
int max = a[i];
for (j = i; j < n; j++) {
if (a[j] > max) {
max = a[j];
b[i] = max;
}
}
for (k = 0; k < n; k++) {
printf("%d", a[k]);
if (a[k] == b[i]) {
a[k] = 0;
}
}
printf("\n");
}
for (i = 0; i < n; i++) {
printf("%d ", b[i]);
}
}
The main issue is that you only set b[i] = max; when you find a new max, but since you initialized max to be a[i] it could happen that it already holds the maximum value. So the if never executes, therefore b[i] is not written and there's garbage value in it. You should move this line from the if after that for loop.
Another issue is that you initialize j with i in this loop. You should initialize it to 0.
The changed part:
for (j = 0; j < n; j++) {
if (a[j] > max) {
max = a[j];
}
}
b[i] = max;
here is my code, it is working but it prints only the min value and prints it as negative. what is wrong with this code ?
#include <stdio.h>
int main(void) {
double x[5][5],Max, Min;
int i, j;
for (i = 0; i<5; i++)
{
for (j = 0; j<5; j++)
scanf("%lf", &x[i][j]);
}
Max = x[0][0];
Min = x[0][0];
if (x[i][j] > x[0][0])
printf("Max= %f\n", x[i][j]);
else if (x[i][j] < x[0][0])
printf("Min = %f\n", x[i][j]);
return 0;
}
You never iterate through your arrays after receiving the inputs, because you are outside the for loops. You could do:
Max = x[0][0];
Min = x[0][0];
for(i = 0; i < 5; ++i)
{
for(j = 0; j < 5; ++j)
{
if (x[i][j] < Min) // Is current element smaller than Min?
Min = x[i][j]; // If so, update Min
if (x[i][j] > Max) // Is current element greater than Max?
Max = x[i][j]; // If so, update Max
}
}
printf("Max= %f\n", Max);
printf("Min= %f\n", Min);
You forgot to enclose the search of the minimum and the maximum in a loop.:)
Try the following
for (i = 0; i<5; i++)
{
for (j = 0; j<5; j++)
scanf("%lf", &x[i][j]);
}
Max = x[0][0];
Min = x[0][0];
for (i = 0; i<5; i++)
{
for (j = 0; j<5; j++)
{
if ( Max < x[i][j] )
{
Max = x[i][j];
}
else if ( x[i][j] < Min )
{
Min = x[i][j];
}
}
}
printf( "Max = %f\n", Max );
printf( "Min = %f\n", Min );
You need to iterate through all the values in the 2D array to check all the values of the 2D array and set the max and min variable accordingly:
Max = x[0][0]; //set max as the first element
Min = x[0][0]; //set min as the first element
for (i = 0; i<5; i++) //loop through each row
{
for (j = 0; j<5; j++) //loop through each column
{
if (x[i][j] > Max) //if current value is more than max
Max=x[i][j];
if (x[i][j] < Min) //if current value is less than min
Min=x[i][j];
}
}
printf("Max= %f\n", Max);
printf("Min = %f\n", Min);
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