TUGAS MATA KULIAH
PEMROGRAMAN DAN STRUKTUR DATA
Dosen Pembimbing : I Ketut Resika Arthana, S.T,.M.Kom.
Oleh
Nama : Ni Komang Winda Damayanti
Nim : 1815091003
Kelas : Sistem Informasi B
PROGRAM STUDI SISTEM INFORMASI
FAKULTAS TEKNIK DAN KEJURUAN
UNIVERSITAS PENDIDIKAN GANESHA
TAHUN 2019
1. Change the program at the bottom so that it prints to the output "Hello, World!".
#include <stdio.h>
int main ()
{
printf("\"Hello, Word!\"");
return 0;
}
2. In the next exercise, you will need to create a program which prints out the sum of the
numbers a, b, and c.
#include <stdio.h>
int main() {
int a = 3;
float b = 4.5;
double c = 5.25;
float sum;
/* Your code goes here */
sum=a+b+c;
printf("The sum of a=%d, b=%.1f, and c=%.2f is %.2f.",a,b,c,sum);
return 0;
}
3.
1. The code below does not compile, because the grades variable is missing.
2. One of the grades is missing. Can you define it so the grade average will be 85?
#include <stdio.h>
int main() {
/* TODO: define the grades variable here */
int average;
int grades[3] ={80,85,90};
average = (grades[0] + grades[1] + grades[2]) / 3;
printf("The average of the 3 grades is: %d", average);
return 0;
}
4. Let us try to find out the average marks of a group of five students for two subjects,
Mathematics and Physics. To do this, we use a two-dimensional array called grades. The
marks corresponding to Mathematics would be stored in the first row (grades[0]),
whereas those corresponding to Physics would be stored in the second row (grades[1]).
Complete the following steps so that you can execute this program.
1. Declare grades as a two-dimensional array of integers
2. Complete the for loops by specifying their terminating conditions
3. Compute the average marks obtained in each subject
#include <stdio.h>
int main() {
/* TODO: declare the 2D array grades here */
float average;
int grades;
int i;
int j;
grades[0][0] = 80;
grades[0][1] = 70;
grades[0][2] = 65;
grades[0][3] = 89;
grades[0][4] = 90;
grades[1][0] = 85;
grades[1][1] = 80;
grades[1][2] = 80;
grades[1][3] = 82;
grades[1][4] = 87;
/* TODO: complete the for loop with appropriate terminating conditions */
for (i = 0; i<j ; i++) {
average = 0;
for (j = 0; j<i ; j++) {
average += grades[i][j];
}
/* TODO: compute the average marks for subject i */
printf("The average marks obtained in subject %d is: %.2f\n", i,
average);
}
return 0;
}
5. In this exercise, you must construct an if statement inside the guessNumber function
statement that checks if the number guess is equal to 555. If that is the case, the
function must print out using printf "Correct. You guessed it!". If guess is less than
555, the function must print out using printf "Your guess is too low." If guess is
greater than 555, the function must print out using printf "Your guess is too high."
#include <stdio.h>
int main() {
guessNumber(500);
guessNumber(600);
guessNumber(555);
}
void guessNumber(int guess) {
printf("Input guess=");
scanf("%d",&guess);
if (guess == 555) {
printf(" Correct You guessed it!\n");
} else if (guess < 555) {
printf("Your guess is too low.\n");
} else {
printf("Your guess is too high\n");
}
}
6. Strings
#include <stdio.h>
#include <string.h>
int main() {
char name[100];
char first_name[] = "John";
char last_name[] = "Boe";
last_name[0] = 'B';
sprintf(name, "%s %s", first_name, last_name);
if (strncmp(name, "John Boe", 100) == 0) {
printf("Done!\n");
}
name[0]='\0';
strncat(name,first_name,4);
strncat(name,last_name,20);
printf("%s\n",name);
return 0;
}
7. For Loops
#include <stdio.h>
int main() {
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int factorial = 1;
int i;
for(i=0;i<10;i++){
factorial = factorial * array[i];
}
printf("10! is %d.\n", factorial);
}
8. While Loops
#include <stdio.h>
int main() {
int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4};
int i = 0;
while (i < 10) {
if(array[i] < 5){
i++;
continue;
}
if(array[i] > 10){
break;
}
printf("%d\n", array[i]);
i++;
}
return 0;
}
9. Functions
#include <stdio.h>
/* function declaration */
void print_big(int number);
int main() {
int array[] = { 1, 11, 2, 22, 3, 33 };
int i;
for (i = 0; i < 6; i++) {
print_big(array[i]);
}
return 0;
}
void print_big(int number){
if(number > 10){
printf("%d is big\n",number);
}
}
10. Static
#include <stdio.h>
int sum (int num) {
static int total = 0;
total = total + num;
return total;
}
int main() {
printf("%d ",sum(55));
printf("%d ",sum(45));
printf("%d ",sum(50));
return 0;
}