Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Passing Arguments | Functions
C Basics

book
Passing Arguments

Finding the Maximum Element in an Array

c

main

copy
#include <stdio.h>

int findMaxElement(int arr[]) // 1
{
int max = arr[0]; // 2

for (int i = 1; i < 5; i++) // 3
{
if (arr[i] > max) // 3
{
max = arr[i]; // 4
}
}

return max;
}

int main()
{
int array[] = { 10, 5, 7, 14, 3 };

int maxElement = findMaxElement(array);

printf("Max element is: %d\n", maxElement);

return 0;
}
123456789101112131415161718192021222324252627
#include <stdio.h> int findMaxElement(int arr[]) // 1 { int max = arr[0]; // 2 for (int i = 1; i < 5; i++) // 3 { if (arr[i] > max) // 3 { max = arr[i]; // 4 } } return max; } int main() { int array[] = { 10, 5, 7, 14, 3 }; int maxElement = findMaxElement(array); printf("Max element is: %d\n", maxElement); return 0; }
Tâche

Swipe to start coding

Craft a function that determines the smallest element of an array.

Solution

#include <stdio.h>

int findMinElement(int arr[])
{
int min = arr[0];

for (int i = 1; i < 5; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
}

return min;
}

int main()
{
int array[] = { 5, 1, 3, -6, 100 };

int minElement = findMinElement(array);

printf("Min element is: %d\n", minElement);

return 0;
}

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 4
#include <stdio.h>

int findMinElement(int arr[])
{
int min = arr[___];

for (int i = 1; i < 5; i++)
{
if (arr[i] ___ min)
{
min = arr[i];
}
}

return min;
}

int main()
{
int array[] = { 5, 1, 3, -6, 100 };

int minElement = ___(___);

printf("Min element is: %d\n", minElement);

return 0;
}

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

We use cookies to make your experience better!
some-alt