DEPARTMENT OF COMPUTER ENGINEERING CMPE101: Foundation of Computer Engineering EXPERIMENT 3. Introduction to C Programming: Sequential code structure

Similar documents
CMPE Experiment 3 Selective Structures

int main(void) { int a, b, c; /* declaration */

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

INTRODUCTION TO COMPUTER SCIENCE - LAB

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Question 2. [5 points] Given the following symbolic constant definition

First of all, it is a variable, just like other variables you studied

THE FUNDAMENTAL DATA TYPES

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

Chapter 3. Fundamental Data Types

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Introduction to C. Systems Programming Concepts

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

ET156 Introduction to C Programming

Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0.

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

A Short Course for REU Students Summer Instructor: Ben Ransford

ECET 264 C Programming Language with Applications

ECET 264 C Programming Language with Applications

ISA 563 : Fundamentals of Systems Programming

Basic Types and Formatted I/O

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C Program Structures

Structured programming

TEST BDA24202 / BTI10202 COMPUTER PROGRAMMING May 2013

Operators and Expressions:

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Content. In this chapter, you will learn:

Programming. Data Structure

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Constants Lesson Outline

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Practice Sheet #07 with Solutions

Principles of C and Memory Management

Engineering 12 - Spring, 1999

Name: Target 12.2: Find and apply surface of Spheres and Composites 12.2a: Surface Area of Spheres 12.2b: Surface Area of Composites Solids

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

Fundamentals of Programming

Pointers. Introduction

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Integer Data Types. Data Type. Data Types. int, short int, long int

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Arithmetic Expressions in C

Assoc. Prof. Dr. Tansu FİLİK

Pointers (2) Applications

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Problem # 1. calculate the grade. Allow a student the next grade if he/she needs only 0.5 marks to obtain the next grade. Use else-if construction.

Selection Statements. Pseudocode

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Variables and Constants

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Multiple Choice Questions ( 1 mark)

large units some names for large numbers CIS 2107 Chapter 2 Notes Part 1

CIS 2107 Chapter 2 Notes Part 1. Representing and Manipulating Information

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Structured Programming. Dr. Mohamed Khedr Lecture 4

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

CSE413 Midterm. Question Max Points Total 100

3. Types of Algorithmic and Program Instructions

Week 3 Lecture 2. Types Constants and Variables

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Language comparison. C has pointers. Java has references. C++ has pointers and references

Numerical Analysis First Term Dr. Selcuk CANKURT

CS 61C: Great Ideas in Computer Architecture Introduction to C

C++/Java. C++: Hello World. Java : Hello World. Expression Statement. Compound Statement. Declaration Statement

What we have learned so far

Data Types and Variables in C language

Function I/O. Last Updated 10/30/18

Princeton University COS 217: Introduction to Programming Systems C Primitive Data Types

Tutorial No. 2 - Solution (Overview of C)

Basic and Practice in Programming Lab7

TEST REVIEW: UNIT 8 Surface Area 2018

Approximately a Test II CPSC 206

Quiz1 Fall 2007 October 2 nd, UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated :October 2nd, 2007.

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

How to declare an array in C?

INFO-I308 Laboratory Session #2

Question 2. [2 points] Which of the following is a correct statement to obtain user input? (Assume that fleems is an int variable.

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Computer System and programming in C

Chapter 3: Arrays and More C Functionality

Fundamental of Programming (C)

CSCI2467: Systems Programming Concepts

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Transcription:

DEPARTMENT OF COMPUTER ENGINEERING CMPE101: Foundation of Computer Engineering EXPERIMENT 3 Introduction to C Programming: Sequential code structure Objectives: 1) Understand how to edit, compile and execute C computer codes. 2) Understand C programming: sequential code structure. Note: Before writing a computer code, you should do the following steps: 1) understand and analyze the problem, 2) develop an algorithm and/or flowchart and 3) convert the algorithm and/or the flowchart into a C code. Task I: Tracing section A/ The following code displays the number of bytes used to store different data types. Edit, compile, and execute the C-code. Record the results. sizeof(datatype) returns the size of a datatype. #include <stdio.h> int main(void) { printf("the size of char is %d bytes\n", sizeof(char)); printf("the size of short is %d bytes\n", sizeof(short)); printf("the size of int is %d bytes\n", sizeof(int)); printf("the size of long is %d bytes\n", sizeof(long)); printf("the size of float is %d bytes\n", sizeof(float)); printf("the size of double is %d bytes\n", sizeof(double)); printf("the size of long double is %d bytes\n", sizeof(long double)); return 0;} B/ Edit the following code segments in a form of C-program and investigate the output. 1) int x, y, z; float a; x = 9 * 0.5; a = 9 * 0.5; y = 15 % 15; z = 15 % 2; printf( x=%d, a=%f, y=%d, z=%d\n,x,a,y,z) 2) #define PI 3.14159 int x, y, z; float a, b; x = 5; y = 10; z = x + (4 * y * y * y) + PI; a = x + (4 * y * y * y) + PI; b = (x / y) + a; printf( x=%d, y=%d, z=%d\na=%7.2f, b=%f\n,x,y,z,a,b);

