Difference between revisions of "Solutions to Introduction to C Presentation"

From dftwiki3
Jump to: navigation, search
(Min, max, sum with Malloc)
 
Line 1: Line 1:
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 15:53, 10 October 2013 (EDT)
+
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:49, 2 March 2017 (EST)
 
----
 
----
 
=Min, max, sum=
 
=Min, max, sum=

Latest revision as of 10:49, 2 March 2017

--D. Thiebaut (talk) 10:49, 2 March 2017 (EST)


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;
}

With A Data File

Data File

  • data.txt
10
4
3
2
1
0
6
5
9
8
7

C Program

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

//--- make N global and accessible by all functions ---
int N;

// min
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, i, n;
  FILE *fp;

  fp = fopen( "data.txt", "r" );
  fscanf( fp, "%d", &N );
   
  A = (int *) malloc( N * sizeof( int ) );

  i = 0;
  while ( !feof( fp ) && i < N ) {
    fscanf( fp, "%d", &n );
    if (feof( fp ) )
      break;
    A[i] = n;
    i++;
  }
  fclose( fp );

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

  free( A );
  return 0;
}