ANew - Lab1 - C Programing With Quincy

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

PENANG CAMPUS

SCHOOL OF ENGINEERING, SCIENCE AND TECHNOLOGY

ENGINEERING DEPARTMENT

DIPLOMA IN ELECTRICAL AND ELECTRONIC ENGINEERING


(MECHATRONICS)

(DIP-EEM)
LEONG MIN SHEN ( 0189074 )

Microcontroller System Design and Applications (EED2323/EED1324)

Lab 1 – C Programming with Quincy

DURATION: 2 HOURS

TOTAL MARKS: 100

Plagiarism

The assignment is based on an individual response. The report must be completely your own work and
you must not copy from others. Any plagiarized work will be zero-rated. Any reference material you use
(books, journals, Internet, magazines etc.) must be clearly identified in your report using procedures in the
Harvard System of Referencing.
Lab 1

Create codes for the following questions.

a) Given the following for loop, convert it to a while loop.

b) Write a complete C program using while loop to compute total course fees for students.

 Prompt the user to enter their name (string), id no (string), duration of study (integer) and total
subjects taken with marks for each.

 Suppose that the course fee is starting from RM10k in the first year and increases 5% the
following

 years.

 Calculate the annual fee and total course fees for the whole duration of study.

 Display all information as shown below.


Your report must include introduction, aims, source code, snapshots of working program, discussion of
each code and conclusion.
Introduction

Loop are used to repeat a block of codes. Being able to have program that repeatedly execute a block
of code is one of the most basic but useful tasks in programming. A loop will lets us to write a very
simple statement to produce a significantly greater result simply by repetition. There are three types of
loop in C++ programming, which are For Loops, While Loops and Do While Loops. We’ll discuss about Do
While Loops since both of the questions are using While Loops.

Aims

To investigate the function of while loop structure in a C programming.

Source Codes

#include<stdio.h>

int main()

int no=2;

printf ("List of integer numbers : \n");

while ( no<=10)

printf ("%d\n",no);

no=no+2;

return (0);

}
Working Program

Discussion

Above codes are required to convert from for loop structure into while loop structure. A C program
line will always begins with a preprocessor directives. First, we will begin our program with “ #include<
stdio. h> ” codes which identifies the header file for standard input and output needed by the printf()
and scanf() statement. After that, it is followed by the code of “int main()”. “int main()” code is a
function that expects unknown number of arguments of unknown types. It can returns an integer
representing the status of application software. “main()” function is used to identify the start of a
program. We must always remember to insert an opening brace to indicate the open segment after the
line of “int main()”. Then, we have to initialize integer number by writing a code “int no=2” with initial
integer number of 2.”int” code is the declaration of integer either it’s in positive or negative. “printf” is a
code that used to send data standard output to be printed according to specific format. Based on the
written code above, code “printf(“ List of integer numbers :\n”)” means the program will execute this
code by printing exactly same sequence of all the number of characters surrounded by double quotation
marks. Code “\n” known as new line is an escape sequence in coding. The syntax of the while loop
stated above is while (condition) and then followed by statement(s). .”printf(“%d\n” ,no) will allows the
program to display integer number from the variable. “%d” is a placeholder for integer variable and the
“no” is the marks for displaying the position for the type of integer variable. While loop with condition of
“no<=10” state that integer numbers must be less than or equal to 10.While, statement with condition
of “no=no+2” stated that there will be increment of 2 for the integer number once the condition is true.
For example, if the integer number is 11 (false condition), the program will stop executing and return
0.The system will show the correct answer which are 2, 4, 6, 8, 10.We must insert code “return(0)” to
end a program. Finally, a closing brace must be inserted to indicate the whole close segment.

Source Codes

#include<stdio.h>

int main()

char name[80],id[20];

int duration,subject,marks,count,totalmarks;

double fees,totalfee,average;

totalmarks=0;

totalfee=0;

printf("\nEnter name : ");

gets(name);

printf("\nEnter student id : ");

scanf("%s",&id);

printf("\nEnter duration of study [year] : ");

scanf("%d",&duration);

printf("\nPlease enter amount of subjects : ");

scanf("%d",&subject);

printf("\n");

count=0;

while(count<subject)

printf("Enter mark subject number %d : ",count+1);


scanf("%d",&marks);

if ( marks >=0 & marks<=100)

totalmarks=totalmarks+marks;

count+=1;

average=totalmarks/subject;

printf("==================================\n");

printf("\tKDU COLLEGE\n");

printf("==================================\n");

printf("Student name: %s\n",name);

printf("Student id: %s\n",id);

printf("Amount of subjects: %d\n",subject);

printf("Average marks: %.2f\n",average);

printf("Duration of study: %d\n",duration);

printf(" Year\tCourse Fee\n");

count=0;

fees=10000;

while(count<duration)

printf(" %d \tRM %.2f\n",count+1,fees);

totalfee=totalfee+fees;

fees=(fees*1.05);

count++;
}

printf("=================================\n");

printf("Total Course Fees: RM %.2f\n",totalfee);

printf("=================================\n");

return(0);

Working Program
Discussion

Question (b) is a question that asking us to compute total course fees for the whole study duration of
student using while loop structure. The initial course fee is given and it will increases 5% per year. We
are also required to enable the user to enter their name in string form, student id in string form,
duration of study in integer form and total subjects taken with marks for each in integer form too.

