Download docx - Dti2143 lab sheet 8

Transcript
Page 1: Dti2143 lab sheet 8

UNIVERSITI TUN HUSSEIN ONN MALAYSIAFACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING DTI 2143: COMPUTER PROGRAMMING

LAB EXERCISE 8 ARRAY

Exercise 1.0: Compile and run the program below

#include<stdio.h>#include<conio.h>

int main(){int i, mark[10], j=0;

printf("Insert 10 Marks\n");

for(i=0;i<10;i++){ scanf("%d",&mark[i]); if(mark[i]>=50) j++; } printf("\n Total of students passed:%d",j);getch();return 0; }

1.1. What does the program do1.2. Reprogram the coding above to calculate

1.2.1. Total1.2.2. Average

Page 2: Dti2143 lab sheet 8

Exercise 2.0: Compile and run the program below

#include<stdio.h>#include<conio.h>

int main(){

int i,j;float b[3][4]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1,11.1,12.1};

for(i=0;i<3;i++){ for(j=0;j<4;j++) { printf("[%d][%d]=%.1f\n",i,j,b[i][j]); } }

getch();return 0; }

2.1 Reprogram the coding above to:2.1.1 Input 12 number2.1.2 Calculate total2.1.3 Calculate Average

Page 3: Dti2143 lab sheet 8

Exercise 3: Compile and run the program below#include<stdio.h>#include<conio.h>

char matrik[20],nama[50];

float kiratotal(float x, float y, float z){ float a; a=x+y+z; return a; }

float kirapurata(float b){ return (b/3); } int display (float total, float average){printf("\n\n=========================");printf("\n STUDENT'S RESULT");printf("\n=========================");

printf("\nname:%s",nama); printf("\n\nmatrix number:%s",matrik); printf("\n\ntotal:%.2f",total); printf("\n\naverage:%.2f\n",average); if (average<=100 && average>=80) { printf("\ngrade:A",average); } else if (average<=79 && average>=60) { printf("\ngrade:B",average); } else if (average<=59 && average>=40) { printf("\ngrade:c",average); } else if (average<=39 && average>=20) { printf("\ngrade:D",average); } else if (average<=19 && average>=0) { printf("\ngrade:E",average);} }

int main(){float total,average,test,assign,final;char ulang='y';

while (ulang=='y'){printf("Enter student information\n");

Page 4: Dti2143 lab sheet 8

printf("----------------------------");printf("\nMatrik No. : "); scanf("%s",&matrik);printf("\nName : "); scanf("%s",&nama);printf("\nTest Mark : "); scanf("%f",&test);printf("\nAssignment Mark : "); scanf("%f",&assign);printf("\nFinal Exam Mark : "); scanf("%f",&final);

total= kiratotal(test, assign, final);average= kirapurata(total);display(total,average);

printf("\n========================="); printf ("\nDo you want to proceed? Enter 'y' for yes or else for no : "); scanf ("%s",&ulang); printf ("\n\n\n\n\n");}

getch();return 0;}

3.1 Modify the coding above to get input and output from user in a sub function called getio()