COMPSCI 210 Semester 1- 2015 Tutorial 8 – C programming language.

Slides:



Advertisements
Similar presentations
Recursive Descent Technique CMSC 331. UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D ->
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
COMPSCI 210 Semester Tutorial 10 Exercises from CH 14.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
COMPSCI 210 Semester Tutorial 6 – Revision.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Adding Dynamic Content to your Web Site
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
C Intro.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Introduction to C Programming CE Lecture 1 Introduction to C.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Selection in C.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
©2005 GE Fanuc Automation, Inc. All Rights Reserved PACSystems Training Programmer’s Toolkit.
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
High-Level Programming Languages: C++
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
Programming With C.
COMPSCI 210 Semester Tutorial 7 – C Exercises.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
How to start Visual Studio 2008 or 2010 (command-line program)
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Agenda Computer Languages How to Write a Simple C Program
C Fundamentals CGS 3460, Lecture 4 Jan 18, 2006 Hen-I Yang.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Chapter 1: Introduction to Computers and Programming.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Introduction to C Topics Compilation Using the gcc Compiler
Basic concepts of C++ Presented by Prof. Satyajit De
Compsci 210 Tutorial Five.
Programming what is C++
Topic Pre-processor cout To output a message.
Character Processing How characters can be treated as small integers?
Introduction to Programming
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Introduction to C Language
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Method Mark and Lyubo.
CSC1201: Programming Language 2
Introduction to CS Your First C Programs
CSCE 206 Lab Structured Programming in C
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Command Line Parameters
Character Processing How characters can be treated as small integers?
Introduction to C Topics Compilation Using the gcc Compiler
CSC1201: Programming Language 2
Strings #include <stdio.h>
Computer Security Password Policy.
Displaying Memory/Files
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CSCE 206 Lab Structured Programming in C
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

COMPSCI 210 Semester Tutorial 8 – C programming language

Exercise 8.1 Write a programme to create a file called “inst.txt” and write the following instruction into the file : Add R1, R2, R3 You need the following header only: #include Name the programme Ex8-1

Solution - Exercise 8.1 #include int main() { FILE *fp; fp= fopen("inst.txt","w+"); fprintf(fp,"Add R1, R2, R3\n"); fclose(fp); return 0; }

Exercise 8.2 Write another programme to open the file created in Exercise 8.1, and read the contents using “fgetc” command and print the content on the screen. You need the following header only: #include Name this program Ex8-2

Solution - Exercise 8.2 #include int main(){ FILE *fp; char c; fp=fopen("inst.txt","r"); c=fgetc(fp); while(c!=EOF){ printf("%c",c); c=fgetc(fp); } fclose(fp); return 0; }

Exercise 8.3 Expand your codes in Exercise 8.2 to detect the instruction written in “inst.txt” and print a message declaring the instruction. Your programme should be able to detect whether the instruction is ADD, AND or LD Note: Your programme should be able to convert the contents of the file to lowercase before doing any detection. You need to add the following headers: #include For example it must detect that the instruction written in the file is ADD and print a message like : “This is ADD instruction”

Solution - Exercise 8.3 #include int main() { FILE *fp; char c; char reader[100]; char delimiter[2]=" "; char *token; int i; fp=fopen("inst.txt","r"); c=fgetc(fp); i=0; while(c!=EOF){ printf("%c",c); reader[i]=tolower(c); i++; c=fgetc(fp); } fclose(fp); printf("%s", reader);

Solution - Exercise 8.3 cont. token=strtok(reader,delimiter); if(strcmp(token,"add")==0)printf("This is ADD instruction \n"); else if(strcmp(token,"and")==0)printf("This is AND instruction\n"); else if(strcmp(token,"ld")==0)printf("This is LD instruction\n"); return 0;}