1. Top-down design methodology
Find all integer numbers (n) between 1 and 1000 that are prime, and the next integer (n+1) has exactly 14 divisors (including 1 and the number itself). Design your program using "wishful thinking". Realize the required functions.
MINIMUM: Realize the function that returns a truth value whether the parameter is a prime or not.
Realize the function that returns the number of divisors of its parameter.
Complete the program.
Solution
#include <stdio.h> #include <stdbool.h> #include <math.h> bool is_prime(int n) { int t=2; while(n%t!=0 && t<=sqrt(n)) { ++t; } return n%t!=0; } int numdiv(int n) { int num=0, i; for (i=1; i<=n; ++i) if (n%i==0) ++num; return num; } int main() { int k; for(k=2;k<=1000;++k) if(is_prime(k) && numdiv(k+1)==14) printf("%d and %d\n", k, k+1); return 0; }
2. Maximum for average
MINIMUM: Write a C function that receives an array of double values and returns the average ((a1+a2+... +an)/n) of the values.
Create a function that receives an array of double values and returns the index of that element in the array, which has the greatest value.
Write a C program that reads exactly 30 real values from the user (standard input), calculates the average of the values, locates the maximum element and replaces that by the average value. At last print the elements of the array.
Solution
#include<math.h> #include<stdio.h> double aver(double *b, int n) { double sum=0; int j; for(j=0;j<n;++j) sum+=b[j]; return sum/n; } int maxi(double *b, int n) { int mi=0,j; for(j=1;j<n;++j) if(b[j]>b[mi]) mi=j; return mi; } int main() { double a[30],ave; int j, m; for(j=0;j<30;++j) scanf("%lf",&a[j]); ave=aver(a,30); m=maxi(a,30); a[m]=ave; for(j=0;j<30;++j) printf("%g\n",a[j]); return 0; }
3. K
The rooms in the main building (K) of BME are numbered in a special way (to help navigate in the maze): 82 means room 2 on the corridor segment 8 on the ground floor, 234 means room 4 on segment 3 on the second floor. (The segment is always a single digit.)
MINIMUM:Create a new type Lecture
to represent a lecture: name of the
subject (at most 30 characters) and room number (integer). Write a function
that receives a Lecture
and returns the corridor
segment of its room.
Write a function that receives the array of lectures in
building K and a name. Find the lecture with the given name and return a
pointer to the corresponding element of the array. Return NULL
if there
is no such lecture. (Use strcmp
to compare strings.)
Create another function that receives the array of lectures in building K and another, not yet initialized array, which can be indexed by the corridor segment. Count the usage of each corridor segment (not regarding the floor) and put the result into the second array.
Solution
typedef struct Lecture { char name[30+1]; int room; } Lecture; int segment(Lecture v) { return v.room / 10 % 10; } #include <string.h> Lecture *search(Lecture *lectures, int n, char *name) { int i; for (i = 0; i < n; ++i) if (strcmp(lectures[i].name, name) == 0) return &lectures[i]; return NULL; } void usage(Lecture *lectures, int n, int *segments) { int i; for (i = 0; i <= 9; ++i) segments[i] = 0; for (i = 0; i < n; ++i) segments[segment(lectures[i])] += 1; } /* to test, NOT needed in the test */ int main() { Lecture a[4]={{"prog",234},{"phys",147},{"cs",32},{"econ",86}}; int ct[10],i; usage(a,4,ct); for(i=0;i<10;++i) printf("segment %d: %d\n",i,ct[i]); return 0; }
4. Mute text
MINIMUM: Write a function that receives a single character and returns true for lowercase vowels ( a e i o u ). Use this function in the following task!
Write a function that receives two string parameters: source and destination. "Mute" the text received in the source parameter: remove all the vowels of the original text. Like "Hello world!" → "Hll wrld!". Put the result into the destination string.
Write a C program that demonstrates the use of the above function and prints the muted text.
Solution
int isvowel(char c) { return c=='a'||c=='e'||c=='i'||c=='o'||c=='u'; } void mute(char *d, char *s) { int from=0,to=0; while(s[from] != '\0') { d[to]=s[from]; if(!isvowel(d[to])) ++to; ++from; } d[to]='\0'; } #include <stdio.h> int main() { char a[]="Hello world!", b[50]; mute(b,a); printf("|%s|",b); return 0; }