Solutions to Introduction to C Presentation

From dftwiki3
Revision as of 15:53, 10 October 2013 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Min, max, sum= <source lang="C"> #include <stdio.h> #include <stdlib.h> #define N 10 // functions go here... ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 15:53, 10 October 2013 (EDT)


Min, max, sum

#include <stdio.h>
#include <stdlib.h>

#define N 10

// functions go here...                                                                                      

int main() {
  
  int A[N] = { 4, 3, 2, 1, 0, 6, 5, 9,8, 7 };
  int i, min, max, sum=0;

  // smallest
  min = A[0];
  for ( i=1; i<N; i++)
    if ( A[i] < min ) min = A[i];
  
  // largest
  max = A[0];
  for ( i=1; i<N; i++ )
    if ( A[i] > max ) max = A[i];

  // sum
  sum = 0;
  for ( i=0; i<N; i++ )
    sum += A[i];

  printf( "min = %d  max = %d  sum = %d\n\n",
	  min, max, sum );

  return 0;
}

Min, max, sum with functions

#include <stdio.h>
#include <stdlib.h>

#define N 10

// functions go here...                                                                                      
int min( int* A ) {
  int _min, i;
  _min = A[0];
  for ( i=1; i<N; i++)
    if ( A[i] < _min ) _min = A[i];
  return _min;
}

// largest
int max( int* A ) {
  int max, i;
  max = A[0];
  for ( i=1; i<N; i++ )
    if ( A[i] > max ) max = A[i];
  return max;
}


// sum
int sum( int* A ) {
  int sum = 0, i;
  for ( i=0; i<N; i++ )
    sum += A[i];
  return sum;
}



int main() {
  
  int A[N] = { 4, 3, 2, 1, 0, 6, 5, 9,8, 7 };

  printf( "min = %d  max = %d  sum = %d\n\n",
	  min(A), max(A), sum(A) );

  return 0;
}

Passing by Reference

#include <stdio.h>
#include <stdlib.h>

#define N 10

// functions go here...                                                                                      
void minn( int* A, int *min ) {
  int i;
  *min = A[0];
  for ( i=1; i<N; i++)
    if ( A[i] < *min ) *min = A[i];
}

// largest
void maxx( int* A, int *max ) {
  int i;
  *max = A[0];
  for ( i=1; i<N; i++ )
    if ( A[i] > *max ) *max = A[i];
}


// sum
void summ( int* A, int *sum ) {
  int i;

  *sum = 0;
  for ( i=0; i<N; i++ )
    *sum += A[i];
}



int main() {
  
  int A[N] = { 4, 3, 2, 1, 0, 6, 5, 9, 8, 7 };
  int min, max, sum;
  minn( A, &min );
  maxx( A, &max );
  summ( A, &sum );
   
  printf( "min = %d  max = %d  sum = %d\n\n",
	  min, max, sum );

  return 0;
}

Min, max, sum with Malloc

#include <stdio.h>
#include <stdlib.h>

#define N 10

// functions go here...                                                                                      
int min( int* A ) {
  int _min, i;
  _min = A[0];
  for ( i=1; i<N; i++)
    if ( A[i] < _min ) _min = A[i];
  return _min;
}

// largest
int max( int* A ) {
  int _max, i;
  _max = A[0];
  for ( i=1; i<N; i++ )
    if ( A[i] > _max ) _max = A[i];
  return _max;
}


// sum
int sum( int* A ) {
  int _sum = 0, i;
  for ( i=0; i<N; i++ )
    _sum += A[i];
  return _sum;
}



int main() {
  int *A = (int *) malloc( N * sizeof( int ) );

  A[0] = 4; 
  A[1] = 3;
  A[2] = 2;
  A[3] = 1;
  A[4] = 0;
  A[5] = 6;
  A[6] = 5;
  A[7] = 9;
  A[8] = 8;
  A[9] = 7;

  printf( "min = %d  max = %d  sum = %d\n\n",
	  min(A), max(A), sum(A) );

  free( A );
  return 0;
}