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.
C code to find max and min values: unexpected results
C programming problem to find largest number
[C Programming: Finding Maximum Element in an Array Using Functions] What's wrong with my code?
c - How can I write a program to find maximum value of integer variable - Stack Overflow
Videos
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)
Hi everyone,
I'm trying to find the maximum and minimum values in a C array, but I'm running into a problem. My code calculates the maximum value correctly, but the minimum value is always a very large negative number, even when all the values in the array are positive.
I've tried initializing the min variable to a large positive number, but it doesn't seem to help.
Here's my code:
#include <stdio.h>
int main(void)
{
int i, sum = 0;
int numbers [5];
int min, max, average;
printf("enter 5 numbers:\n");
for (i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
sum += numbers[i];
}
max = numbers[i];
min = numbers[i];
for (i = 0; i < 5 ; i++)
{
if (numbers[i] > max)
{
max = numbers[i];
}
if (numbers[i] < min)
{
min = numbers[i];
}
}
average = (double)sum/5;
printf("Average is %d and sum is %d\n", average, sum);
printf("Max number is %d and the min number is %d\n", max, min);
}Can anyone help me figure out what's going wrong?
Thanks!
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;
}As ı know the maximum value of a integer variable is 2147483647
That's a wrong assumption. The maximum value of an int can vary across systems. For example, on a 16-bit machine maximum value of int is not 2147483647.
You don't need to find it yourself. There are pre-defined macros which you can use from <limits.h>. For example, INT_MAX represents the maximum value an int can hold.
You cannot find this value programatically by means of computation. There is no way to detect that you're "at the end", and incrementing the largest integer has undefined behaviour.
That's why the largest and smallest values of integral types are made available to you by the standard library in the <limits.h> header. It is your responsibility to ensure that the results of operations fit into the desired types.