Introduction to Algorithms and Programming I Lab. Exercises #1 Solution

Similar documents
3/13/2012. ESc101: Introduction to Computers and Programming Languages

Session 1: Accessing MUGrid and Command Line Basics

Programming and Data Structure Laboratory (CS13002)

Lab 1 Introduction to UNIX and C

Programming Studio #1 ECE 190

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

CS CS Tutorial 2 2 Winter 2018

Programming Studio #1 ECE 190

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

1. The Mac Environment in SIE 1222

Lab 1 Introduction to UNIX and C

Intermediate Programming, Spring Misha Kazhdan

Practical Session 0 Introduction to Linux

CSCE 212H, Spring 2008, Matthews Lab Assignment 1: Representation of Integers Assigned: January 17 Due: January 22

1. The Mac Environment in Sierra Hall 1242

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Temple University Computer Science Programming Under the Linux Operating System January 2017

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Introduction to Linux

Unit 10. Linux Operating System

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Unit 13. Linux Operating System Debugging Programs

Introduction. SSH Secure Shell Client 1

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

Working with Basic Linux. Daniel Balagué

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Turn-in your project from your Windows PC

Helpful Tips for Labs. CS140, Spring 2015

CMSC 104 Lecture 2 by S Lupoli adapted by C Grasso

Unix Basics. Systems Programming Concepts

The Unix Shell & Shell Scripts

CS11001/CS11002 Programming and Data Structures Autumn/Spring Semesters. Introduction

Introduction: The Unix shell and C programming

C++ Programming on Linux

LAB 1 INTRODUCTION TO LINUX ENVIRONMENT AND C COMPILER

AMS 200: Working on Linux/Unix Machines

CS19001/CS19002 Programming and Data Structures Lab Autumn/Spring Semester. Introduction. Abhijit Das. January 4, 2015

Lecture 1. A. Sahu and S. V. Rao. Indian Institute of Technology Guwahati

CSE Linux VM. For Microsoft Windows. Based on opensuse Leap 42.2

Parallel Programming Pre-Assignment. Setting up the Software Environment

For Dr Landau s PHYS8602 course

PDS Lab Section 16 Autumn Tutorial 1. Unix Commands pwd The pwd command displays the full pathname of the current directory.

Chapter 3 DATA REPRESENTATION

EECS Software Tools. Lab 2 Tutorial: Introduction to UNIX/Linux. Tilemachos Pechlivanoglou

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

CS11002 Programming and Data Structures Spring Introduction

How to SFTP to nice.fas.harvard.edu from Windows

Lab 1 1 Due Wed., 2 Sept. 2015

CENG393 Computer Networks Labwork 1

Guided Tour (Version 3.3) By Steven Castellucci as Modified by Brandon Haworth

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Course Information and Introduction

Basic Unix Commands. CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Introduction to Computer Science (I1100) With 1 coin 2 possibilities: Head / Tail or 0/1

Once you have installed MobaXterm, open MobaXterm. Go to Sessions -> New Session, and click on the SSH icon.

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

بسم اهلل الرمحن الرحيم

Using the Unix system. UNIX Introduction

Tiny Instruction Manual for the Undergraduate Mathematics Unix Laboratory

Systems Programming and Computer Architecture ( ) Exercise Session 01 Data Lab

CS1600 Lab Assignment 1 Spring 2016 Due: Feb. 2, 2016 POINTS: 10

Introduction to Linux. Fundamentals of Computer Science

Linux Tutorial. Ken-ichi Nomura. 3 rd Magics Materials Software Workshop. Gaithersburg Marriott Washingtonian Center November 11-13, 2018

Introduction to Linux Environment. Yun-Wen Chen

CSC111 Computer Science II

UoW HPC Quick Start. Information Technology Services University of Wollongong. ( Last updated on October 10, 2011)

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Introduction to Linux

CS Operating Systems, Fall 2018 Project #0 Description

Introduction to Linux (Part I) BUPT/QMUL 2018/03/14

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

Week 2 Lecture 3. Unix

Your algorithm and program should print values for the following:

commandname flags arguments

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

Python for Astronomers. Week 1- Basic Python

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Please choose the best answer. More than one answer might be true, but choose the one that is best.

Unix Tutorial Haverford Astronomy 2014/2015

Introduction to Computing V - Linux and High-Performance Computing

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Working with Shell Scripting. Daniel Balagué

COMP s1 Lecture 1

The Shell. EOAS Software Carpentry Workshop. September 20th, 2016

Guided Tour (Version 3.4) By Steven Castellucci

STA 303 / 1002 Using SAS on CQUEST

Computer Organization and Assembly Language. Lab Session 01

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

FILE MAINTENANCE COMMANDS

Unix Introduction to UNIX

