wiki:k2015/demot/demoC1english
Last modified 2 years ago Last modified on 2016-01-14 13:32:55

Demot » Demo C1, 27.4.2015

You can read the questions here, but do them in TIM-environment.

You have to do a minimum of 60% of all the tasks in demo C1 and C2. A minimum of 40% of tasks must be done in each demo (C1 and C2). Course is graded pass / fail. The maximum tasks for each demo is 10, so to pass this course, you have to do a minimum of 12 tasks. Return using Nettidemowww. In nettidemowww use 'c1' as Demokerran numero.

At least two of these tasks must be tested using automated tests (Comtest for example).

Task 1. Precompiler

How would precompiler compile the file below? The precompiled file can be saved using option -save-temps. For example, if the file is named hopo.c, the command

gcc -save-temps hopo.c

makes file hopo.i, which is the precompiled file. Try to do this task just by thinking, so not using computer to help you.

Are there any errors in the code below? What would this code print to user? (After the possible fixes made)

#define pois return 0;
#include <stdio.h>
#define HALPA=HINTA;
#define auto "Mosse"
#define Tul "Terve"
#define kaa  "tuloa"
#define TASTA int main(void){
#define TAHAN pois }
#define VAHAN
#define PP ;
#define VEROJA (1+40 prosenttia) PP
#define plus *
#define on =
#define HINTA=7;
#define lf "\n"
#define prosenttia / 100.0

TASTA
  int maksu on HALPA plus VAHAN VEROJA
  printf("auto on vanha, mutta halpa: %d!\n",maksu) PP
  printf(Tulkaa " kyytiin!" lf) PP
TAHAN

Task 2. Procedures and argument passing

Write a C-program (using functions in stdio.h, not iostream.h) that asks user the measurements of a room and then prints the area and the volume of the room. Use pointers in argument passing.

Task 3. C++ argument passing

Do the same task as in task 2 but use iostreams and reference variables. Example

Taks 4. Pointers

Fill the picture below so that all the variables and pointers are visible. Draw the picture again after every assignment. Remember to draw pointer p in every step.

int a,b,c;
int *p;
int e;
/* 1 */    a = 19;
/* 2 */    p = &b;
/* 3 */   *p = a+2;
/* 4 */    p = p+1;  /* Same as p=p+2 */
/* 5 */   *p = 7;
/* 6 */    p = p+2;  /* Same as p=p+4 */
/* 7 */   *p = p-3;  /*  p-6          */
/* 8 */  **p = 199;
     
    /* 1 */    /* 2 */  /* 3 */  /* 4 */  /* 5 */  /* 6 */  /* 7 */  /* 8 */  
    +-----+    +-----+  +-----+  +-----+  +-----+  +-----+  +-----+  +-----+  
100 |     | a  |     |  |     |  |     |  |     |  |     |  |     |  |     |   
    +-----+    +-----+  +-----+  +-----+  +-----+  +-----+  +-----+  +-----+   
102 | ??  | b  |     |  |     |  |     |  |     |  |     |  |     |  |     |   
    +-----+    +-----+  +-----+  +-----+  +-----+  +-----+  +-----+  +-----+   
104 | ??  | c  |     |  |     |  |     |  |     |  |     |  |     |  |     |   
    +-----+    +-----+  +-----+  +-----+  +-----+  +-----+  +-----+  +-----+  
106 | ??  | p  |     |  |     |  |     |  |     |  |     |  |     |  |     |  
    +-----+    +-----+  +-----+  +-----+  +-----+  +-----+  +-----+  +-----+   
108 | ??  | e  |     |  |     |  |     |  |     |  |     |  |     |  |     |   
    +-----+    +-----+  +-----+  +-----+  +-----+  +-----+  +-----+  +-----+   

When calculating with pointers p=p+1 means that p points to next object. This means that the the value of grows by 2.

Task 5. Testing

Fill in a blanket similar to this. What are the values of different variables after each line? What does the program print to user? (a=i++; means the same as a=i; i=i+1; and a=++i; means the same as i=i+1; a=i;)

#include <stdio.h>
/* 01 */
/* 02 */ int *d,c=4,u=9;
/* 03 */
/* 04 */ int huijaus(int a,int *b)
/* 05 */ {
/* 06 */   *(d+1) = ++(*b);
/* 07 */   c = *b - 2;
/* 08 */   *b += a/2;
/* 09 */   return a > 3;
/* 10 */ }
/* 11 */
/* 12 */ int puijaus(int **b)
/* 13 */ {
/* 14 */   int n;
/* 15 */   *b = &u;
/* 16 */   *d =175;
/* 17 */   n = --u;
/* 18 */   return 3 * (*b == d) + n;
/* 19 */ }
/* 20 */
/* 21 */ int main(void)
/* 22 */ {
/* 23 */   int k1 = 31, k2 = 45; d = &c-1;
/* 24 */   k1 = huijaus(k2+*(d+1),&c);
/* 25 */   k2 = puijaus(&d);
/* 26 */   printf("%4d ",*d);
/* 27 */   printf("%4d %4d %4d %4d\n",u,c,k2,k1);
/* 28 */   return 0;
/* 29 */ }

Taks 6. Functions

Write and test a C-function ask_year that asks user a year and then returns the value in parameters. The returned value in return-statement is 1, if the year is not from this century of if there is something wrong in the input. Otherwise the return value is 0.

Task 7. The index of the smallest number

Write a procedure index_of_smallest which returns the index of the smallest number in an integer array. For example for this array 13.1.4 the result is 1.

i = index_of_smallest(k_pituudet,12); /* => i=1 */

Using that procedure, make another procedure smallest that returns the value of the smallest number in an integer array.

n = smallest(k_pituudet,12);         /* => n = 28 */

Task 8. Palindrome

Write a function is_palindrome. Make two different versions. One for C-strings and another for C++ -strings (string -class).

Task 9. Delete from end

Write two different procedures (one for strings and one for char * -strings) named delete_from_end. These procedures both delete the last n characters. Remember all the possible errors.

char s[]="Kissa istuu";
delete_from_end(s,3);               /* => s = "Kissa is" */
string st("Kissa istuu");
delete_from_end(st,3);              // => st= "Kissa is" 

Task 10. Deleting from an array

Make a procedure

    int delete_from_array(int array[],int count,int n)

that deletes all the occurrences of number n in the array.

   int t[]={4,7,6,3,6,2};
   int count=6;

   count = delete_from_array(t,count,6);  /* => t = {4,7,3,2}, count = 4  */