Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

PEMECAHAN

MASALAH DENGAN C
EL 2008

32
BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2014/2015
Kementerian Kesejahteraan Akademik – Kementerian PSDF BP HME ITB 2015/2016
UTS TAHUN AJARAN 2014/2015
Soal

Problem 1 : Using Mathematical Library (20 points)

Convert the following mathematical statements into C language statements. You do not need
to write full programs, just a line of C statements. Assume that you already include <math.h>
library and also the variables have declared before.


a.
b.

c. H = √ ( )
d.

Problem 2 : Conditional Statements (20 points)

Part 1. Write the following condition into C statements. You do not need to write full programs,
just a line of C statements

A. a is not greater than c*d/2 or is less than b+c


B. a is greater than b and b is greater than c and c is ewual to zero
C. x is less than y and y must be between -5 and 5

Part 2. Given a point in the cartesian coordinate (x,y), build an if-else structure that will prints a
message telling wether the point lies on an axis or the quadrant in which it is found. Not a full
program, just if-else part.

Problem 3 :

Trace the following program, write down the output of it

#include <stdio.h>

int fun(int a, int b, int *c, int *d);

BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2015/2016


82 Kementerian Kesejahteraan Anggota – Kementerian Kewirausahaan BP HME ITB 2016/2017
int main (void)

int a = 1;

int b = 9;
int c = 4;

int d = 5;
a = fun(a,b,&c,&d);

printf(“%d\t%d\t%d\t%d\n”,a,b,d,c);
}

int fun(int a, int b, int *c, int *d)

int i,j=b;

for (i=a;i!=b;i+=2)

if (*d > 2)

*c += *d-2;

else

*c -= *d-2;

j *= 2;

*d*=3;

return i+j;

PEMECAHAN MASALAH DENGAN C


83
Problem 4 : Loops (20 points)

Colatz sequence, also known as 3n + 1 sequence, is a mathematical sequence defined for


positive integer as :

n  n/2 if n is even

n  3n + 1 if n is odd

For example, let’s use 13 as starting number

13  40  20  10  5  16  8  4  2  1

Is is conjectured (half-proven) that for any starting number, the sequence will end at 1. It can
be seen that the sequence has length10 with 13 as starting number.

Your task is to complete the following program to calculate the length of the s equence, when
the user input starting number.

#include <stdio.h>

int main(void)

int start, length;

scanf(“%d”, &start);

Problem 5 : File Reading/Writing (20 points)

Assume that you have a file named “input.in” with the following data in it :

31

10

BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2015/2016


84 Kementerian Kesejahteraan Anggota – Kementerian Kewirausahaan BP HME ITB 2016/2017
Write a program that count the number of even values and sums all the even values in the file.
For example, the sum for this file is :

Sum = 2+10+4+6 = 22 and there are 4 even values

Print a message to the screen :

Sum of 4 even value is 22

Write your code here :

#include <stdio.h>

#include <stdlib.h>

int main (void)

PEMECAHAN MASALAH DENGAN C


85
UTS TAHUN AJARAN 2014/2015
Solusi

Solusi No.1

a. x= √

x = (-b + sqrt(pow(b,2) - 4*a*c)) / (2.0 * a);

Catatan:

 pow(float basis, float pangkat) ->


 sqrt(float bilangan) -> √

b. v= ( )

v = pow(e,(-1)*a*t) * (A*cos(w*t) + B*sin(w*t));

Catatan:
 cos(double radian) -> Memberikan nilai balik dalam radian
 sin(double radian) -> Memberikan nilai balik dalam radian
 e = 2,71818 -> Harus didefinisi sendiri

c. H = -20log(√ (
))

H = (-20) * log10(sqrt(1.0 + pow((w/w0), 2)));

Catatan:
 log(double argumen) -> log dengan basis e
 log10(double argumen) -> log dengan basis
10
( )
d. f= √

f = 1.0 / (R*C*log(1.0/(1-sqrt(n))));

BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2015/2016


86 Kementerian Kesejahteraan Anggota – Kementerian Kewirausahaan BP HME ITB 2016/2017
Solusi No.2
Part 1

a. a is not greater than c*d/2 or a is less than b+c


!(a > (c*d/2)) || (a < (b + c));

Catatan:
 ! -> NOT
 || ->
OR

b. a is greater than b and b is greater than c and c is equal to zero


(a > b) && (b > c) && (c == 0);

Catatan:
 && -> AND

c. x is less than y and y must be between -5 and 5


(x < y) && (y > -5) && (y < 5);

Catatan:
 “between” -> Tidak meng-include end point/number

