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?

int marks[] = {};

This should have been an error. Your leaving the compiler to decide the size of array but your not initializing it.The size of array is 0.
Solutions:
1.

int marks[50]; //Static Initialization

2.

int *marks; 
marks=(int*) malloc( noOfMarks );//Dynamic Initialization. Do ckeck if it returns NULL.

3.

vector<int> marks; //Use a vector

Malloc() is part of stdlib.h, which you did not include. You would have received an error, except you cast the return from malloc(), so it didn't give you one.

In addition to the above advice, you need to include stdlib.h in your header list, and in C, there is little cause to cast the result from malloc().

in C, there is little cause to cast the result from malloc().

Sorry, but I beg to differ. malloc() returns void* which is can point to any datatype (which you already know). Though C would implicitly typecast it, but on some major c++ compilers the same code would produce errors. In fact, void* should rarely be used in C++ when you have other polymorphism features like function overloading. Hence we can explicitly type-cast it, just to be on a safer side. :)

Malloc() is part of stdlib.h, which you did not include

Point taken.

This is the C forum. If you want to get help with C++, you need to go to the C++ forum.

Casting the return from malloc, in C, will cause C compilers to NOT show the error you would otherwise get from using malloc, without including stdlib.h.

Otherwise, there is no harm in casting the return from malloc. Your code warnings here, have been restrained by that cast.

This is the C forum. If you want to get help with C++, you need to go to the C++ forum.

I think you missed that I'm not the OP. :)

Save your processing time.

for (i = 1; i < numOfMarks; i++)
  {
    int v = arrayMarks[i];   // Save to a tmp. variable so save index math time

    if (v > currMax)
    {
      currMax = v;
    }
    else if (v < currMin)
    {
      currMin = v;
    }
  }

It looks like the only thing you need to do is give the array marks a size and you should be good to go.

You do not need vectors. You do not need malloc().

I second WaltP...:)

Thanks everyone, got it working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.