You must pass a valid array with at least one member to this function:
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int
maxValue(int myArray[], size_t size) {
/* enforce the contract */
assert(myArray && size);
size_t i;
int maxValue = myArray[0];
for (i = 1; i < size; ++i) {
if ( myArray[i] > maxValue ) {
maxValue = myArray[i];
}
}
return maxValue;
}
int
main(void) {
int i;
int x[] = {1, 2, 3, 4, 5};
int *y = malloc(10 * sizeof(*y));
srand(time(NULL));
for (i = 0; i < 10; ++i) {
y[i] = rand();
}
printf("Max of x is %d\n", maxValue(x, sizeof(x)/sizeof(x[0])));
printf("Max of y is %d\n", maxValue(y, 10));
return 0;
}
By definition, the size of an array cannot be negative. The appropriate variable for array sizes in C is size_t, use it.
Your for loop can start with the second element of the array, because you have already initialized maxValue with the first element.
So I made a function to determine the maximum element of an array of integers. But when I try to print the value of the maximum element, it gives me a weird large number 1619117764., and isn't what I'm expecting. What am I doing wrong? Thank you so much in advance.
This is the error message:
C:\Users\USER\Desktop\Discrete Math\Max Value.c|25|warning: passing argument 1 of 'max_value' makes pointer from integer without a cast [-Wint-conversion]|
Here is my code:
#include <stdio.h>
int max_value (int input[])
{
int size=10, max;
max=input[0];
for(int i=1;i<size;i++)
{
if(max<input[i])
{
max = input[i];
}
}
return max;
}
int main ()
{
int x, max;
int input[] = {2, 3, 5, 10, 15, 42, 28, 88, 92};
x = max_value(max);
printf("/n The Max is: %d" ,x);
return 0;
}MIN and MAX in C - Stack Overflow
Function in C to find max element in an array - Stack Overflow
C functions in finding a max value - Stack Overflow
How to find max and min? (C-Program) - C++ Forum
You must pass a valid array with at least one member to this function:
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int
maxValue(int myArray[], size_t size) {
/* enforce the contract */
assert(myArray && size);
size_t i;
int maxValue = myArray[0];
for (i = 1; i < size; ++i) {
if ( myArray[i] > maxValue ) {
maxValue = myArray[i];
}
}
return maxValue;
}
int
main(void) {
int i;
int x[] = {1, 2, 3, 4, 5};
int *y = malloc(10 * sizeof(*y));
srand(time(NULL));
for (i = 0; i < 10; ++i) {
y[i] = rand();
}
printf("Max of x is %d\n", maxValue(x, sizeof(x)/sizeof(x[0])));
printf("Max of y is %d\n", maxValue(y, 10));
return 0;
}
By definition, the size of an array cannot be negative. The appropriate variable for array sizes in C is size_t, use it.
Your for loop can start with the second element of the array, because you have already initialized maxValue with the first element.
A for loop has three parts:
for (initializer; should-continue; next-step)
A for loop is equivalent to:
initializer;
while (should-continue)
{
/* body of the for */
next-step;
}
So the correct code is:
for (i = 0; i < size; ++i)
Where are
MINandMAXdefined in C, if at all?
They aren't.
What is the best way to implement these, as generically and type safe as possible (compiler extensions/builtins for mainstream compilers preferred).
As functions. I wouldn't use macros like #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)), especially if you plan to deploy your code. Either write your own, use something like standard fmax or fmin, or fix the macro using GCC's typeof (you get typesafety bonus too) in a GCC statement expression:
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
Everyone says "oh I know about double evaluation, it's no problem" and a few months down the road, you'll be debugging the silliest problems for hours on end.
Note the use of __typeof__ instead of typeof:
If you are writing a header file that must work when included in ISO C programs, write
__typeof__instead oftypeof.
It's also provided in the GNU libc (Linux) and FreeBSD versions of sys/param.h, and has the definition provided by dreamlax.
On Debian:
$ uname -sr
Linux 2.6.11
$ cat /etc/debian_version
5.0.2
$ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
$ head -n 2 /usr/include/sys/param.h | grep GNU
This file is part of the GNU C Library.
On FreeBSD:
$ uname -sr
FreeBSD 5.5-STABLE
$ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
The source repositories are here:
- GNU C Library
- FreeBSD
"How I write the code to find the max value of the array elements without comparing the last element to something outside the memory block?"
You just need to set max before going into the loop to 0. Then you compare if array[i] is greater than max at each iteration. If it is, max gets assigned by array[i]. The loop condition i < len checks whether there are more elements or not in array:
int max = 0;
for (unsigned int i = 0; i < N; i++) {
if (array[i] > max) {
max = array[i];
}
}
Another thing is that you have first a fixed size array of 10 elements with
int array[10];
and then later ask the user to input the amount of elements:
scanf("%d", &N);
If the user inputs a number above 11 for N, you accessing memory beyond the bounds of the array in the loops.
In this case, a declaration of array as variable length array after the scanf() call would be more appropriate:
int N, i;
scanf("%d", &N);
int array[N];
Since there will never be more than 10 players: "There are never more than 10 players in the game" - You could also leave array of fixed size with 10 elements and analyze the input of N in a loop and ask the user to reinput a valid number between 0 and 10 if N isn't in this range. You use N for the iterations only but it doesn't influence the array size.
int array[10];
while (1) {
scanf("%d", &N);
if ( N >= 0 || N <= 10 ) {
break;
}
printf("Please reinput a valid number!\n");
}
Side notes:
Always check the return value of
scanf()for an input error. This happens frequently and to check is a very good practice and makes your program way more safe.VLAs are not portable and only possible to compiler compliant to C99 and above. To maintain portability and to keep safety use an dynamically allocated array instead.
Related:
malloced array VS. variable-length-array
Is there any overhead for using variable-length arrays?
Is it a good idea to use C99 VLA compared to malloc/free?
how I write the code to find the max value of the array elements without comparing the last element to something outside the memory block?
You can code the behind() something like:
void behind(int *arr, int n) {
int max_value = 0;
for (int i = 0; i < n; i++)
if (arr[i] > max_value)
max_value = arr[i];
printf("Max value: %d\n", max_value);
}
Rather than defining an array, you may declare the array as a pointer variable, that'd be quite helpful. You can then dynamically allocate the memory and then use it like an array thereafter, also note that you're trying to pass a pointer, so it's better take some benefit of pointer declaration.
The full approach to do that:
#include <stdio.h>
#include <stdlib.h> // for dynamic memory allocation
void behind(int *, int);
int main(void) {
int *array; // declaring as pointer rather than array
int N, i;
scanf("%d", &N);
if (N > 11) { // must be less than 10
printf("Must be lesser than or equal to 10!\n");
return -1;
}
// dynamically allocating the required memory
array = (int *)malloc(sizeof(int) * N);
for (i = 0; i < N; i++)
scanf("%d", &array[i]);
// here we go
behind(array, N);
for (i = 0; i < N; i++)
printf("%d\n", array[i]);
return 0;
}
void behind(int *arr, int n) {
int max_value = 0;
for (int i = 0; i < n; i++)
if (arr[i] > max_value)
max_value = arr[i];
printf("Max value: %d\n", max_value);
}
An example output is as follows:
5 // ------------ INPUT
12
45
23
1
05
Max value: 45 // --- OUTPUT
12
45
23
1
5
This should work fine.
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z){
int max;
max=x;
if(y>max){
max=y;
}
if(z>max){
max=z;
}
return max;
}
Try this one:
#include <stdio.h>
int largest(int x, int y, int z);
int main() {
int val1, val2, val3;
int maximum;
printf("enter value \n");
scanf("%d", &val1, &val2, &val3);
maximum = largest(val1, val2, val3);
printf("the largest integer is %d = \n", maximum);
return 0;
}
int largest(int x, int y, int z){
if (x >= y && x >= z)
return x;
if (y >= x && y >= z)
return y;
// otherwise
return z;
}
The problem is that you wanted the method to return the largest value, but simply didnt do that - the code is not compiling because the largest function is defined to "return" an int but there's no return statement anywhere in your function.
If you dont know what exactly "returning function" is then take a look at this tutorial: http://www.cplusplus.com/doc/tutorial/functions/
You set max = auction[0]; on every loop, eliminating your previous max value.
Initialize the max value only once within the loop, or initialize max outside the loop (after reading the first value).
You are finding the last number in the list that is bigger than auction[0]