Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Report Tutorial 0 Interaction with the user Student Foo, Student Bar We have written a main program in order to demonstrate the application of our functions, and the two files « interaction.c » and « interaction.h » for the first part as well as « automatique.c » and « automatique.h » for the second part. #include <stdio.h> #include "interaction.h" #include "automatic.h" #define N 10 int main() { /* variable declarations */ int int_variable; double dbl_array[N]; double *dbl_array2; /* part 1: interaction with the user */ printf("\n----- example - part 1 -----\n"); /* exercise 1: integer variable */ int_variable = int_prompt_user(); int_variable = int_variable + 1; int_show(int_variable); printf("\n---\n"); /* exercise 2: array of floating point numbers */ dbl_arr_prompt_user(dbl_array, N); dbl_arr_mult_dbl(dbl_array, N, 2.0); dbl_arr_show(dbl_array, N); /* part 2: without any interaction */ printf("\n----- example - part 2 -----\n"); dbl_array2 = dbl_arr_sin(dbl_array, N); dbl_arr_save_to_file(dbl_array2, "e:\\prog_c\\array_sin.dat", N); /* we have finished, bye */ printf("\n"); return 0; } Student Foo, Student Bar Tutorial 0 page 2 / 6 Exercise 1 This is the header file « interaction.h » : int int_prompt_user(); void int_show(int); void dbl_arr_prompt_user(double[], int); void dbl_arr_mult_dbl(double[], int, double); void dbl_arr_show(double[], int); And this is the file « interaction.c » : #include <stdio.h> #include <stdlib.h> /* ******************************************************************* int_prompt_user prompts the user to enter the value of an integer variable on the keyboard. INPUT: none OUTPUT: - answer: value entered by the user Felix Kroeger, Pierre Lugan ******************************************************************* */ int int_prompt_user() { int answer; printf("\nPlease enter an integer number: "); scanf("%d", &answer); return(answer); } /* ******************************************************************* int_show displays the current value of an integer variable. INPUT: - value: integer variable to display OUTPUT: none Felix Kroeger, Pierre Lugan ******************************************************************* */ void int_show(int value) { printf("\nThe variable of type integer has the value: %d\n", value); } Student Foo, Student Bar Tutorial 0 page 3 / 6 /* ******************************************************************* dbl_arr_prompt_user prompts the user to enter all the elements of an array of real numbers of type double on the keyboard which is passed to the function as a parameter. The values are changed in the computer memory. INPUT: - dim: size of the array, i.e. the number of elements OUTPUT: - array: array containing the values entered by the user Felix Kroeger, Pierre Lugan ******************************************************************* */ void dbl_arr_prompt_user(double array[], int dim) { int i; printf("\nPlease enter the %d elements of the double array:\n", size); for(i=0; i<dim; i++) { printf("< element %d: ", i); scanf("%lf", array+i); } } /* ******************************************************************* dbl_arr_mult_dbl multiplies an array of real numbers of type double by a single real number of type double and returns the resulting array. The values are changed in the computer memory. INPUT: - array: array to multiply with the real number - dim: size of the array, i.e. the number of elements - factor: real number, i.e. scalar factor OUTPUT: - array: array after the scalar multiplication Felix Kroeger, Pierre Lugan ******************************************************************* */ void dbl_arr_mult_dbl(double array[], int dim, double factor) { int k; for(k=0; k<dim; k++) { array[k] = array[k] * factor; } } Student Foo, Student Bar Tutorial 0 /* ******************************************************************* dbl_arr_show displays all the elements of an array of real numbers of type double on the screen. INPUT: - array: array of double values to display - dim: size of the array, i.e. number of elements OUTPUT: none Felix Kroeger, Pierre Lugan ******************************************************************* */ void dbl_arr_show(double array[], int dim) { int j; printf("\nThe array of double values looks like this:\n"); for(j=0; j<dim; j++) { printf("> %.4lf\n", array[j]); } } Result: page 4 / 6 Student Foo, Student Bar Tutorial 0 Exercise 2 This is the header file « automatic.h »: double *dbl_arr_sin(double[], int); void dbl_arr_save_to_file(double[], char*, int); And this is the file « automatic.c »: #include <math.h> /* ******************************************************************* dbl_arr_sin calculates the sine of all elements of an array of real numbers of type double and returns the resulting array. The original array is NOT changed in the computer memory. INPUT: - array: - dim: array containing the original real number size of the array, i.e. the number of elements OUTPUT: - r_array: resulting array, i.e. the sine of all elements Felix Kroeger, Pierre Lugan ******************************************************************* */ double *dbl_arr_sin(double array[], int dim) { int k; double *r_array; r_array = malloc(dim * sizeof(double)); for(k=0; k<dim; k++) { r_array[k] = sin(array[k]); } return(r_array); } /* ******************************************************************* dbl_arr_save_to_file saves an array of real numbers of type double to a file on the hard disk. INPUT: - array: array containing the original real number - dim: size of the array, i.e. the number of elements - filename: name and path of the output file OUTPUT: none Felix Kroeger, Pierre Lugan ******************************************************************* */ page 5 / 6 Student Foo, Student Bar Tutorial 0 page 6 / 6 void dbl_arr_save_to_file(double array[], int dim, char *filename) { /* TODO: this remains for you to write !!! */ } Results: In this case the program does not display anything on the screen, but it creates a file that has to be treated with Excel, i.e. you have to show the results in a table as well as create a graph. Index 0 1 2 3 4 5 6 7 8 9 Sin(array[index]) 1 0,84147098 0,91294525 0,14112001 0,8 0,6 0,7568025 0,26237485 0,4 0,2794155 0,2 0,54397052 0,00799991 0 0 1 2 3 4 5 6 7 8 9 10 0,33945745 -0,01229969 -0,2 Remarks A good report is above all well readable, i.e. one easily understands what you do. Some small comments help to give a structure to your source files or, if necessary, explain the more complicated parts of your programs. It is however useless to explain for example that « int i; » is a declaration of an integer variable. If the results of your programs are displayed by means of « printf() » you should present in your report a screenshot, as in exercise 1. In order not to waste too much printer toner, please invert the images with the small program « Paint » (one of the accessories of Windows) as in the example above. If however the data have been written to a file on the hard disk you should present them in an appropriate way, e.g. the results of a treatment with Excel, as in the exercise 2. In the source files you should not use the accents or special characters: for the names of functions or variables as well as for the filenames it strictly forbidden, in the comments it is possible, in principle, but not recommended, because there are often problems when transfering a file from one computer to another one.