0

I'm reading the book "Cracking the Coding Interview" which contains several examples of algorithms in C. I'd like to make programs which implement these algorithms and run them as I go along.

One such algorithm is "Min and Max 1" (from the "Big O" chapter):

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int x : array) {
  if (x < min) min = x;
  if (x > max) max = x;
}

I've attempted to 'write a program around this' as follows:

#include<stdio.h>

int array[5] = [1, 3, 2, 5, 4];

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

int main(void):
{
  for (int x : array) {
    if (x < min) min = x;
    if (x > max) max = x;
  }
  printf("The minimum is %i", min)
  printf("The maximum is %i", max)
}

However, if I try to compile and run this I get the error: expected identifier before numeric constant int array[5] = [1, 3, 2, 5, 4];. How would I correctly implement this algorithm for this example input array?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • 1
    it is `int array[] = {1, 3, 2, 5, 4};` (size is computed automatically from your data). And this `for (int x : array) {` is C++11 if I'm not mistaken. – Jean-François Fabre Aug 26 '16 at 15:33
  • 4
    Questions on how to write basic C syntax are not a good fix for SO - I recommend grabbing a good book: http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Oliver Charlesworth Aug 26 '16 at 15:36

4 Answers4

4

What you mean is the following

#include <stdio.h>
#include <limits.h>

#define N   5

int main( void ) 
{
    int array[N] = { 1, 3, 2, 5, 4 };

    int min = INT_MAX;
    int max = INT_MIN;

    for ( size_t i = 0; i < N; i++ )
    {
        if ( array[i] < min ) min = array[i];
        if ( max < array[i] ) max = array[i];
    }

    printf( "The minimum is %i\n", min );
    printf( "The maximum is %i\n", max );

    return 0;
}

The program output is

The minimum is 1
The maximum is 5

As for your program then it contains invalid constructions according to the C grammar.

In C++ the loop can look the same way as you showed.

#include <iostream>
#include <limits>

int main() 
{
    const size_t N = 5;
    int array[N] = { 1, 3, 2, 5, 4 };

    int min = std::numeric_limits<int>::max();
    int max = std::numeric_limits<int>::min();

    for ( int x : array )
    {
        if ( x < min ) min = x;
        if ( max < x ) max = x;
    }

    std::cout << "The minimum is " << min << std::endl;
    std::cout << "The maximum is " << max << std::endl; 

    return 0;
}

Take into account that there is no sense to declare the array like global.

As for the array definition in C then you can define it either like

int array[5] = { 1, 3, 2, 5, 4 };

(or using some named constant instead of the number 5)

or like

int array[] = { 1, 3, 2, 5, 4 };

In the last case the number of elements is equal to the number of the initializers. Or even you can use the following initialization

int array[] = { [0] = 1, [1] = 3, [2] = 2, [3] = 5, [4] = 4 };
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

A few issues:

  1. int array[5] = [1, 3, 2, 5, 4]; needs to be int array[] = {1, 3, 2, 5, 4};

  2. The short form for loop doesn't exist in C. (Are you using a C++11 compiler to compile C code? If so then saving your source file with a .c extension might be a simple way of putting it into C mode.) Use for (size_t i = 0; etc. instead and access the array elements by the index i.

  3. Using min and max as variable names is to be discouraged as they frequently appear as macro definitions.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Use C syntax to implement it in C.

  • Use {}, not [], to define the initial values of arrays.
  • Use INT_MIN and INT_MAX from limits.h to use minimum and maximum values of int.
  • Don't place a colon after int main(void).
  • for (int x : array) is not supported in C. Use one of the supported forms of loops.
  • Semicolons are required after each statements.

Here is an implementation in C99:

#include<stdio.h>
#include<limits.h>

int array[5] = {1, 3, 2, 5, 4};

int min = INT_MAX;
int max = INT_MIN;

int main(void)
{
  for (size_t i = 0; i < sizeof(array) / sizeof(*array); i++) {
    int x = array[i];
    if (x < min) min = x;
    if (x > max) max = x;
  }
  printf("The minimum is %i", min);
  printf("The maximum is %i", max);
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

Array is initialized inside {} not []

Looks like you copied it from wrong place or you are a java Programmer.

#include<stdio.h>

int a[5] = {1, 3, 2, 5, 4};

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;


int main(void):
{

  for (int x : array) {
    if (x < min) min = x;
    if (x > max) max = x;
  }
  printf("The minimum is %i", min)
  printf("The maximum is %i", max)
}

This will still give you error if you want i can fix it. I left it so that you can learn this by your own.

Ravi Thapa
  • 79
  • 1
  • 7