How to find the maximum values in an array? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Example
Say that I have an array:
Array = (9, 1, 9, 9);
how can I loop through the array and print the value and position of each maximum value?
so it will print
Value: 9 and Position: 0
Value: 9 and Position: 2
Value: 9 and Position: 3
I am having trouble creating an algorithm for it.

The simplest way I can think of is to go over the array once and find the max value, then go over the array again and print the message with the current index each time you get to an element which is equal to the max you have found.

1) Iterate through all values in array to find MAX value
2) Iterate again to print position if current value == MAX value

Here is a pseudo code for one pass algorithm : -
list maxs;
int maxvalue = arr[0];
maxs = new list();
maxs.append(0);
for(int i=1;i<arr.length;i++) {
if(maxvalue<arr[i]) {
maxvalue = arr[i];
maxs = new list();
maxs.append(i);
}
else if(maxvalue==arr[i]) {
maxs.append(i);
}
}
print(maxvalue,maxs);

Related

Assign a multiple numbers to an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have a list of number : -1.5 , - 1 , -0,5 , ... , 100.
I want to assign them to an array.
E.g :
a[0] = -1.5;
a[1] = -1;
....
a[202] = 100;
Can you show me how to do it ?
Thanks.
If you want to initialize array with several values, you can use this:
int a[4] = {1, 2, 3, 4};
But it should not be used, if you have a lot of numbers. If you need to assign list of numbers, that you stated in question, you should describe them with mathmetical expression in cycle:
for(int i = 0; i < 203; i++)
{
a[i] = -1.5 + 0.5 * i;
}
If you want to assign array with list of values, that can't be described with mathematical expression, you should read them from file.

How does the code that prints array elements works? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}
Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.

Write functions that take a non empty array of doubles and its length as arguments and returns : [closed]

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 6 years ago.
Improve this question
Write functions that take a non empty array of doubles and its length as arguments and returns :
a) the sum of the items
b)the index of the maximum value
c) a boolean which indicates if the numbers are in strictly increasing order
Clue 1:
You can return a structure with 3 variables inside, something like this:
struct Resu{
double sum;
int max;
bool order;
};
Clue 2:
Resu homework(double d[],int l){
..
.. your code must be here
..
}
Clue 3:
Resu homework(double d[],int l){
Resu result;
..
.. your magic must be here
..
return result;
}

I need a variable that have a small range value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an input number that it's value is not always consistent, i mean the value change plus 1 or minus 1 and etc. So, i want to compare it with a const but have a small range value, for example a const int Dist that have value between 14 to 16. Is that possible to implement it on C programming? Please help me.
You can set constant for lower bound, and constant for upper bound and check if the value falls within the range.
Pseudocode:
int const LOWER_BOUND = 14;
int const UPPER_BOUND = 16;
if (input <= UPPER_BOUND && input >= LOWER_BOUND)
... logic here ...

Using one array print frequency of elements in array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Write an algorithm called occurrences that, given an array of numbers A, prints all the distinct values in A each followed by its number of occurrences.
For example, if A = <28, 1, 0, 1, 0, 3, 4, 0, 0, 3>, the algorithm should output the following five lines (here separated by a semicolon 28 1; 1 2; 0 4; 3 2; 4 1.
The algorithm may modify the content of A, but may not use any other memory.
Each distinct value must be printed exactly once.
Values may be printed in any order.
Possible solution will have 2 steps:
Sort your array
Iterate on array storing current value and number of its occurrences and printing current value/count pair when current value changes
This solution does not require extra memory and has complexity O(n*log(n)) as sorting is the "heaviest" part of algorithm

Resources