Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

For loops For loops are controlled by a counter variable. for( c =init_value;c
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
Lecture 4 Introduction to Programming in C Prof. Dr. Arne Kutzner Hanyang University / Seoul Korea.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Chapter 5: Structured Programming
Chapter 5: Structured Programming
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
Computer Programming for Engineers
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
The Repetition control structure using while loop.
EKT120: Computer Programming
EKT120 COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Looping.
CSCE 206 Lab Structured Programming in C
1) C program development 2) Selection structure
Loops in C.
A function with one argument
Functions.
CSCE 206 Lab Structured Programming in C
DATA TYPES There are four basic data types associated with variables:
The while Looping Structure
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Principle Prog Revision

Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int INITIAL = 0; int auto,num1,num2; num1 = 2 % INITIAL; printf(“The value of INITIAL is &d\n”,INITIAl) } (b)Write the C codes to perform the following tasks: Assigning an integer value 5 to a variable named numberTemp. [1 mark] Assigning a value 0 to a variable named numberConst using #define preprocessor directive. [1 mark] Display character ‘A’ using placeholder %c. [1 mark]

Question 2 Write the output of the following program. #include int main() { int i, j, sum=0; for(i = 3 ; i < 17 ; i += 7) { for(j = 9 ; j > 2; j -= 3) { printf("%d x %d =%d\n",i,j,i*j ); sum += i + j; } printf("The sum is %d\n",sum); return 0; } [8 marks]

Question 3 (a)List THREE (3) types of selection control structure in C programming language? [3 marks] (b) Study on the following code: #include int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); //get marks if (marks >50) // if marks >50 display message printf("You have passed the exam!"); return 0; } Modify program above, so that it displays two messages “You have failed the exam!” and “Please study to improve your grade”, if marks are less than or equal to 50. [5 marks]

Question 4 (a)What is infinite loop? Give one example? [3 marks] (b)Study the code below and determine the exact output when this program is executed. #include int main() { int marks; for (int cnt = 0; cnt <=5; cnt=cnt + 1) { if (cnt != 3) continue; printf("cnt = %d\n",cnt); } printf(“for loop process\n”); return 0; } [2 marks]

(c) Study on the following table: Link each of the above repetition type to its appropriate feature. Repetition types Features (I)do…while (A) The statement(s) inside body of loop will always get executed (I) while (A)The statement(s) inside body of loop will be executed once no matter what (III) for(C) It has three expressions and each expression is separated by a semicolon (;)

Question 5 (a) What is the output of the following program segment? int main(void) { int a = 10, b = 2, c = 5, d; d = compute(a, b, c); printf (“Result of compute = %d\n”, d); return 0; } int compute(int m, int x, int c) { return m*x + c; } [3 marks]

(b) What is the output of the following program segment? int main(void) { int num1 = 100, num2 = 200; printf (“num1 = %d, num2 = %d\n”, num1, num2); myFunc(num1, num2); printf (“num1 = %d, num2 = %d\n”, num1, num2); return 0; } void myFunc(int num1, num2) { num1 = num1 + 5; num2 = num2 – 10; printf (“num1 = %d, num2 = %d\n”, num1, num2); } [6 marks]

Question 6 The program below is demonstrating how to calculate sum of numbers that could be divided by 7 in an array that consists of 20 numbers. Please fill in the EIGHT (8) blanks as labeled (i) to (viii) below: #include int main() { int i,a[20],sum__[i]__; for(i=__[ii]__; i<_[iii]__; i__[iv]___) { printf("Please enter number:"); scanf("__[v]____",__[vi]____); if(__[vii]______==0) sum=__[viii]_____ } printf("The sum is %d\n",sum); return 0; }

The C program below makes use of THREE (3) functions: getNumbers(), printNumbers() and sumNumbers(). int main(void){ int myArray[100], numint, result; printf(“How many numbers to be entered? ”); scanf(“%d”, &numint); getNumbers(myArray, numint); printNumbers(myArray, numint); result = sumNumbers(myArray, numint); printf(“Sum of numbers entered = %d”, result); return 0; } Based on the program written above, write down the function prototypes for the THREE (3) functions.[3 marks] (a) Write the function getNumbers(). This function is supposed to ask user for a list of integers and store them in the array passed as the first argument. The number of integers to be entered is passed as the second argument. [4 marks] (b) Write the function printNumbers(). This function is supposed to print out the integers inside the array passed in the first argument. The number of integers stored in the array is passed as the second argument. [4 marks] (c) Write the function sumNumbers(). This number is supposed to sum up all the integers inside the array passed as the first argument. The number of integers stored in the array is passed as the second argument. The function must return an integer which is the result of the summation. [4 marks]