Programming contests

50 Programming Exercise for Beginners

January 2, 2019 12:00 AM – December 31, 2019 12:00 AM

It's Logical

Write a function that takes the address of a two-dimensional array, the number of rows and columns, and a string as parameters! The function should create a new one-dimensional array of integers with as many elements as the number of columns in the original array. The function should fill the new array with Boolean values. An element should be true if the string taken as the fourth parameter occurs in the corresponding column of the original two-dimensional array. The function should return the address of the new array.

The Specification of the Function

int *logical( char *[], int, int, char * );

Note

Place the function in a file named logical.c and submit this file as a solution to the evaluation system. You can test your solution using the following files. The evaluation system does not necessarily perform the evaluation using these files.

myheader.h

  1. #ifndef _MYHEADER_H
  2. #define _MYHEADER_H 1
  3. int *logical( char *[], int, int, char * );
  4. #endif /* myheader.h */
download as text file

main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "myheader.h"
  5. #define N 3
  6. #define M 4
  7.  
  8. int main()
  9. {
  10.   char *t[ N ][ M ] =
  11.     { { "szaxofon", "szaxofon",    "szaxofon", "szaxofon"    },
  12.       { "szaxofon", "kakukktojas", "szaxofon", "szaxofon"    },
  13.       { "szaxofon", "szaxofon",    "szaxofon", "kakukktojas" } };
  14.   char str[ 100 ];
  15.   int i, j;
  16.   int *p;
  17.   strcpy( str, "kakukktojas" );
  18.   printf( "%d %d *%s*\n\n", N, M, str );
  19.   for ( i = 0; i < N; ++i )
  20.   {
  21.     for ( j = 0; j < M; ++j )
  22.     {
  23.       if ( j > 0 )
  24.         putchar( ' ' );
  25.       printf( "*%s*", t[ i ][ j ] );
  26.     }
  27.     putchar( '\n' );
  28.   }
  29.   putchar( '\n' );
  30.   p = logical( &t[ 0 ][ 0 ], N, M, str );
  31.   for ( i = 0; i < N; ++i )
  32.   {
  33.     for ( j = 0; j < M; ++j )
  34.     {
  35.       if ( j > 0 )
  36.         putchar( ' ' );
  37.       printf( "*%s*", t[ i ][ j ] );
  38.     }
  39.     putchar( '\n' );
  40.   }
  41.   putchar( '\n' );
  42.   for ( j = 0; j < M; ++j )
  43.   {
  44.     if ( j > 0 )
  45.       putchar( ' ' );
  46.     printf( "%d", p[ j ] );
  47.   }
  48.   putchar( '\n' );
  49.   free( p );
  50.   return EXIT_SUCCESS;
  51. }
download as text file

Makefile

  1. CC = gcc
  2. OBJS = main.o logical.o
  3. TARGETS = main
  4. all: $(TARGETS)
  5. main: main.o logical.o
  6. $(CC) main.o logical.o -o main
  7. main.o: main.c
  8. $(CC) -c main.c
  9. logical.o: logical.c
  10. $(CC) -c logical.c
  11. clean:
  12. rm -rf $(OBJS) *~ $(TARGETS)
download as text file

Output of the Sample Test Program

  1. 3 4 *kakukktojas*
  2. *szaxofon* *szaxofon* *szaxofon* *szaxofon*
  3. *szaxofon* *kakukktojas* *szaxofon* *szaxofon*
  4. *szaxofon* *szaxofon* *szaxofon* *kakukktojas*
  5. *szaxofon* *szaxofon* *szaxofon* *szaxofon*
  6. *szaxofon* *kakukktojas* *szaxofon* *szaxofon*
  7. *szaxofon* *szaxofon* *szaxofon* *kakukktojas*
  8. 0 1 0 1
download as text file

One possible solution is available in: main.c.

Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 03/01/2019