Reputation: 73
The purpose of this program is to read 10 double values in a one dimensional array and then search and print out the maximum and minimum values from the array on the standard output. I tried doing some pseudo code on paper to help me better understand this problem so far what I believe would work is a function that checks for min and max using a if
statement. Im not sure the proper format for using a function with an array due to my lack of knowledge in C. If you can give me a skeleton of how the function should look like using arrays that would be great because i'd like to learn and attempt the program.
Below i have attached the main program which loops 10 times and collects the user input as an array:
#include<stdio.h>
// main program
int main() {
int i;
double num[10];
for (i = 1; i <= 10; i++) {
printf("Enter a number: ");
scanf("%lf",&num);
}
return 0;
}
Upvotes: 0
Views: 7707
Reputation: 153338
A pedantic note for future consideration: Many systems allow for a double
that can take on a not-a-number value. In that case, a > b
will be false if either is a NaN
. Usually, when finding the min/max of a list containing NaN
, those values are ignored. Since the first number may be a NaN
, use not <=
instead of >
for maximum.
#include <math.h>
#include <stdio.h>
#define N 10
int main(void) {
size_t i;
double num[N];
for (i = 0; i < N; i++) {
printf("Enter a number: ");
if (scanf("%lf",&num[i]) != 1) return -1;
}
double maximum = num[0];
double minimum = num[0];
for (i = 1; i < N; i++) {
if (isnan(num[i])) continue;
if (!(num[i] >= minimum)) minimum = num[i];
if (!(num[i] <= maximum)) maximum = num[i];
}
printf("min: %e\n", minimum);
printf("max: %e\n", maximum);
}
Upvotes: 0
Reputation: 355
The best way is to sort the array containing elements in ascending or descending order and the show first and last value as minimum and maximum.
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(data[i]<data[j])
{
double temp;
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
Upvotes: 0
Reputation: 73
Thanks to all the information given by everyone I managed to answer this problem without the use of a function. Here is my code:
#include <stdio.h>
// main program
int main(void){
int i;
double num[10],max,min;
for (i=0;i<10;i++){
printf("Enter a number: ");
if (scanf("%lf",&num[i]) != 1){
puts("invalid input");
return 1;
}
}
// calculate max and minimum values and print them
min=num[0];
max=num[0];
for(int i=1;i<10;i++)
{
if(min>num[i])
{
min=num[i];
}
else if(max<num[i])
{
max =num[i];
}
}
printf("The max is %lf and the min is %lf",max,min);
return 0;
}
Upvotes: 0
Reputation: 84521
If, in addition to the other answers, you intend to write/call a function to find both the max and min values in your array of doubles, your first challenge is overcoming the axiom that only a single value (or pointer) can be returned by a function in C. There are several options available to determine the max and min in a function (or functions).
The first, and obvious, alternative is to write both a max and min function and call each. (but that would require iterating over your array twice -- that would be less than optimal). Your next option is to pass a separate array (of at least 2 double values) to store your max and min values in and either update the values or return a pointer to that array
(passing the array will automatically make values stored in the maxmin array available back in your calling function (main()
in your case), but to immediately use the values in same call, you can return the pointer to the max/min array for convenience. You can also return NULL
on error to indicate failure)
Or, since there are only 2 values needed, you can pass individual pointers to max and min as parameters and update the values pointed to by each within your function. Choosing some return type to indicate success/failure is also a good idea. int
in this case is as efficient as any other (better from a numeric conversion standpoint)
With that in mind a simple maxmin function taking a pointer to your array of double values, pointers to each max and min and finally the number of values in the array could be written like:
/** find the max and min in array of 'n' doubles `ad`.
* update 'max' and 'min' pointers so values are available back
* in calling function.
*/
int maxmin_dbl (double *ad, double *max, double *min, size_t n)
{
if (!ad || !max || !min) return 0; /* validate parameters */
*max = (double)LLONG_MIN; /* initialize max/min to sufficiently */
*min = (double)LLONG_MAX; /* large negative/positive values. */
size_t i;
for (i = 0; i < n; i++) {
if (ad[i] > *max) *max = ad[i]; /* test for new max */
if (ad[i] < *min) *min = ad[i]; /* test for new min */
}
return 1;
}
(note: if your data values are outside the range of LLONG_MIN
and LLONG_MAX
, you will need to adjust your initializations accordingly)
A short example program reading values from the filename given as the first argument (or from stdin
by default) and writing the array values and maximum and minimum to stdout
could be written as:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
enum { MAXD = 10 }; /* constant for no. of values */
int maxmin_dbl (double *ad, double *max, double *min, size_t n);
int main (int argc, char **argv) {
size_t i, n;
double max, min, tmp, ad[MAXD] = {0.0};
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
/* read double values from fp until at most 10 values read */
for (n = 0; n < MAXD && fscanf (fp, " %lf", &tmp) == 1; n++)
ad[n] = tmp;
if (fp != stdin) fclose (fp); /* close file if not stdin */
printf ("\narray values:\n\n"); /* output the values read */
for (i = 0; i < n; i++)
printf (" ad[%2zu] : %13.2lf\n", i, ad[i]);
if (maxmin_dbl (ad, &max, &min, n)) /* get max/min from array */
printf ("\n maximum : %.2lf\n minimum : %.2lf\n\n", max, min);
else {
fprintf (stderr, "error: maxmin_dbl failed.\n");
return 1;
}
return 0;
}
/** find the max and min in array of 'n' doubles `ad`.
* update 'max' and 'min' pointers so values are available back
* in calling function.
*/
int maxmin_dbl (double *ad, double *max, double *min, size_t n)
{
if (!ad || !max || !min) return 0; /* validate parameters */
*max = (double)LLONG_MIN; /* initialize max/min to sufficiently */
*min = (double)LLONG_MAX; /* large negative/positive values. */
size_t i;
for (i = 0; i < n; i++) {
if (ad[i] > *max) *max = ad[i]; /* test for new max */
if (ad[i] < *min) *min = ad[i]; /* test for new min */
}
return 1;
}
Example Use/Output
$ ./bin/arrdbl_maxmin <../dat/10int_nl.txt
array values:
ad[ 0] : 8572.00
ad[ 1] : -2213.00
ad[ 2] : 6434.00
ad[ 3] : 16330.00
ad[ 4] : 3034.00
ad[ 5] : 12346.00
ad[ 6] : 4855.00
ad[ 7] : 16985.00
ad[ 8] : 11250.00
ad[ 9] : 1495.00
maximum : 16985.00
minimum : -2213.00
Look over all the answers, those that directly address your error, and then let us know if you have any further questions.
Upvotes: 2
Reputation: 632
MikeCAT is right about the array indexing stuff. You want to read into num
at each of its indexes.
Additionally, about having a function that takes an array, there signature would be double arr[]
where arr is the name of the parameter. Note that C arrays don't store a size, so you often have to pass one along with the array:
double max(double arr[], int size) { ... }
int main(int argc, char **argv) {
double arr[10];
/* ... fill arr ... */
double maximum = max(arr, 10);
/* ... */
return 0;
}
Upvotes: 0
Reputation: 1246
If you're asking about how the function should work, try to understand this pseudocode:
Your function can be defined like so:
double max(double arr[], int size) {
// have a variable that stores the current max number
double currmax = -1;
// loop over arr and for each element,
// compare if it's greater than the current max number
// if it is, replace the current max number to this element's value
// after you've gone through all the elements in arr, return the current max number
return currmax;
}
You can call your function like so:
double the_biggest_number_ever = max(arr, 10);
The reason you have to pass the size of your array is because without it, the function max
will not know how many elements are there in your array. You can use it to loop through the elements in the max
function.
Upvotes: 1
Reputation: 75062
scanf("%lf",&num);
will invoke undefined behavior because double(*)[10]
is passed where double*
is expected.
You should read to each elements if you want to use an array. Note that array index in C starts from 0, not 1. You also should check if reading is successful.
#include<stdio.h>
// main program
int main(void){
int i;
double num[10];
for (i=1;i<=10;i++){
printf("Enter a number: ");
if (scanf("%lf",&num[i - 1]) != 1){
puts("read error");
return 1;
}
}
// calculate max and minimum values and print them
return 0;
}
or this code uses what seems better for range of i
.
#include<stdio.h>
// main program
int main(void){
int i;
double num[10];
for (i=0;i<10;i++){
printf("Enter a number: ");
if (scanf("%lf",&num[i]) != 1){
puts("read error");
return 1;
}
}
// calculate max and minimum values and print them
return 0;
}
Upvotes: 2
Reputation: 53
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
maximum = array[0];
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
return 0;
}
Here you can make array length and get location where is highest number
Upvotes: 0