Part 2
Given a point in the Cartesian coordinate (x,y), build an if-else structure that will prints a message
telling whether the point lies on an axis or the quadrant in which it is found. Not a full program, just
if-else part.
if (x > 0 && y > 0) {
printf("Kuadran 1");
} else if (x < 0 && y > 0) {
printf("Kuadran 2");
} else if (x < 0 && y < 0) {
printf("Kuadran 3");
} else if (x > 0 && y < 0) {
printf("Kuadran 4");

PEMECAHAN MASALAH DENGAN C


87
} else {
printf("Axis");
}

Solusi No.3

#include <stdio.h>

int fun(int a, int b, int *c, int *d);

int main(void)
{
int a = 1;
int b = 9;
int c = 4;
int d = 5;
a = fun(a, b, &c, &d);
printf("%d\t%d\t%d\t%d\n",a,b,d,c);
}

int fun(int a, int b, int *c, int *d)


{
int i, j=b;
for (i = a; i!= b; i+= 2) {
if (*d > 2)
*c += *d -2;
else
*c -= *d - 2;
j *= 2; /* BARIS INI DILUAR ELSE */
}
*d*=3; /* NILAI *d dikali 3 */
return i+j;
} looping
i j *c *d
ke
0 1 9 4 5
Inisial
1 1 18 7 5
(Kode dalam loop
belum dieksekusi) 2 3 36 10 5
3 5 72 13 5
4 7 144 16 5
5 9 144 16 5

BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2015/2016


88 Kementerian Kesejahteraan Anggota – Kementerian Kewirausahaan BP HME ITB 2016/2017
Saat iterasi ke-5 nilai i = 9 maka proses looping akan selesai (perintah dalam loop tidak dieksekusi).
Perhatikan bahwa kode j *= 2; berada pada luar else sehingga nilai j selalu dikali 2 pada setiap loop.

Pada program utama perhatikan bahwa nilai a = fun(a,b,&c,&d) yang memberikan nilai balik berupa i
+ j. Oleh karena itu nilai a = 144 + 9 = 153.

Karena fungsi fun melewatkan alamat pada parameter ke 3 dan 4 maka variable yang dilewatkan ke
fungsi fun pada parameter ke 3 dan 4 akan mengalami perubahan nilai setelah fungsi ini di-eksekusi.
Oleh karena itu nilai c = 16 dan d = 5 (c  4 dan d  5)

Output program: (Ingat variable d ditampilkan terlebih dahulu sebelum


c) 153 9 15 16

Solusi No.4

#include <stdio.h>

int main(void)
{
int start, length;
scanf("%d", &start); /*Asumsi input start selalu positif*/

/* Inisialisasi */
length = 1;

/* Algoritma */
while (start > 1)
{
if (start % 2 == 0) { /* Start Genap */
start /= 2;
} else { /* Start Ganjil */
start = (start * 3) + 1;
}

length += 1;
}

/* Output */
printf("length = %d\n", length);
return 0;
}

PEMECAHAN MASALAH DENGAN C


89
Solusi No.5

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

int main(void)
{
/* Deklarasi variable */
FILE *baca;
int x;
int sum = 0; /* Jumlah bilangan genap */
int count = 0; /* Menghitung banyak genap */

/* Buka file */
baca = fopen("input.in", "r"); /* r -> read */

/* Algortima */
while (!feof(baca))
{
fscanf(baca, "%d", &x);
if (x % 2 == 0)
{
count += 1;
sum += x;
}
}

/* Output */
printf("Sum of %d even values is %d\n", count, sum);
return 0;
}

BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2015/2016


90 Kementerian Kesejahteraan Anggota – Kementerian Kewirausahaan BP HME ITB 2016/2017
UTS TAHUN AJARAN 2013/2014
Soal
Nomer 1

PEMECAHAN MASALAH DENGAN C


91
Nomer 2

BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2015/2016


92 Kementerian Kesejahteraan Anggota – Kementerian Kewirausahaan BP HME ITB 2016/2017
Nomer 3

[notes: Tulis program utama (int main (void))]

Nomer 4

PEMECAHAN MASALAH DENGAN C


93
Nomer 5

BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2015/2016


94 Kementerian Kesejahteraan Anggota – Kementerian Kewirausahaan BP HME ITB 2016/2017
UAS TAHUN AJARAN 2013/2014
Soal
Nomer 1

PEMECAHAN MASALAH DENGAN C


95
Nomer 2

BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2015/2016


96 Kementerian Kesejahteraan Anggota – Kementerian Kewirausahaan BP HME ITB 2016/2017
Nomer 3

Nomer 4

PEMECAHAN MASALAH DENGAN C


97
Nomer 5

BUNDEL SOAL ELEKTROTEKNIK | SEMESTER 4 2015/2016


98 Kementerian Kesejahteraan Anggota – Kementerian Kewirausahaan BP HME ITB 2016/2017
Nomer 6

PEMECAHAN MASALAH DENGAN C


99

You might also like