Introduction to Linux

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Refresher workshop in programming for polytechnic graduates General Java Program Compilation Guide

Operating Systems and Using Linux. Topics What is an Operating System? Linux Overview Frequently Used Linux Commands

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Transcription:

60-140 Introduction to Algorithms and Programming I Lab. Exercises #1 Solution Objectives are to: 1. Learn basic Unix commands for preparing a source C program file, compiling a C program and executing a C program with input data from the keyboard. Also, learn how to use script file for handing in record of source program file, compilation and execution of programs. 2. Practise on concepts taught in chapter 1 of text including how to present solutions to simple problems in the form of an algorithm (or program), how to identify the necessary input and output data of a problem. Also, practise on conversions of numeric values from one number base to another, and character data from character to ASCII code and vice versa. Que. 1. Type, compile and run the C program of Figure 1.15 in the course book. This program computes and prints the binary number equivalent of any given positive decimal number. Hand in or show your source program file, the input data and the output of your run. Use the following set of input data: 52 300 436 16 98 Solution to Que 1 /* Given a decimal number, convert it to its binary equivalent. We are also including <math.h> to use math built functions, but must compile with the -lm option to link in math library with command cc -lm file.c */ #include <stdio.h> #include <math.h> /* Given a decimal number, convert it to its binary equivalent. For example, given 52, it returns 110100. Note though that we can only work with integer numbers and we need to find a way to present our output binary number like 110100 correctly as an integer. */ int main (void) { int num_d; /*input number */ int num_b; /* output number with only digits 1 and 0 */ int decimalnum, quotient, remainder, power2; /* other variables */

