I'm supposed to ask the user to enter a number of marks, and to decide what each mark is.
Then my program is supposed to find the min and max in the array, so far this is what my code looks like:
#include <stdio.h>
void findMinAndMax(int arrayMarks[], int numOfMarks)
{
int i;
int currMax = arrayMarks[0];
int currMin = arrayMarks[0];
for (i = 1; i < numOfMarks; i++)
{
if (arrayMarks[i] > currMax)
{
currMax = arrayMarks[i];
}
}
for (i = 1; i < numOfMarks; i++)
{
if (arrayMarks[i] < currMin)
{
currMin = arrayMarks[i];
}
}
printf ("The largest value is: %d\n", currMax);
printf ("The smallest value is: %d\n", currMin);
}
int main()
{
int numOfMarks;
int marks[] = {};
int i;
printf ("How many marks do you want to enter?\n");
scanf ("%d", &numOfMarks);
for (i = 0; i < numOfMarks; i++)
{
printf ("Please enter a mark: ");
scanf ("%d", &marks[i]);
}
findMinAndMax(marks, numOfMarks);
return 0;
}
When I run it, it shows this:
How many marks do you want to enter?
12
Please enter a mark: 2
Please enter a mark: 3
The largest value is: 4
The smallest value is: 2
I was only asked to enter marks twice, when it was supposed to ask me 12 times.
What am I doing wrong?