Professional Documents
Culture Documents
Ebook PMC
Ebook PMC
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
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.
√
Part 1. Write the following condition into C statements. You do not need to write full programs,
just a line of C statements
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 :
#include <stdio.h>
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 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;
n n/2 if n is even
n 3n + 1 if n is odd
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)
scanf(“%d”, &start);
Assume that you have a file named “input.in” with the following data in it :
31
10
#include <stdio.h>
#include <stdlib.h>
Solusi No.1
a. x= √
Catatan:
b. v= ( )
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(√ (
))
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))));
Catatan:
! -> NOT
|| ->
OR
Catatan:
&& -> AND
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");
Solusi No.3
#include <stdio.h>
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);
}
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)
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;
}
#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;
}
Nomer 4
Nomer 4