As I stated in the discussion of question (a),“#include<stdio.h>” is a code that must be written at the
very beginning of every C program and then followed by main function of “int main()” .After this two
lines of code, we must always remember to insert an opening brace that indicates the start of
segment .Declaration is a statement of the program that tells the compiler the names of memory cells in
a program. There are four basic data types in programming usually are int, float, double and char. Based
on the question, we will declare name and student id as char. Char name(80) means there will be
maximum storage size of 80 for the user to enter the character name while char name id(20) means user
can only insert a maximum number of 20 character number as student id. Variables of duration, subject,
marks, count and total marks are declared as integer. Besides, we have declared fees, total fee and
average as double because these three variables have higher precision and higher range of numbers.
Initial values of total marks and total fees must be 0 to enable us to obtain correct values at the next
calculation.
After variables declaration, we will start to write “printf(“string literal”); ” followed by
“scanf(“format string” ,&variable)” for each variable. Term “Printf(“string literal”) means program will
print and display the sequence of any number of characters surrounded by double quotation marks on
the screen. While “scanf(“format string” ,&variable)” allows data to be read from the standard input
device and store it in a variable. For variable in string form, we must use “gets (name)” instead of
“scanf(“format string” ,&variable)”.This is because term “gets(name)” is to ask the compiler to take the
input character by user and store them into variable name. “Printf(“\n”)” will displayed a next line on
the screen.

Variable “count” must be initially zero. There are two conditional statement which are if statement
and while statement. Conditional statements give us the power to make basic decisions during
programming. ”While (count<subject)” enable looping to function once the count is smaller than
subject, but it will stop looping when count is greater than subject. “ printf("Enter mark subject number
%d : ",count+1);” known as “printf(“format string” ,variables); , will print the sequence of number of
characters surrounded by double quotation marks on the screen, ”%d” inside the double quotation
marks is the placeholder that will display marks based on the input. So, we have to write”
scanf(“%d” ,&marks )” in order to enter marks into the program. Since count =0, so “count+1” as
variable that locate inside “printf(“format string” ,variables); will let the first loop to have count number
of 1.There are two decisions stated in if statement where the marks can be in the range of 0-100. For
example, if the marks are out of range, the loop still cannot proceed to the next loop even the while
loop statement at the beginning is correct. If all the while loop statement and if statement are correct, it
will continue to calculate total marks before the next loop. As I mentioned earlier, total marks is initially
zero. At the first loop, “total marks = total marks + marks” means overall total marks = 0+ marks (input
manually).So, for the next loop, the overall total marks will be the sum of the marks(input manually) and
the previous overall total marks. However, while loop will still proceeding by increment of 1 until stop
when the condition is false. Next, “average=total marks/subject” codes will executing by calculate the
average of the overall total marks by given the equation of “total marks /subject”.
Code of “printf(“===============\n”); means the program will display =============== followed by a
new line behind it. Code of “ printf(“\t KDU COLLEGE\n”)” means the screen will displayed tab then
followed by letter of “KDU COLLEGE” and then followed by a new line again. Codes of printf("Student
name: %s\n" ,name); printf("Student id: %s\n" ,id); printf("Amount of subjects: %d\n", subject);
printf("Average marks: %.2f" ,average); printf("Duration of study: %d\n" ,duration); are known as
“printf(“format string “,variable); . These function will be used to send data to the screen to be printed
according to specific format. “%.2f”means the output result will have 2 decimal places while “%s”
represent string. Code “printf(“Year\t Course Fee\n”); will print out letter of “Year Course Fee” and
then followed by a new line.

Program line above will print out the result as shown below:

====================

KDU COLLEGE

====================

Student name:

Student id:

Amount of subjects:

Average marks:

Duration of study:

Year Course Fee


Variable “count” in this case is exactly same as the “count “stated above .The count is initialized to zero.
Code “Fees=10000” means initial fees is 10000. “While (count<duration)”enable the programs to carry
out While loop. If the count is smaller than duration (True Condition), looping will be continued. But, if
the count is bigger than duration (False Condition), the looping will stop. Code “printf(“ %d\t RM %.2f\
n”,count+1,fees) will print out the input value stored that is a integer variable (”%d”) and then it will
continuously print another input value stored that has 2 decimal places(“%.2f).Code”count+1” will
marked to display the position for the type of integer(“%d”). Since the initial count=0,”count+1” as
variable will let the first loop to have count number of 1. Code “fees” will marked to display the position
for the type of integer (“%.2f”).For the first loop, the fees will also be initially 10000 since we have
mentioned “Fees=10000” at the beginning. If the first while looping is in true condition, then it will
proceed to the next calculation part where “Total fee=total fee + fees”. The initial total fee is 0.At the
first loop, “Total fee=total fee + fees” means overall total fee=0+ fees(10000).The following code is
“fees=(fees*1.05) which means the overall fees will multiply with 1.05.Then,while loop will still continue
functioning by increment of 1 if condition is still true. After finishing the first looping, the looping will
keep continue functioning and stop once the count is more than the duration. Next, code “print
(“====================\n”); followed by code “printf(“Total Course Fees: RM %.2f\n”, total fee); and
then code (“====================\n”); will be written. Both codes “print (“====================\
n”); will make the program to display “==================== “and then followed by spacing down a
new line. While code “printf(“Total Course Fees: RM %.2f\n”,total fee); will displayed the letter of “
Total Course Fees: RM “.The following display will be the input value stored represented by “%.2f” and
the “total fee” will be a variable that marked to display the position for the type of integer variable.
Code of “return (0)” function as ending the whole program. Last but not least, we have to insert a
closing brace to indicate the end of the whole segment.
Conclusion

Throughout this experiment, we had learnt about while loop structure where its statement will
repeatedly executes a target statement as long as a given condition is true. Statement may be a single
statement or a block of statements. While, the condition may be any expression, and true is any non-
zero value. The key point of the “while loop” is that loop might not ever run. The loop iterates while the
condition is true only. However, once the condition is tested and given a false result, the loop body will
be skipped and the first statement after the while loop will continue be executed.

You might also like