3) int x = 20, y; y = x++; printf( x=%d, y=%d\n,x,y); 4) int x = 20, y; y = ++x; printf( x=%d, y=%d\n,x,y); 5) int x, y, z; x = 5; y = 10; z = x++ * y 2; z += ++x; printf( x=%d, y=%d, z=%d\n,x,y,z); 6)float x = 1.0, y; int z = 2; y = x + z; 7) int w, z, q; float x = 4.0, y; y = sqrt(x); w = y; z = x w; q = sqrt(3); printf( y=%f,w=%d,z=%d,q=%d,y,w,z,q); 8)int a=9, b=4; float c,d; c = a/b; d = (float)a / (float)b; printf( c=%.2f,d=%.2f,c,d); 9) int i = 5; float j = 314.15; char cr = $ ; printf( %5i, i); printf( %6.1f, j); printf( %f, j); printf( %.1e, j); printf( %10.2e, j); printf( %c, cr); 10) int x, a = 5; x = a++; printf( x=%d\n a=%d\n,x,a); 11) int x, a = 5;

x = ++a; printf( x=%d\n a=%d\n,x,a); 12) int x, a = 5, b = 10; x = a++ + b++; printf( x=%d\na=%d\nb=%d\n,x,a,b); 13) int x, a = 5, b = 10; x = ++a + ++b; printf( x=%d,\n a=%d\nb=%d\n,x,a,b); 14) int x, a = 5, b = 10; x = a++ + --b; printf( x=%d,\n a=%d\nb=%d\n,x,a,b); 15) int a = 3, b = 5; a += b; 16) int a = 3, b = 5; a += b++; 17) int a = 3, b = 5; a *= b (b / 2); 18) int a=3, b=5, c=4; a += b -= --c * 2; printf( a=%d,\n b=%d \nc=%d\n,a,b,c); 19) float x = 4; x += 2 / 3; printf( x=%lf\n,x); 20) float x = 4; x += 2.0 / 3; printf( x=%f\n,x); 21) float x = 4; x += (float)2 / 3; printf( x=%f\n,x); 22) int a = 5, b = 8; a = b; b = a; 23) int a = 5, b = 8, temp;

temp = a; a = b; b = temp; 24) what is the result of 35 / 8 % 3 35 / (8 % 3) 3 + 2 * (12 4 * 11 % 6 * 4 / 3) 25) Write the following mathematical expression in computer form using C programming language: A+B. X F= C+D.Y 4 Task II: Programming tasks 1) Consider the following code that computes ad prints the area of a circle with the radius r. The area of the circle is computed by: 2 area r #include<stdio,h> #include<math.h> #define PI 22.0/7.0 int main(){ double radius,area; printf("enter the radius of a circle: "); scanf("%lf",&radius); area=pi*pow(radius,2); printf("the area of the circle=%lf",area); return 0;} a) Edit, compile and execute this code. Use the following input values for the radius r: 4 b) Modify the given code to read the radius of a sphere and then compute and print the sphere volume. Note: The volume of a sphere of radius r is computed as: 4 3 volume r 3 *** PROGRAM TO CACLCULATE THE VOLUME OF A SPHERE *** Enter the radius of the sphere: 2.0 The volume of the sphere is 33.5.

2) To calculate the total points of a student in CMPE110 course according to his midterm, lab, and final grades, the following weights are used: Midterm 40%, Final 50%, Lab 10%. Write a C code that reads the student s midterm, final and lab grades and then computes and prints on the computer monitor his total points. *** Program to calculate the total points in CMPE110 course *** Enter your grades in final, midterm, and quiz? 70 85 80 Your total point is 77.0 3) Consider the problem of converting an amount of money in US dollars to Turkish Liras (TL). Assuming that the exchange rate is given by: 1 US dollar equals 1.8 TL, write down a C code that will read the amount of dollars and convert it to Turkish lira. *** Program to convert money in $ to TL *** Exchange Rate 1$= 1.8 TL Enter the amount in US dollars: 100 The equıvalnent amount in Turkish Liras is : 180.0 4/ Write a program that reads the radius of the circle inscribed inside the square and based on that it will print the values of red and yellow areas respectively. Print the values with 2 digits after decimal point. Define the value of π (3.1416) as a constant. A sample run of your program gives the following: Enter the value of the radius: 1.578 The yellow area is 2.14 The red area is 7.82