/* Now sequence of instructions for solving the problem */ printf("type the decimal number you want to convert to binary:\t"); scanf ("%d", &num_d); /*read decimal number */ power2 = 0; /* we set the bit position as least significant */ num_b = 0; /* set the current binary number to 0 */ /* compute its binary equivalent */ decimalnum = num_d; /* make a copy of the decimal number */ step5: quotient = num_d / 2; remainder = num_d % 2; num_b = num_b + (remainder * pow (10, power2)); if (quotient!= 0) { num_d = quotient; power2 = power2 + 1; goto step5; /* this repetition is better done with while loop instruction but kept this way for simplicity */ /* print the number and its binary equivalent */ printf ("Binary equivalent of %d is %d.\n", decimalnum, num_b); return 0; Que. 2. Compile and run the same program of Figure 1.15 of text and show the source code, compilation, execution, program input and output data in a script file. Solution to Que 2 A record of all Unix commands executed during a logon session or part of a logon session can be saved in a script file by simply initiating the recording with the Unix script command and ending the recording when completed, with an exit command as follows. i. Open a Unix terminal window and type: script lab01_scriptfile [The general command is script filename, our script file here is lab01_scrptfile] ii. Now display your source program file with the Unix cat command as: cat lab01.c [The command cat filename is used to display contents of filename on the screen] iii. Now compile the program by typing: cc lab01.c

iv. Now Run the program by typing: a.out v. Now, the CPU is waiting for you to type in a positive decimal number as before. You can re-run the program with the following decimal numbers. 52 300 436 16 98 vi. After the result of the program has been displayed, you must exit script session by typing: exit [Failure to exit will prevent the script file from being saved and created.] Que. 3. Practise with other Unix commands to list all the files in your directory, see the contents of your script file, send the script file to your GA, send your script file to your home computer so that you can print it. Solution to Que 3 i. To see all files in the current directory including the script file, type: ls ii. To see contents of the script file, type: cat lab01_scriptfile or more lab01_scriptfile iii. To send a file like your script file lab01_scriptfile (which is currently on the server.uwindsor.ca) to your home computer so that you can print it, you need to use SSH file transfer program (SFTP) to transfer files from one computer (e.g., sol.cs.uwindsor.ca) to another (e.g., a PC or laptop). Note that you can only do this exercise when you have a PC or laptop and still connected to a Unix server. Thus, you can complete it at home to learn how to transfer your files and assignments. To use SFTP, follow the steps below: a. From your home PC computer or any PC or laptop that has internet connection and already has the SSH client installed on it, launch the SSH client software to log on to a campus Unix server from PC. You can log on to sol.cs.uwindsor.ca or any new server name available at the time of doing this lab. While the SSH terminal or window is used to issue any Unix command on the remote server, the SFTP terminal or window is used to transfer files between the two computers. Open up the SFTP terminal (the secure file transfer program) icon. This SFTP window looks like popular Windows explorer window. On the SFTP window, there are two panes where the left pane has a listing of files in the local computer (e.g., PC or laptop) and the right pane has a listing of files in the remote computer (e.g., the Unix server like sol.cs.uwindsor.ca). b. Use the SFTP menu commands to download files from Unix server to PC and to upload files from PC to Unix server. You can also just drag and drop files from one computer (e.g., right pane) to another (e.g., left pane) the same way you would do it with Windows explorer.

Now, download your scriptfile called lab01_scriptfile. c. Print your scriptfile lab01_scriptfile with a printer connected to the PC or laptop. Once you have downloaded your scriptfile lab01_scriptfile onto your PC, you can open it up with Wordpad or Notepad and print the file so it can be handed in for marking later. iv Practise with other Unix commands for creating and deleting directory, deleting file, changing directory, renaming a file, making a copy of a file and so on. To create a new directory on Unix, use the mkdir command as: mkdir dirname v. To change to a different directory on Unix, use the cd as: cd dirname [Note that dirname stands for the entire path of the directory from root (/)or home directory (~)]. vi. To move up one directory, for example to the parent directory, use: cd.. vii. Wherever you are, to go to your home directory, use: cd ~ viii. To make a copy of lab01.c file and keep in lab01cp.c, use: cp lab01.c lab01cp.c ix. To delete a file like delete lab01.c after making a copy, use: rm lab01.c x. To rename a file from lab01.c to lab02.c, use: mv lab01.c lab02.c Use the ASCII table in Appendix A of course text, for exercises involving ASCII conversion. Que. 4. Here is a message coded in ASCII using eight bits per symbol. What does it say? 01001001 00100000 01110111 01101111 01110010 01101011 00100000 01101000 01100001 01110010 01100100 00101110 Solution to Que 4 01001001 = 73 = I 00100000 = 32 = 01110111 = 119 = w 01101111 = 111 = o 01110010 = 114 = r 01101011 = 107 = k 00100000 = 32 = 01101000 = 104 = h 01100001 = 97 = a 01110010 = 114 = r 01100100 = 100 = d 00101110 = 46 =.

Que. 5. Show how the following instructions are represented in binary using ASCII by writing the codes for the instructions. Q /= 20; a--; Solution to Que 5 a. b. Q = 01010001 / = 00101111 = = 00111101 20 = 00010100 ; = 00111011 a = 01100001 - = 00101101 - = 00101101 ; = 00111011 Que. 6. Convert each of the following binary representations to its decimal form. (a) 110100 (b) 1000000 (c.) 1111111 Solution to Que 6 a. 0110100 = 52 b. 1000000 = 64 c. 1111111 = 127 Que. 7. Convert each of the following decimal representations to its equivalent binary form. (a) 21 (b) 53 (c.) 400 Solution to Que 7 a. 21 = 10101 b. 53 = 110101 c. 400 = 110010000 Que. 8. Name all the hardware components of the computer and identify the function of each component.

Solution to Que 8 Hardware Component Function of Component Input device Used to accept input from user Output device Used to display computer output to the user Memory Used to store programs and data to be Primary memory processed by the computer. Secondary memory CPU (central processing unit) Used to process program instructions. Que. 9.What is an algorithm? What is a program?, What is a problem?, What are input and output data? Solution to Que 9 An algorithm is a sequence of steps for transforming input data to a desired output data. A problem has some input data, some output data and needs an algorithm for transforming its input data to its output data. A program is the version of an algorithm written in a Computer executable language like a computer high-level or low-level language Que. 10. For each of the conversions you did in problems (4) to (7) above identify the input, output and algorithm. Solution to Que 10 The coarse algorithms are: Qu4: Input: numbers(representing binary equivalent of a character), ASCII table Output: a string of characters Algorithm: For each input number do: read each eight digit integer number representing a binary number convert this binary number to its to decimal equivalent convert this decimal number to ascii character using the ASCII table print out the ASCII character equivalent for the decimal number. repeat Qu5: Input: string of characters, ASCII table Output: binary numbers Algorithm: For each input character do:

read each each input charact convert each character to decimal value using the ASCII table convert the decimal value to binary number print binary number repeat Qu6: Input: binary number (binary) Output: decimal number (dec) Intermediate: powerof2 = 0, quotient, remainder Algorithm read in binary number dec = 0 remainder = binary%10 quotient = binary/10 while (quotient!= 0) do: { dec = dec + (remainder * (2 ^ powerof2)) powerof2 = powerof2 + 1 remainder = quotient % 10 quotient = quotient / 10 print dec Qu7: Input: decimal number (decimal) Output: binary number (binary) Intermediate: powerof10 = 0, quotient, remainder Algorithm: read decimal number binary = 0 remainder = decimal % 2 quotient = decimal / 2 binary = binary + (remainder * (10 ^ powerof2)) while (quotient!= 0) do { power2 = power2 + 1 remainder = quotient % 2 quotient = quotient / 2 binary = binary + (remainder * (10 ^ powerof2)) print binary